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

include /usr/share/dpkg/default.mk

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
export DPKG_GENSYMBOLS_CHECK_LEVEL=4
export NO_PNG_PKG_MANGLE=1
export QT_SELECT=qt5

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 armhf powerpc ppc64el s390x

ifeq ($(shell dpkg-vendor --derives-from Ubuntu && echo yes),yes)
	CONFIGURE_OPTS += -DENABLE_UBUNTU_ACCOUNTSSERVICE=ON
endif

ifneq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	CONFIGURE_OPTS += "-DNO_TESTS=ON"
endif

%:
	dh $@ --parallel --fail-missing --with python3,migrations

override_dh_auto_configure:
	# Debian defines CMAKE_INSTALL_LOCALSTATEDIR as /usr/var, which is wrong.
	# So until Debian bug 719148 is fixed, do it ourselves.
		dh_auto_configure -- \
		-DCMAKE_INSTALL_LOCALSTATEDIR="/var" \
		-DDISPLAYED_DISTRO_NAME="Ubuntu Touch" \
		$(CONFIGURE_OPTS)

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.
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
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
endif

override_dh_install:
ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
	# install autopilot tests
	cd tests/autopilot; \
	set -ex; for python in $(shell py3versions -r); do \
	    $$python setup.py install --root=$(CURDIR)/debian/tmp --install-layout=deb; \
	done; \
	cd $(CURDIR)
else
	rm -f debian/tmp/usr/share/lomiri/unlock-device
	rm -rf debian/tmp/usr/lib/*/lomiri/qml/mocks
endif
	dh_install -X'*.pyc' --fail-missing

# use private lib directories
override_dh_makeshlibs:
	dh_makeshlibs -Nlomiri-private -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 -Llomiri-private
