#!/usr/bin/make -f
# -*- makefile -*-
# See debhelper(7) (uncomment to enable)
# output every command that modifies files on the build system.
#export DH_VERBOSE = 1
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

FLAVORS = cli gui
CONFIG = -Djack=enabled -Dqtversion=6
CONFIG_cli = -Dnogui=true -Drtaudio=disabled
CONFIG_gui = -Dnogui=false -Drtaudio=enabled

ifeq ($(strip $(filter $(DEB_HOST_ARCH), amd64 arm64 armhf i386)),)
CONFIG += -Dnovs=true
else
QT_DEPENDS = \
	qml6-module-qt5compat-graphicaleffects, \
	qml6-module-qtqml-workerscript, \
	qml6-module-qtquick-controls, \
	qml6-module-qtquick-layouts, \
	qml6-module-qtquick-templates, \
	qml6-module-qtquick-window, \
	qml6-module-qtwebchannel, \
	qml6-module-qtwebengine
endif



builddir=debian/build/flavor-

export QT_SELECT=qt6
DEB_SRCDIR = .

%:
	dh $@ --sourcedirectory=$(DEB_SRCDIR) --buildsystem=meson

#.PHONY: $(patsubst %,configure_%,$(FLAVORS))
override_dh_auto_configure-arch: $(patsubst %,configure_%,$(FLAVORS))
configure_%:
	dh_auto_configure -a -B $(builddir)$* -- $(strip $(CONFIG) $(CONFIG_$*))

#.PHONY: $(patsubst %,build_%,$(FLAVORS))
override_dh_auto_build-arch: $(patsubst %,build_%,$(FLAVORS))
build_%:
	dh_auto_build -a -B $(builddir)$*


#.PHONY: $(patsubst %,test_%,$(FLAVORS))
override_dh_auto_test-arch: $(patsubst %,test_%,$(FLAVORS))
test_%:
	dh_auto_test -a -B $(builddir)$*

#.PHONY: $(patsubst %,install_%,$(FLAVORS))
override_dh_auto_install-arch: $(patsubst %,install_%,$(FLAVORS))
install_%:
	dh_auto_install -a -B $(builddir)$*
	-mv debian/tmp/usr/bin/jacktrip debian/tmp/usr/bin/jacktrip-$*
	# dummy manpages (for cross-compiling)
	mkdir -p debian/tmp/usr/share/man/man1/
	test -e debian/tmp/usr/share/man/man1/jacktrip.1 || cp debian/jacktrip.1 debian/tmp/usr/share/man/man1/jacktrip.1
	# flavoured manpages
	-mv debian/tmp/usr/share/man/man1/jacktrip.1 debian/tmp/usr/share/man/man1/jacktrip-$*.1
	-mv debian/tmp/usr/share/man/man1/jacktrip.1.gz debian/tmp/usr/share/man/man1/jacktrip-$*.1.gz
	-find debian/tmp -type f -name "*.desktop" -exec sed -e "s|^\(Exec=jacktrip\)$$|\1-$*|" -i {} ";"

override_dh_installchangelogs:
	dh_installchangelogs docs/changelog.yml

override_dh_gencontrol:
	dh_gencontrol -- \
		-Vqt:Depends="$(QT_DEPENDS)"



DEB_COPYRIGHT_CHECK_IGNORE_REGEX = \
        debian/.*|documentation/img/.*|docs/images/.*\.png|src/vs/.*\.png|src/gui/.*\.png|src/gui/alt/Jacktrip\.ai|macos/jacktrip\.iconset/.*\.png|macos/JackTrip\.app_template/Contents/Resources/jacktrip\.icns|macos/jacktrip_alt\.icns
# licensecheck v1
.PHONY: licensecheck
licensecheck:
	LANG=C.UTF-8 licensecheck \
		-i "^($(DEB_COPYRIGHT_CHECK_IGNORE_REGEX))$$" \
		--check '.*' --recursive --deb-machine --lines 0 * \
		> debian/copyright_newhints
	cmp debian/copyright_hints debian/copyright_newhints \
		&& rm debian/copyright_newhints
