#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

define TOOLS
	cache_check
	cache_dump
	cache_metadata_size
	cache_repair
	cache_restore
	cache_writeback
	thin_check
	thin_delta
	thin_dump
	thin_ls
	thin_repair
	thin_restore
	thin_rmap
	thin_metadata_size
	thin_metadata_pack
	thin_metadata_unpack
	thin_migrate
	thin_trim
	era_check
	era_dump
	era_invalidate
	era_restore
endef

include /usr/share/dpkg/default.mk

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

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

%:
	dh $@ --buildsystem=cargo

override_dh_installman: $(foreach i,$(TOOLS),debian/thin-provisioning-tools/usr/share/man/man8/$(i).8)
	dh_installman

override_dh_link:
	mv debian/thin-provisioning-tools/usr/bin debian/thin-provisioning-tools/usr/sbin
	dh_link $(foreach i,$(TOOLS),usr/sbin/pdata_tools usr/sbin/$(i))

debian/thin-provisioning-tools/usr/share/man/%.8: %.txt
	@mkdir -p $(dir $@)
	bin/txt2man -t $(basename $(notdir $<)) -s 8 -v "System Manager's Manual" -r "Device Mapper Tools" $< > $@

VENDOR_TARBALL = thin-provisioning-tools_$(DEB_VERSION_UPSTREAM).orig-$(CARGO_VENDOR_DIR).tar.xz

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

vendor-deps:
	if QUILT_PATCHES=debian/patches quilt applied | grep vendor-remove-unused-deps ; then \
	 echo "Detecting patch on vendor dir applied, aborting."; \
	 exit 1; \
	fi
	rm -rf $(CARGO_VENDOR_DIR)
	# Deliberately don't use the wrapper set in CARGO, as it expects the
	# configure step to have occurred already.
	cargo vendor --manifest-path Cargo.toml $(CARGO_VENDOR_DIR)
	# Remove some crates that aren't useful for us and take up a lot of space
	rm -r \
	 $(CARGO_VENDOR_DIR)/winapi \
	 $(CARGO_VENDOR_DIR)/winapi-* \
	 $(CARGO_VENDOR_DIR)/windows-* \
	 $(CARGO_VENDOR_DIR)/windows_* \
	 # End of list
	# Remove unused and undesirable data files (e.g. test data for vendored crates)
	rm -r \
	 $(CARGO_VENDOR_DIR)/*/tests \
	 $(CARGO_VENDOR_DIR)/zerocopy/testdata \
	 $(CARGO_VENDOR_DIR)/nix/test \
	 # End of list
	# 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-sanity-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=thin-provisioning-tools_$(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)
