#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk

derives_from_ubuntu := $(shell (dpkg-vendor --derives-from Ubuntu && echo "yes") || echo "no")

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure -- \
		--disable-silent-rules \
		--with-shell=/bin/sh \
		--libdir=/usr/lib/$(DEB_HOST_MULTIARCH) \
		--mandir=/usr/share/man \
		--enable-auto-setup-driverless

override_dh_auto_test:
	# Skip the "make check" test for now, as it needs avahi-daemon
	# running, which is not easily doable in the chroot of the build
	# service. The script works perfectly in the VM of the autopkgtest
	# where we actually use it.
	true

override_dh_fixperms:
	dh_fixperms -Xusr/lib/cups/backend

override_dh_auto_clean:
	[ ! -r Makefile ] || make distclean

override_dh_install:
	dh_install
	# Correct permissions of implicitclass backend, to silence Lintian
	# warning
	chmod 744 debian/cups-browsed/usr/lib/cups/backend/implicitclass
ifeq ($(derives_from_ubuntu),yes)
	# Install Apport hook
	install -D -m 644 debian/local/apport-hook.py debian/cups-browsed/usr/share/apport/package-hooks/source_cups-browsed.py
endif
	# AppArmor profile
	dh_apparmor -pcups-browsed --profile-name=usr.sbin.cups-browsed
	install -D -m 644 debian/apparmor/usr.sbin.cups-browsed debian/cups-browsed/etc/apparmor.d/usr.sbin.cups-browsed
	# systemd service file
	install -D -m 644 daemon/cups-browsed.service debian/cups-browsed/lib/systemd/system/cups-browsed.service
	# Install test script for autopkgtest, binary package
	# cups-browsed-tests
	install -D -m 755 test/run-tests.sh debian/cups-browsed-tests/usr/bin/run-tests.sh
	install -d -m 755 debian/cups-browsed-tests/usr/share/cups-browsed-tests
	install -m 644 test/*.ppd debian/cups-browsed-tests/usr/share/cups-browsed-tests
	install -m 644 test/*.convs debian/cups-browsed-tests/usr/share/cups-browsed-tests

get-orig-source:
	set -ex; \
	dbranch=`git branch --show-current`; \
	gbp import-orig --pristine-tar --uscan --no-merge --upstream-branch=upstream/latest --upstream-tag="upstream/vcs-%(version)s"; \
	utag=`git describe --exact-match heads/upstream/latest | sed -e 's#^upstream/vcs-##'`; \
	ucommit=`git rev-parse --verify upstream/latest`; \
	git checkout -f upstream/latest-tarballs; \
	git merge -Xtheirs -m "Merge $$utag from upstream's VCS" upstream/vcs-$$utag; \
	tar --strip-components=1 -xavf ../cups-browsed_$$utag.orig.tar.xz; \
	git add -A .; \
	git commit -m "$$utag tarball artifacts" || :; \
	git tag -s -m "$$utag tarball from upstream" upstream/$$utag; \
	git checkout $$dbranch; \
	git debrebase new-upstream $$utag upstream/$$utag; \
	git debrebase conclude
