#!/usr/bin/make -f
include /usr/share/dpkg/architecture.mk

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

# perf-event-open-sys is only packaged on the 64-bit archs upstream enables it
# for, but cargo needs every dependency available to resolve, even
# target-gated ones: drop it from Cargo.toml elsewhere.
PERF_EVENT_ARCHS := amd64 arm64 loong64 ppc64el riscv64
ifeq (,$(filter $(DEB_HOST_ARCH),$(PERF_EVENT_ARCHS)))
execute_before_dh_auto_configure-arch:
	cp Cargo.toml debian/Cargo.toml.perf-event
	sed -i '/dependencies\.perf-event-open-sys\]$$/,/^$$/d' Cargo.toml
	! grep -q perf-event-open-sys Cargo.toml
endif

execute_after_dh_auto_clean:
	[ ! -f debian/Cargo.toml.perf-event ] || mv debian/Cargo.toml.perf-event Cargo.toml

# The hyperfine-scripts package is Architecture: all and contains no Rust code,
# so a binary-indep-only build must not run the cargo buildsystem at all
# (dh_auto_configure would fail: no Cargo lib or bin package to build).
override_dh_auto_configure-indep:
override_dh_auto_build-indep:
override_dh_auto_test-indep:
override_dh_auto_install-indep:

override_dh_auto_test-arch:
	dh_auto_test -- test --all || true

override_dh_installman-arch:
	help2man \
		--name hyperfine \
		--no-info \
		debian/hyperfine/usr/bin/hyperfine > debian/hyperfine.1
	dh_installman -O--buildsystem=cargo

override_dh_dwz:
	# Don't do anything. fails because of the
	# https://github.com/rust-lang/rust/issues/66118

override_dh_auto_install-arch:
	dh_auto_install --destdir=debian/hyperfine

override_dh_install:
	dh_install
	# The scripts import the plot_utils module from their own directory, so
	# ship them together and symlink the executables into /usr/bin.
	install -D -m 644 scripts/plot_utils.py debian/hyperfine-scripts/usr/share/hyperfine-scripts/plot_utils.py
	set -e; for f in scripts/*.py; do \
		b=$$(basename $$f); \
		[ $$b != plot_utils.py ] || continue; \
		n=$$(basename $$f .py | tr '_' '-'); \
		install -D -m 755 $$f debian/hyperfine-scripts/usr/share/hyperfine-scripts/$$b; \
		mkdir -p debian/hyperfine-scripts/usr/bin; \
		ln -s ../share/hyperfine-scripts/$$b debian/hyperfine-scripts/usr/bin/hyperfine-$$n; \
	done
