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

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

DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH)
# Skip tests on the archs they are known to be broken with current configuration
# Ref.: https://bugs.launchpad.net/ubuntu/+source/qtdeclarative-opensource-src/+bug/1606927
testskip_architectures := powerpc

include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/architecture.mk

ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
export DEB_BUILD_PROFILES := cross
endif

CONFIGURE_FLAGS := -DENABLE_LIBDEVICEINFO=ON

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

ifneq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
export CONFIGURE_FLAGS += -DENABLE_TESTS=OFF
endif

export HOME=$(CURDIR)/debian/build

override_dh_auto_configure:
	dh_auto_configure -- \
		-DLOMIRI_KEYBOARD_PLUGIN_PATH="/usr/lib/lomiri-keyboard/plugins" \
		$(CONFIGURE_FLAGS)

override_dh_auto_clean:
	dh_auto_clean
	rm -rf $(BUILDHOME)

override_dh_auto_test:
	mkdir -p $(HOME)
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
ifneq (,$(filter $(DEB_HOST_ARCH),$(testskip_architectures)))
	-dh_auto_test
else
	dh_auto_test
endif
endif

override_dh_install:
	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)
	dh_install -X'*.pyc'

override_dh_makeshlibs:
	dh_makeshlibs -Xusr/lib/$(DEB_HOST_MULTIARCH)/ubuntu-system-settings

override_dh_missing:
	dh_missing --fail-missing

ifeq ($(shell dpkg-vendor --derives-from Ubuntu && echo yes),yes)
SUBST_VAR := "-Vaccountsservice-ubuntu-schemas=accountsservice-ubuntu-schemas (>= 0.0.7)"
else
SUBST_VAR := -Vaccountsservice-ubuntu-schemas=
endif

override_dh_gencontrol:
	dh_gencontrol -- $(SUBST_VAR)

# Add migrations add-on when build on Ubuntu where the original name is available.
DH_MIGRATIONS = $(shell dpkg-vendor --derives-from Ubuntu && echo "--with migrations")

%:
	dh $@ --with python3 $(DH_MIGRATIONS)
