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

export DPKG_GENSYMBOLS_CHECK_LEVEL=4
export LOMIRI_CONTENT_HUB_TESTING=1
export DEB_BUILD_MAINT_OPTIONS=hardening=+all
include /usr/share/dpkg/buildflags.mk

PROJECT_DH_OPTIONS = --with click

# Disable coverage reporting, enable locally for introspecting test coverage
CONFIGURE_OPTS += -DENABLE_COVERAGE=OFF

ifneq (,$(findstring nocheck,$(DEB_BUILD_PROFILES)))
  CONFIGURE_OPTS += -DENABLE_TESTS=OFF
  DEB_BUILD_PROFILES += noinsttest
endif

ifneq (,$(findstring nodoc,$(DEB_BUILD_OPTIONS)))
  CONFIGURE_OPTS += -DENABLE_DOC=OFF
endif

# Skip tests on architectures where tests are being flaky, timeout, etc.
TESTSKIP_ARCHITECTURES_QT6 := armhf
TESTSKIP_ARCHITECTURES_QT5 :=

%:
	dh $@ $(PROJECT_DH_OPTIONS)

override_dh_auto_configure:
	dh_auto_configure --builddirectory=build-qt6 --buildsystem=cmake -- $(CONFIGURE_OPTS) -DENABLE_QT6=ON  -DENABLE_DOC=OFF ..
	dh_auto_configure --builddirectory=build-qt5 --buildsystem=cmake -- $(CONFIGURE_OPTS) -DENABLE_QT6=OFF                  ..

override_dh_auto_build:
	dh_auto_build --builddirectory=build-qt6 --buildsystem=cmake ..
	dh_auto_build --builddirectory=build-qt5 --buildsystem=cmake ..

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
ifeq (,$(filter $(DEB_HOST_ARCH),$(TESTSKIP_ARCHITECTURES_QT6)))
	dh_auto_test --builddirectory=build-qt6 --buildsystem=cmake --no-parallel ..
endif
ifeq (,$(filter $(DEB_HOST_ARCH),$(TESTSKIP_ARCHITECTURES_QT5)))
	dh_auto_test --builddirectory=build-qt5 --buildsystem=cmake --no-parallel ..
endif
endif

override_dh_auto_install:
ifneq (,$(findstring nocheck,$(DEB_BUILD_PROFILES)))
	mkdir -p debian/lomiri-content-hub-testability
endif
	dh_auto_install --builddirectory=build-qt6 --buildsystem=cmake ..
	dh_auto_install --builddirectory=build-qt5 --buildsystem=cmake ..
	dh_apparmor -plomiri-content-hub-testability --profile-name=lomiri-content-hub-testability
	mkdir -p debian/tmp/var/cache/lomiri-content-hub/peers

override_dh_install:
	rmdir debian/tmp/usr/share/doc/lomiri-content-hub/qml/html/images/
	cd debian/tmp/usr/share/doc/lomiri-content-hub/cpp/html/ && rdfind -makesymlinks true -makeresultsfile false .
	cd debian/tmp/usr/share/doc/lomiri-content-hub/cpp/html/ && symlinks -rc .
	cd debian/tmp/usr/share/doc/lomiri-content-hub/qml/html/ && rdfind -makesymlinks true -makeresultsfile false .
	cd debian/tmp/usr/share/doc/lomiri-content-hub/qml/html/ && symlinks -rc .
	dh_install

override_dh_missing:
	dh_missing --fail-missing

override_dh_auto_clean:
	dh_auto_clean --builddirectory=build-qt6 --buildsystem=cmake ..
	dh_auto_clean --builddirectory=build-qt5 --buildsystem=cmake ..

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 =
VER  ?= $(subst $(DTYPE),,$(UVER))

UURL = https://gitlab.com/ubports/development/core/lomiri-content-hub.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 debian \
	   && rm -rf src/Ubuntu \
	   && rm -rf test/autopilot \
	   && 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)"
