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

# Uncomment this to be more picky with non-updated .symbols files
#export DPKG_GENSYMBOLS_CHECK_LEVEL=4

include /usr/share/dpkg/default.mk

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

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

%:
	dh $@

override_dh_missing:
	dh_missing --fail-missing

FLAGS := -DWITH_CONTENTHUB=ON -DWerror=OFF -DWITH_MIR2=on

# Explicitly disable building with Valgrind on architectures without it.
# If valgrind is available or not gets decided via the valgrind-if-available
# virtual package.
ifeq (, $(shell command -v valgrind))
	FLAGS += -DWITH_VALGRIND=OFF
endif

override_dh_auto_configure:
	dh_auto_configure -- $(FLAGS)

override_dh_install:
	#clean gmock/gtest if it built in our build dir
	-rm -f debian/tmp/usr/lib/libgmock*.a debian/tmp/usr/lib/libgtest*.a
	-rm -rf debian/tmp/usr/include/gmock debian/tmp/usr/include/gtest
	dh_install

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/qtmir.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 .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)"
