#!/usr/bin/make -f
# -*- makefile -*-

NULL =

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

include /usr/share/dpkg/architecture.mk

# see FEATURE AREAS in dpkg-buildflags(1)
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
include /usr/share/dpkg/buildflags.mk

# UBports-specific:
#
# The online-accounts-client/client/service/tst_libaccounts_service/ unit
# tests fails on Ubuntu noble. Skip this for now.
#
ifeq ($(shell dpkg-vendor --derives-from Ubuntu && echo yes),yes)
        CONFIGURE_OPTS += -DDISABLE_TESTS_THAT_FAIL_ON_UBUNTU_NOBLE=ON
        CONFIGURE_OPTS += -DENABLE_MIRCLIENT=ON
endif

# required for building against Debian unstable on UBports CI
UBUNTU_COMPAT_OPT = -DENABLE_UBUNTU_COMPAT=ON

# Skip tests on the archs they are known to be flaky with current configuration
# See https://launchpad.net/bugs/1630906
testskip_architectures :=

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure --builddirectory=build-qt5 --buildsystem=cmake .. -- \
	    -DENABLE_QT6=OFF \
	    -DBUILD_TESTING=ON \
	    $(UBUNTU_COMPAT_OPT) \
	    $(CONFIGURE_OPTS) \
	    $(NULL)
	dh_auto_configure --builddirectory=build-qt6 --buildsystem=cmake .. -- \
	    -DENABLE_QT6=ON \
	    -DBUILD_TESTING=ON \
	    $(CONFIGURE_OPTS) \
	    $(NULL)

override_dh_auto_build:
	dh_auto_build --builddirectory=build-qt6 --buildsystem=cmake ..
	dh_auto_build --builddirectory=build-qt5 --buildsystem=cmake ..

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	mkdir -p debian/home
ifneq (,$(filter $(DEB_HOST_ARCH),$(testskip_architectures)))
	-dh_auto_test --builddirectory=build-qt5 --buildsystem=cmake --no-parallel ..
	-dh_auto_test --builddirectory=build-qt6 --buildsystem=cmake --no-parallel ..
else
	dh_auto_test --builddirectory=build-qt5 --buildsystem=cmake --no-parallel ..
	rm -rf \
	    /tmp/accounts.db* \
	    /tmp/hooks-test \
	    /tmp/hooks-test2 \
	    /tmp/loa-runtime/ \
	    /tmp/tst_application_manager \
	    /tmp/tst_service_*
	dh_auto_test --builddirectory=build-qt6 --buildsystem=cmake --no-parallel ..
	rm -rf \
	    /tmp/accounts.db* \
	    /tmp/hooks-test \
	    /tmp/hooks-test2 \
	    /tmp/loa-runtime/ \
	    /tmp/tst_application_manager \
	    /tmp/tst_service_*
endif
endif

override_dh_install:
	# empty directories
	rmdir debian/tmp/usr/share/doc/qml-module-lomiri-onlineaccounts/html/images/
	rmdir debian/tmp/usr/share/doc/qml-module-lomiri-onlineaccounts-client/html/images/
	dh_install

override_dh_auto_install:
	dh_auto_install --builddirectory=build-qt6 --buildsystem=cmake ..
	dh_auto_install --builddirectory=build-qt5 --buildsystem=cmake ..

override_dh_auto_clean:
	dh_auto_clean --builddirectory=build-qt5 --buildsystem=cmake ..
	dh_auto_clean --builddirectory=build-qt6 --buildsystem=cmake ..

override_dh_clean:
	rm -Rf debian/home
	dh_clean

get-orig-source:
	uscan --noconf --force-download --rename --download-current-version --destdir=..
