#!/usr/bin/make -f
export DH_VERBOSE = 1
export RUST_BACKTRACE=full

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
include /usr/share/dpkg/vendor.mk

%:
	dh $@ --buildsystem cargo

execute_after_dh_auto_configure:
ifeq ($(DEB_VENDOR),Ubuntu)
	rm -rf debian/cargo_registry/*
	for d in vendor/*; do \
		ln -rs $$d debian/cargo_registry; \
	done
	CARGO_HOME=debian /usr/share/cargo/bin/cargo generate-lockfile --offline
	CARGO_VENDOR_DIR=$(CURDIR)/vendor /usr/share/cargo/bin/dh-cargo-vendored-sources
	rm -rf debian/.package-cache
else
	rm -rf vendor
endif

# dh_cargo install builds and installs the binary to /usr/bin
# the generated Makefile installs supplementary files
execute_before_dh_install:
	make install DESTDIR=debian/mdevctl

# move the binary to /usr/sbin
execute_after_dh_install:
	mkdir -p debian/mdevctl/usr/sbin
	mv debian/mdevctl/usr/bin/mdevctl debian/mdevctl/usr/sbin

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
ifeq ($(DEB_VENDOR),Ubuntu)
	CARGO_HOME=debian /usr/share/cargo/bin/cargo test --offline
else
	dh_auto_test -- test
endif
endif

execute_after_dh_auto_clean:
	rm -f mdevctl.spec Makefile
