#!/usr/bin/make -f

include /usr/share/dpkg/default.mk
include /usr/share/rustc/architecture.mk
export CFLAGS CXXFLAGS CPPFLAGS LDFLAGS
export DEB_HOST_RUST_TYPE DEB_HOST_GNU_TYPE
export PATH := /usr/share/cargo/bin:$(PATH)
export CARGO=/usr/share/cargo/bin/cargo
export CARGO_HOME=$(CURDIR)/debian/cargo_home
export CARGO_REGISTRY=$(CURDIR)/debian/cargo_registry
export DEB_CARGO_CRATE=$(DEB_SOURCE)_$(DEB_VERSION_UPSTREAM)
export DEB_BUILD_MAINT_OPTIONS=hardening=+bindnow

%:
	dh $@ --buildsystem=cargo --with shell_completions

override_dh_auto_clean:
	dh_auto_clean
	rm -rf debian/cargo_registry
	rm -f debian/cargo-checksum.json

override_dh_auto_configure:
	$(CARGO) prepare-debian $(CARGO_REGISTRY) --link-from-system
	rm -f Cargo.lock
	echo '{"package":"Could not get crate checksum","files":{}}' > debian/cargo-checksum.json
	$(CARGO) build --release --no-default-features --features cli -p skim

# Unfortunately we need to hack around the installation like this
override_dh_auto_install:
	dh_auto_install || true
	mkdir -p $(CURDIR)/debian/skim/usr/bin
	cp $(CURDIR)/target/$(DEB_HOST_RUST_TYPE)/release/sk $(CURDIR)/debian/skim/usr/bin/sk

execute_after_dh_auto_install:
	/usr/share/cargo/bin/dh-cargo-built-using skim
	rm -f debian/skim/usr/.crates.toml debian/skim/usr/.crates2.json

execute_after_dh_fixperms:
	find debian/ -path "*/.githooks/*" -type f -exec chmod -x {} +

override_dh_auto_test:

override_dh_strip:
	dh_strip --no-automatic-dbgsym

override_dh_dwz:

# skip these targets on arch:all builds
override_dh_auto_test-indep:
override_dh_auto_build-indep:
override_dh_auto_install-indep:
	dh_install
