#!/usr/bin/make -f
export QT_SELECT=6

# MX Linux releases are named after their base Debian codename (e.g. MX 25 is
# based on Trixie); OBS builds the same source for multiple Debian repos, so
# append that release suffix on Trixie builds only. Only the binary package
# version carries the suffix: dpkg-source, dpkg-genbuildinfo and
# dpkg-genchanges all derive filenames from debian/changelog, so rewriting it
# mid-build leaves them disagreeing about the source version and the build
# fails looking for a .dsc that was never produced under the suffixed name.
DEB_CODENAME := $(shell sed -n 's/^VERSION_CODENAME=//p' /etc/os-release 2>/dev/null)
ifeq ($(DEB_CODENAME),trixie)
DEB_VERSION := $(shell dpkg-parsechangelog -SVersion)
ifeq ($(filter %mx25,$(DEB_VERSION)),)
BINARY_VERSION := $(DEB_VERSION)mx25
DH_GENCONTROL_ARGS := -- -v$(BINARY_VERSION)
# Keep the version shown in the application in step with the package version.
CMAKE_VERSION_ARG := -DPROJECT_VERSION_OVERRIDE=$(BINARY_VERSION)
endif
endif

override_dh_auto_configure:
	dh_auto_configure -- \
		-G Ninja \
		$(CMAKE_VERSION_ARG) \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_EXPORT_COMPILE_COMMANDS=ON

override_dh_auto_clean:
	dh_auto_clean
	rm -rf build/
	rm -f translations/*.qm

override_dh_shlibdeps:
	dh_shlibdeps --dpkg-shlibdeps-params=--ignore-missing-info

# Do not create package-private .dwz debug data.
override_dh_dwz:
	@:

override_dh_auto_install:
	# Install is handled by debian/install file
	dh_auto_install

override_dh_gencontrol:
	# Mirror CMakeLists.txt: Settings moved from Qt.labs.settings into QtCore
	# in Qt 6.5, so the package must depend on whichever module the build
	# actually embedded into Main.qml.
	QT6_VERSION=$$(dpkg-query -W -f='$${Version}' qt6-base-dev); \
	if dpkg --compare-versions "$$QT6_VERSION" ge 6.5; then \
		SETTINGS_DEP=qml6-module-qtcore; \
	else \
		SETTINGS_DEP=qml6-module-qt-labs-settings; \
	fi; \
	echo "qml:SettingsDepends=$$SETTINGS_DEP" >> debian/mx-tools.substvars; \
	dh_gencontrol $(DH_GENCONTROL_ARGS)

%:
	dh $@ --buildsystem=cmake+ninja --no-automatic-dbgsym
