#!/usr/bin/make -f

# see FEATURE AREAS in dpkg-buildflags(1)
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
include /usr/share/dpkg/buildflags.mk

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

export HOME := $(shell realpath .)

include /usr/share/dpkg/architecture.mk

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

ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
	# Cross compiling
	CONFIGURE_OPTS_QT5 += "CONFIG+=no_docs" "CONFIG+=cross_compile"
	CONFIGURE_OPTS_QT6 += # "CONFIG+=cross_compile"
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 ..
ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
	dh_auto_build --builddirectory=build-qt5 --buildsystem=qmake -- docs ..
endif

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	mkdir -p debian/tmp/home/run
	chmod +x tests/xvfb.sh
ifneq (,$(filter $(DEB_HOST_ARCH),$(testskip_architectures)))
	-env UITK_BUILD_ROOT=$(CURDIR)/build-qt6 tests/xvfb.sh dh_auto_test --no-parallel --builddirectory=build-qt6 --buildsystem=cmake
else
	-env 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)))
	-env UITK_BUILD_ROOT=$(CURDIR)/build-qt5 tests/xvfb.sh dh_auto_test --no-parallel --builddirectory=build-qt5 --buildsystem=qmake
else
	env UITK_BUILD_ROOT=$(CURDIR)/build-qt5 tests/xvfb.sh dh_auto_test  --no-parallel --builddirectory=build-qt5 --buildsystem=qmake
endif
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 ..

override_dh_install:
ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
	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
else
	# Add empty qmltypes files
	for module in "Components" "Components/Labs" "Components/Styles" "Layouts" "Metrics" "PerformanceMetrics"; \
	do \
		touch debian/tmp/`qmake -query QT_INSTALL_QML`/Lomiri/$$module/plugins.qmltypes; \
	done
endif
	rm -f debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/*.la

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

	# drop lomiri-ui-toolkit-autopilot files, not buildable on non-Ubuntu systems
	rm -f  debian/tmp/usr/lib/*/lomiri-ui-toolkit/apicheck
	rm -Rf debian/tmp/usr/lib/python3

	# 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/apicheck-qt6

	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 ..

	# 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 \
	       sync.profile \
	       ;

include /usr/share/dpkg/pkg-info.mk

PKD   = $(word 1,$(abspath $(dir $(MAKEFILE_LIST))))
PKG   = $(DEB_SOURCE)
UVER  = $(shell echo $(DEB_VERSION) | cut -d "-" -f1)
DTYPE = +dfsg
VER  ?= $(subst $(DTYPE),,$(UVER))

UURL = https://gitlab.com/ubports/development/core/lomiri-ui-toolkit.git
UREV = $(shell echo $(VER) | cut -d"." -f4)

## http://wiki.debian.org/onlyjob/get-orig-source
.PHONY: get-orig-source
get-orig-source: $(PKG)_$(VER)$(DTYPE).orig.tar.xz $(info I: $(PKG)_$(VER)$(DTYPE))
	@

$(PKG)_$(VER)$(DTYPE).orig.tar.xz: $(info I: GIT Revision=$(UREV))
	$(if $(wildcard $(PKG)-$(VER)),$(error $(PKG)-$(VER) exist, aborting..))
	@echo "# Downloading..."
	git clone $(UURL) $(PKG)-$(VER) \
	   || $(RM) -r $(PKG)-$(VER)
	cd $(PKG)-$(VER) \
	   && git checkout "$(UREV)" \
	   && ( echo "# Generating ChangeLog..." \
	      ; git --no-pager log --format="%ai %aN (%h) %n%n%x09*%w(68,0,10) %s%n" > ChangeLog \
	      ; touch -d "$$(git log -1 --format='%ci')" ChangeLog) \
	   && echo "# Setting times..." \
	   && for F in $$(git ls-tree -r --name-only HEAD); do touch --no-dereference -d "$$(git log -1 --format="%ai" -- $$F)" "$$F"; done \
	   && echo "# Cleaning-up..." \
	   && rm -rf documentation/autopilot-helpers/_build \
	   && rm -rf debian \
	   && rm -rf .git .git*
	@echo "# Packing..."
	find -L "$(PKG)-$(VER)" -xdev -type f -print | sort \
	   | XZ_OPT="-6v" tar -caf "../$(PKG)_$(VER)$(DTYPE).orig.tar.xz" -T- --owner=root --group=root --mode=a+rX \
	   && $(RM) -r "$(PKG)-$(VER)"
