#!/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)

override_dh_auto_test:
	# 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.
	builddir="$$(ls -d obj-*)"; \
	mkdir -p "$$builddir/dh-test-home"; \
	HOME="$(CURDIR)/$$builddir/dh-test-home" xvfb-run -a ctest --test-dir "$$builddir" --output-on-failure -E ScreenshotTests
