#!/usr/bin/make -f

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

NULL =

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

export QT_SELECT := qt5

include /usr/share/dpkg/architecture.mk

CONFIG = tests ofono upower
ifeq ($(DEB_HOST_ARCH_OS),linux)
	CONFIG += udisks
endif

%:
	dh $@ --with pkgkde_symbolshelper

override_dh_auto_configure:
	dh_auto_configure -- CONFIG+="$(CONFIG)" QT_BUILD_PARTS+=tests

override_dh_auto_install:
	dh_auto_install

	# Fix wrong path in pkgconfig files
	find $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig -type f -name '*.pc' \
	-exec sed -i -e 's/$(DEB_HOST_MULTIARCH)\/$(DEB_HOST_MULTIARCH)/$(DEB_HOST_MULTIARCH)/g' {} \;

	# Remove libtool-like files
	rm -f debian/tmp/usr/lib/*/*.la

	# Remove superfluous qmake .pro file
	rm -f debian/tmp/usr/lib/*/qt5/examples/examples.pro

override_dh_missing:
	dh_missing --fail-missing

override_dh_auto_test:
	# Disable auto tests at build time
	:

override_dh_clean:
	rm -Rfv $(CURDIR)/debian/test-home/
	dh_clean

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://code.qt.io/qt/qtsystems.git
UREV = $(shell echo $(VER) | cut -d"." -f3)

QT_MODULE_VERSION = $(shell cat .qmake.conf | grep MODULE_VERSION | sed -re 's/MODULE_VERSION\s*=\s*([^\s]+)\s*/\1/')

## 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) doc/config/*/scripts/jquery.js \
	    && $(RM) -r .git .git* \
	    $(NULL)
	@echo "# Running syncqt.pl (QT_MODULE_VERSION=$(QT_MODULE_VERSION))"
	cd $(PKG)-$(VER) \
	    && cd src/systeminfo/ && perl /usr/lib/qt5/bin/syncqt.pl -module QtSystemInfo -version $(QT_MODULE_VERSION) -outdir $(CURDIR)/$(PKG)-$(VER) -builddir $(CURDIR)/$(PKG)-$(VER) $(CURDIR)/$(PKG)-$(VER) && cd - 1>/dev/null \
	    && cd src/publishsubscribe/ && perl /usr/lib/qt5/bin/syncqt.pl -module QtPublishSubscribe -version $(QT_MODULE_VERSION) -outdir $(CURDIR)/$(PKG)-$(VER) -builddir $(CURDIR)/$(PKG)-$(VER) $(CURDIR)/$(PKG)-$(VER) && cd - 1>/dev/null \
	    && cd src/serviceframework/ && perl /usr/lib/qt5/bin/syncqt.pl -module QtServiceFramework -version $(QT_MODULE_VERSION) -outdir $(CURDIR)/$(PKG)-$(VER) -builddir $(CURDIR)/$(PKG)-$(VER) $(CURDIR)/$(PKG)-$(VER) && cd - 1>/dev/null \
	    $(NULL)
	@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)" \
	    $(NULL)
