#!/usr/bin/make -f

export ASSET_OUT_DIR=debian/tmp

include /usr/share/dpkg/pkg-info.mk

# enable on 64-bit
execute_after_dh_auto_configure:
	echo 'lto="fat"' >> debian/cargo_home/config.toml
	echo 'codegen-units=1' >> debian/cargo_home/config.toml
	echo 'opt-level="z"' >> debian/cargo_home/config.toml
	cat debian/cargo_home/config.toml

%:
	dh $@ --buildsystem cargo --with bash-completion

export CARGO = /usr/share/cargo/bin/cargo
export CARGO_VENDOR_DIR = rust-vendor
export CARGO_HOME = $(CURDIR)/debian/cargo_home

include /usr/share/rustc/architecture.mk
export DEB_HOST_RUST_TYPE DEB_HOST_GNU_TYPE

VENDOR_TARBALL = $(DEB_SOURCE)_$(DEB_VERSION_UPSTREAM).orig-$(CARGO_VENDOR_DIR).tar.xz

vendor-tarball-quick-check:
	if [ -e ../$(VENDOR_TARBALL) ]; then echo "../$(VENDOR_TARBALL) already exists, bailing!"; exit 1; fi

vendor-deps-cargo:
	if QUILT_PATCHES=debian/patches quilt applied | grep vendor-remove-unused-deps ; then \
		echo "Detecting patch on vendor dir applied, aborting."; \
		exit 1; \
		fi
	# We need to have the dependencies removed in the top-level Cargo.toml or we vendor too much
	QUILT_PATCHES=debian/patches quilt applied | grep remove-dependencies.patch || QUILT_PATCHES=debian/patches quilt push remove-dependencies.patch
	rm -rf $(CARGO_VENDOR_DIR)
	# Deliberately don't use the wrapper, as it expects the configure step
	# to have occurred already.
	# If you have to modify the path here, don't forget to change the README.source doc
	# as well.
	env -i cargo-vendor-filterer  -F crypto-openssl --no-default-features --tier 2  --platform '*-*-linux-gnu' --platform '*-*-linux-gnueabi' $(CARGO_VENDOR_DIR)

vendor-deps: vendor-deps-cargo
	# Remove unused and undesirable data files (e.g. test data for vendored crates)
	# The sequioia creates have a custom build script that uses the tests, so we keep them.
	mv $(CARGO_VENDOR_DIR)/sequoia-policy-config/tests $(CARGO_VENDOR_DIR)/sequoia-policy-config/tests.bak
	mv $(CARGO_VENDOR_DIR)/sequoia-openpgp/tests $(CARGO_VENDOR_DIR)/sequoia-openpgp/tests.bak
	rm -r \
		$(CARGO_VENDOR_DIR)/*/test-data \
		$(CARGO_VENDOR_DIR)/*/tests
	mv $(CARGO_VENDOR_DIR)/sequoia-policy-config/tests.bak $(CARGO_VENDOR_DIR)/sequoia-policy-config/tests
	mv $(CARGO_VENDOR_DIR)/sequoia-openpgp/tests.bak $(CARGO_VENDOR_DIR)/sequoia-openpgp/tests
	# Remove the checksum files to allow us to patch the crates to remove extraneous dependencies
	for crate in $(CARGO_VENDOR_DIR)/*; do \
		sed -i 's/^{"files":.*"package":"\([a-z0-9]\+\)"}$$/{"files":{},"package":"\1"}/' $$crate/.cargo-checksum.json; \
		done
	# Cleanup temp files
	rm -rf $(CARGO_HOME)

vendor-tarball: vendor-tarball-quick-check vendor-deps
	tar --sort=name --mtime=@0 --owner=0 --group=0 --numeric-owner --pax-option=exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime -caf ../$(VENDOR_TARBALL) $(CARGO_VENDOR_DIR)

override_dh_auto_configure:
	DEB_CARGO_CRATE=sequoia-sqv_$(shell dpkg-parsechangelog --show-field Version) \
					$(CARGO) prepare-debian $(CARGO_VENDOR_DIR)
#	/usr/share/cargo/bin/dh-cargo-vendored-sources
	dh_auto_configure

override_dh_clean:
	dh_clean
	rm -rf $(CARGO_HOME)
