#!/usr/bin/make -f

# see FEATURE AREAS in dpkg-buildflags(1)
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

export HOME := $(shell realpath .)

# Enforce -Werror for an upstream build.
CONFIGURE_OPTS_QT5 := # CONFIG+=warnings_are_errors
CONFIGURE_OPTS_QT6 :=

# Enable Ubuntu UITK compat layer for UBports build.
ifneq ($(filter qml-module-ubuntu-components,$(shell dh_listpackages)),)
	CONFIGURE_OPTS_QT5 += CONFIG+=ubuntu-uitk-compat
endif

ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
	# Cross compiling
	CONFIGURE_OPTS_QT5 += "CONFIG+=cross_compile"
	CONFIGURE_OPTS_QT6 += "CONFIG+=cross_compile"
endif

ifeq ($(filter %-doc,$(shell dh_listpackages)),)
	CONFIGURE_OPTS_QT5 += "CONFIG+=no_docs"
	CONFIGURE_OPTS_QT5 += "CONFIG+=no_docs"
endif

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

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure --builddirectory=build-qt6 --buildsystem=cmake -- \
		$(CONFIGURE_OPTS_QT6) \
		..
	dh_auto_configure --builddirectory=build-qt5 --buildsystem=qmake -- \
		CONFIG+=debian_build \
		$(CONFIGURE_OPTS_QT5) \
		QMAKE_CFLAGS="$(CFLAGS)" \
		QMAKE_CXXFLAGS="$(CXXFLAGS)" \
		QMAKE_LFLAGS="${LDFLAGS}" \
		..
	dh_auto_configure --sourcedirectory=tests/autopilot --buildsystem=pybuild

override_dh_auto_build:
	dh_auto_build --builddirectory=build-qt6 --buildsystem=cmake
	dh_auto_build --builddirectory=build-qt5 --buildsystem=qmake
ifneq ($(filter %-doc,$(shell dh_listpackages)),)
	dh_auto_build --builddirectory=build-qt5 --buildsystem=qmake -- docs
endif
ifneq ($(filter %-autopilot,$(shell dh_listpackages)),)
	dh_auto_build --sourcedirectory=tests/autopilot --buildsystem=pybuild
endif

override_dh_auto_test:
	mkdir -p debian/tmp/home/run
	chmod +x tests/xvfb.sh
ifneq (,$(filter $(DEB_HOST_ARCH),$(testskip_architectures)))
	-UITK_BUILD_ROOT=$(CURDIR)/build-qt6 tests/xvfb.sh dh_auto_test --no-parallel --builddirectory=build-qt6 --buildsystem=cmake
else
	-UITK_BUILD_ROOT=$(CURDIR)/build-qt6 tests/xvfb.sh dh_auto_test  --no-parallel --builddirectory=build-qt6 --buildsystem=cmake
endif
ifneq (,$(filter $(DEB_HOST_ARCH),$(testskip_architectures)))
	-UITK_BUILD_ROOT=$(CURDIR)/build-qt5 tests/xvfb.sh dh_auto_test --no-parallel --builddirectory=build-qt5 --buildsystem=qmake
else
	UITK_BUILD_ROOT=$(CURDIR)/build-qt5 tests/xvfb.sh dh_auto_test  --no-parallel --builddirectory=build-qt5 --buildsystem=qmake
endif

override_dh_auto_install:
	dh_auto_install --no-parallel --builddirectory=build-qt6 --buildsystem=cmake
	dh_auto_install --no-parallel --builddirectory=build-qt5 --buildsystem=qmake
ifneq ($(filter %-autopilot,$(shell dh_listpackages)),)
	dh_auto_install --no-parallel --sourcedirectory=tests/autopilot --buildsystem=pybuild
else
	rm -f  debian/tmp/usr/lib/*/lomiri-ui-toolkit/apicheck
	rm -Rf debian/tmp/usr/lib/python3
endif

override_dh_install:
ifneq ($(filter %-doc,$(shell dh_listpackages)),)
	mkdir -p debian/tmp/`qmake -query QT_INSTALL_DOCS`/qch
	mkdir -p debian/tmp/usr/share/lomiri-ui-toolkit/doc
	cp -r $(CURDIR)/build-qt5/documentation/*qch debian/tmp/`qmake -query QT_INSTALL_DOCS`/qch
endif
ifneq ($(filter qml-module-ubuntu-components,$(shell dh_listpackages)),)
	for module in "Components" "Components/Labs" "Components/Styles" "Metrics"; \
	do \
		touch debian/tmp/usr/lib/${DEB_HOST_MULTIARCH}/qt5/qml/Ubuntu/$$module/plugins.qmltypes; \
	done
endif
	for module in "Components" "Components/Labs" "Components/Styles" "Metrics"; \
	do \
		touch debian/tmp/usr/lib/${DEB_HOST_MULTIARCH}/qt5/qml/Lomiri/$$module/plugins.qmltypes; \
	done
	rm -f debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/*.la

	# FIXME: Due to autopilot not being in the archive we ship docs for now
	# sphinx-build -b singlehtml documentation/autopilot-helpers documentation/autopilot-helpers/_build/html
	# sphinx-build -b json documentation/autopilot-helpers documentation/autopilot-helpers/_build/json

	# Drop QML types used to test apicheck only
	rm -f debian/tmp/usr/lib/*/qt*/qml/Extinct/Animals/*

	# drop references to the build chroot
	sed -i debian/tmp/usr/lib/*/*.prl -e "/^QMAKE_PRL_BUILD_DIR.*/d"

	# remove Qt 6 examples
	rm -rf debian/tmp/usr/lib/*/qt6/examples

	# remove Qt 6 apicheck for now
	rm -f debian/tmp/usr/libexec/lomiri-ui-toolkit/qt6/apicheck

	dh_install

ifeq ($(shell dpkg-vendor --derives-from Ubuntu && echo yes),yes)
SUBST_VAR := -Vfonts-ubuntu=fonts-ubuntu -Vfonts-noto=
else
SUBST_VAR := -Vfonts-noto=fonts-noto-core,fonts-noto-mono -Vfonts-ubuntu=
endif

override_dh_gencontrol:
	dh_gencontrol -- $(SUBST_VAR)

override_dh_auto_clean:
	-dh_auto_clean --builddirectory=build-qt6 --buildsystem=cmake
	-dh_auto_clean --builddirectory=build-qt5 --buildsystem=qmake
	-dh_auto_clean --sourcedirectory=tests/autopilot --buildsystem=pybuild

	# Qt / qmake creates <FolderName>.version files which don't
	# get clean-up properly by 'make distclean'
	-find . -name '*.version' -delete

	# Delete additional files
	rm -rvf \
	        .cache/ \
	        .config/ \
	        .local/ \
	        .pybuild/ \
	        po/*.mo \
	        examples/lomiri-ui-toolkit-gallery/po/*.mo \
	        xvfb.err \
	        ;

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