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

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

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

export CFLAGS := $(shell dpkg-buildflags --get CFLAGS) $(shell dpkg-buildflags --get CPPFLAGS)
export CXXFLAGS := $(shell dpkg-buildflags --get CXXFLAGS) $(shell dpkg-buildflags --get CPPFLAGS)
export LDFLAGS := $(shell dpkg-buildflags --get LDFLAGS) -Wl,--as-needed
export QT_SELECT=qt5

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

ifneq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	CONFIGURE_OPTS += "CONFIG+=no_tests"
endif

%:
	dh $@ --with python3

override_dh_missing:
	dh_missing --fail-missing

override_dh_auto_configure:
	dh_auto_configure -- \
		LIBDIR=/usr/lib/$(DEB_HOST_MULTIARCH) \
		DEB_HOST_MULTIARCH=$(DEB_HOST_MULTIARCH) \
		"QMAKE_CXXFLAGS=$(CFLAGS)" \
		CONFIG+=enable-mir \
		$(CONFIGURE_OPTS) \
		lomiri-system-settings-online-accounts.pro

override_dh_auto_install:
	dh_auto_install
	rm -rf debian/*/usr/lib/*/libonline-accounts-plugin.so
	rm -rf debian/*/usr/share/accounts/qml-plugins/example
	rm -rf debian/*/usr/share/accounts/providers/example.provider
	rm -rf debian/*/usr/tests

	# Create the compat module for Ubuntu.OnlineAccounts.Client
	mkdir -p debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/qt5/qml/Ubuntu/OnlineAccounts/Client
	for FILE in qmldir; do \
		sed \
			-e 's/Lomiri/Ubuntu/' \
			debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/qt5/qml/Lomiri/OnlineAccounts/Client/$${FILE} \
			> debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/qt5/qml/Ubuntu/OnlineAccounts/Client/$${FILE}; \
	done
	ln -sr debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/qt5/qml/Lomiri/OnlineAccounts/Client/libOnlineAccountsClient.so \
		debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/qt5/qml/Ubuntu/OnlineAccounts/Client/libOnlineAccountsClient.so

	# Create the compat module for Ubuntu.OnlineAccounts.Plugin
	P=debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/lomiri-system-settings/private/ ;\
	mkdir -p $${P}/Ubuntu/OnlineAccounts/Plugin ;\
	cp plugins.qmldir $${P}/Ubuntu/OnlineAccounts/Plugin/qmldir ;\
	ln -sr $${P}/Lomiri/OnlineAccounts/Plugin/libOnlineAccountsPlugin.so \
		$${P}/Ubuntu/OnlineAccounts/Plugin/libOnlineAccountsPlugin.so

override_dh_auto_test-arch:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
ifneq (,$(filter $(DEB_HOST_ARCH),$(testskip_architectures)))
	-dh_auto_test --max-parallel=1
else
	dh_auto_test --max-parallel=1
endif
endif

override_dh_auto_test-indep:
