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

include /usr/share/dpkg/default.mk

export NO_PNG_PKG_MANGLE=1
export QT_SELECT=qt5

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

# Skip tests on the archs they are known to be flaky  with current configuration
testskip_architectures := arm64 powerpc ppc64el s390x

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure -- \
		-DENABLE_UBUNTU_ACCOUNTSSERVICE=$(if $(shell dpkg-vendor --derives-from Ubuntu && echo y),ON,OFF) \
		-DWITH_MIR2=ON \
		-DNO_TESTS=$(if $(filter nocheck,$(DEB_BUILD_OPTIONS)),ON,OFF)

override_dh_auto_build:
	# doc is not a default target
	dh_auto_build -- -O all doc

override_dh_auto_test:
	# Run the entire test battery
	# Parallel tests have a risk of starting xvfb on the same DISPLAY multiple times,
	# causing the whole battery to fail.
ifneq (,$(filter $(DEB_HOST_ARCH),$(testskip_architectures)))
	-dh_auto_test --no-parallel -- -O -k xvfballtests
else
	dh_auto_test --no-parallel -- -O -k xvfballtests
endif

execute_after_dh_fixperms:
	[ -d debian/lomiri-tests ] && chmod a-x debian/lomiri-tests/usr/libexec/lomiri/tests/plugins/Lomiri/Session/libSessionBackendTestPreload.so || true
	[ -d debian/lomiri-tests ] && chmod a-x debian/lomiri-tests/usr/libexec/lomiri/tests/plugins/Lomiri/Launcher/applications/abs-icon.desktop || true
	[ -d debian/lomiri-tests ] && chmod a-x debian/lomiri-tests/usr/libexec/lomiri/tests/plugins/Lomiri/Launcher/applications/click-icon.desktop || true
	[ -d debian/lomiri-tests ] && chmod a-x debian/lomiri-tests/usr/libexec/lomiri/tests/plugins/Lomiri/Launcher/applications/click-icon.svg || true
	[ -d debian/lomiri-tests ] && chmod a-x debian/lomiri-tests/usr/libexec/lomiri/tests/plugins/Lomiri/Launcher/applications/no-name.desktop || true
	[ -d debian/lomiri-tests ] && chmod a-x debian/lomiri-tests/usr/libexec/lomiri/tests/plugins/Lomiri/Launcher/applications/rel-icon.desktop || true
	[ -d debian/lomiri-tests ] && chmod a-x debian/lomiri-tests/usr/libexec/lomiri/tests/plugins/Lomiri/Launcher/applications/rel-icon.svg || true

execute_before_dh_install:
	rm debian/tmp/usr/bin/indicators-client
	rm debian/tmp/usr/share/applications/indicators-client.desktop
	rm debian/tmp/usr/share/lomiri/unlock-device
	rm debian/tmp/usr/share/lomiri/Wizard/Pages/*-update.qml*
	rm debian/tmp/usr/lib/systemd/user/*.service
	cd debian/tmp/usr/share/doc/lomiri/html/ && rdfind -makesymlinks true .
	cd debian/tmp/usr/share/doc/lomiri/html/ && symlinks -rc .
ifeq ($(shell dpkg-vendor --derives-from Ubuntu && echo yes),yes)
	cp debian/home-ubuntu.svg debian/tmp/usr/share/lomiri/Launcher/graphics/home.svg
else
	cp debian/home-lomiri.svg debian/tmp/usr/share/lomiri/Launcher/graphics/home.svg
endif

execute_after_dh_installdocs:
	# Remove results.txt for reproducible builds
	rm -vf debian/lomiri-doc/usr/share/doc/lomiri/html/results.txt

# use private lib directories
override_dh_makeshlibs:
	dh_makeshlibs -Nlomiri-tests

override_dh_shlibdeps:
	# Some mock libraries link against liblightdm-qt5-3.so which we want to
	# avoid, since we only really link against our mock one, not the system one.
	dh_shlibdeps -XlibMockAccountsService-qml.so -Lliblomiri-private0

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