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

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

%:
	dh $@

override_dh_auto_clean:
	-dh_auto_clean
	-rm -f ./config.mk ./tests/*.o

override_dh_auto_configure:
	sed -e 's/^\s*ARCH\s*=\s.*/ARCH = $(DEB_BUILD_GNU_CPU)/' ./configs/linux.mk > ./config.mk || ($(RM) ./config.mk; false)

override_dh_auto_build:
	dh_auto_build -- VERSION=$(DEB_VERSION)

override_dh_auto_install:
	dh_auto_install -- PREFIX=/usr

override_dh_auto_test:
# On x86_64, run tests only on amd64 (linux), as this is the only linux x86_64
# target supported by upstream.
ifeq (amd64,$(DEB_TARGET_ARCH))
	dh_auto_test
endif
# On non x86_64 archs, run tests only on targets supported by upstream
# (e.g. arm64, riscv64).
ifneq (x86_64,$(DEB_BUILD_GNU_CPU))
	if ! (.bin/harec -a $(DEB_BUILD_GNU_CPU) 2>&1 || true) | grep 'Unsupported or unrecognized target:'; then dh_auto_test; fi
endif
