#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
BUILDDIR = obj-$(DEB_HOST_MULTIARCH)

CONFIGURE_ARGS = \
	-DCMAKE_BUILD_TYPE=Release \
	-DCMAKE_INSTALL_PREFIX=/usr \
	-DLIB_INSTALL_DIR:STRING="lib/$(DEB_HOST_MULTIARCH)"

LICENSE_TMPFILE := $(shell mktemp "/tmp/license-check.tmp.XXXXXX")

%:
	dh $@

override_dh_auto_build:
	# dynamically linked
	dh_auto_build -B $(BUILDDIR)
	# statically linked
	dh_auto_build -B $(BUILDDIR)-static

override_dh_auto_configure:
	# dynamically linked
	dh_auto_configure -B $(BUILDDIR) \
		-- $(CONFIGURE_ARGS) \
		-DBUILD_STATIC_LIBS=OFF \
		-DBUILD_TESTING=ON

	# statically linked
	dh_auto_configure -B $(BUILDDIR)-static \
		-- $(CONFIGURE_ARGS) \
		-DBUILD_STATIC_LIBS=ON \
		-DBUILD_TESTING=OFF

override_dh_auto_install:
	dh_auto_install -O--buildsystem=cmake -B $(BUILDDIR)
	find $(CURDIR)/debian/tmp/ -type d -name .cmake | xargs -r rm -rfv

execute_before_dh_install:
	# put the static libs together with the rest of the stuff
	cp -v $(BUILDDIR)-static/src/*.a debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/
	-$(RM) -r $(BUILDDIR)-static

check_license_update:
	@decopy --no-progress -X debian --output $(LICENSE_TMPFILE)
	@diff -urN $(CURDIR)/debian/copyright.tmpl $(LICENSE_TMPFILE)
