#!/usr/bin/make -f

# Defines DEB_*_RUST_TYPE triples
include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildflags.mk
include /usr/share/rustc/architecture.mk
export CFLAGS CXXFLAGS CPPFLAGS LDFLAGS
export DEB_HOST_RUST_TYPE DEB_HOST_GNU_TYPE

export CARGO_HOME = $(CURDIR)/debian/cargo_home
export CARGO_VENDOR_DIR = rust-vendor
VERSION := $(shell dpkg-parsechangelog -S Version|sed -e "s|.*~\(.*\)-.*|\1|g")
UPSTREAM_VERSION := $(shell dpkg-parsechangelog -S Version|cut -d- -f1)
VENDOR_TARBALL = rust-hwlib_$(UPSTREAM_VERSION).orig-$(CARGO_VENDOR_DIR).tar.xz
# Enable debug symbols to avoid debug-file-with-no-debug-symbols warning
export CARGO_PROFILE_RELEASE_DEBUG=1

CARGO_TARGET_DIR = $(CURDIR)/target
NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
PKG_HWCTL = $(CURDIR)/debian/hwctl

%:
	dh $@ --buildsystem cargo

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

vendor-deps-cargo:
	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 PATH=$(PATH) cargo-vendor-filterer \
		--all-features \
		--tier 2 \
		--platform 'x86_64-unknown-linux-gnu' \
		--platform 'aarch64-unknown-linux-gnu' \
		--exclude-crate-path="*#test" \
		--exclude-crate-path="*#tests" \
		--exclude-crate-path="*#**/*.png" \
		--exclude-crate-path="*#test*data" \
		--exclude-crate-path="*#benches" \
		--exclude-crate-path="*#examples" \
		$(CARGO_VENDOR_DIR)

vendor-deps: vendor-deps-cargo
	# 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

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_test:
	/usr/share/cargo/bin/cargo test --offline

override_dh_auto_install:
	# Since dh-cargo doesn't support installing multiple packages in a
	# workspace, we install the CLI tool using the same logic that is
	# implemented in the dh-cargo install function. See
	# https://salsa.debian.org/rust-team/dh-cargo/-/blob/5cc7f7b8/cargo.pm#L168-195
	env RUST_BACKTRACE=1 \
		CARGO_TARGET_DIR=$(CARGO_TARGET_DIR) \
		DESTDIR=$(CURDIR)/debian/hwctl \
		/usr/bin/cargo -Zavoid-dev-deps install \
		--verbose \
		--verbose \
		-j$(NUMJOBS) \
		--target $(DEB_HOST_RUST_TYPE) \
		--config 'profile.release.lto = "thin"' \
		--no-track \
		--path hwctl \
		--root $(CURDIR)/debian/hwctl/usr
	# Since we use vendored Rust code, it's required by ubuntu-mir to ship
	# Cargo.lock file in the package at /usr/share/doc/<pkgname>/Cargo.lock
	install -D -m 644 Cargo.lock $(PKG_HWCTL)/usr/share/doc/hwctl/Cargo.lock
	/usr/share/cargo/bin/dh-cargo-built-using hwctl

	# Delete generated via `cargo install` hidden files
	rm -f $(PKG_HWCTL)/.crates.toml
	rm -f $(PKG_HWCTL)/.crates2.json

	# Install the AppArmor profile
	mkdir -p $(PKG_HWCTL)/etc/apparmor.d/
	cp debian/usr.bin.hwctl $(PKG_HWCTL)/etc/apparmor.d/usr.bin.hwctl
	dh_apparmor -phwctl --profile-name=usr.bin.hwctl
