#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# Derive version from debian/changelog so the binary always matches the package.
# APP_VERSION_OVERRIDE in the environment takes precedence (e.g. manual rebuilds).
DEB_PKG_VERSION := $(shell dpkg-parsechangelog -SVersion | cut -d- -f1)
_APP_VERSION := $(or $(APP_VERSION_OVERRIDE),$(DEB_PKG_VERSION))

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure -- \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DAPP_VERSION_OVERRIDE=$(_APP_VERSION)

# Buildd chroots (Launchpad/sbuild) set HOME to a deliberately
# non-writable path to catch builds that wrongly depend on it. Several
# Qt Test suites legitimately need a writable $HOME during the test run
# (QStandardPaths test mode, deny-list QTemporaryDir fixtures), so give
# them one scoped to the build tree instead of the chroot's HOME.

ifneq (,$(filter $(DEB_HOST_ARCH), amd64 arm64))
override_dh_auto_test:
	builddir="$$(ls -d obj-*)"; \
	mkdir -p "$$builddir/dh-test-home"; \
	xvfb-run -a ctest --test-dir obj-* --output-on-failure \
	-E "LifecycleDenyListTests|LifecycleAuditLogTests|ScreenshotTests"
else
override_dh_auto_test:
	builddir="$$(ls -d obj-*)"; \
	mkdir -p "$$builddir/dh-test-home"; \
	xvfb-run -a ctest --test-dir obj-* --output-on-failure \
	-E "LifecycleDenyListTests|LifecycleAuditLogTests|ScreenshotTests|DockerToolTests"
endif

# No dbgsym packages:
override_dh_strip:
	@echo "NO--THANKS ANYWAY--but not building dbgsym packages for MX Linux"
	dh_strip --no-automatic-dbgsym
