#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk

derives_from_ubuntu := $(shell (dpkg-vendor --derives-from Ubuntu && echo "yes") || echo "no")
# Some cups-filters packages are in Ubuntu universe so only suggest them in dh_gencontrol
ifeq ($(derives_from_ubuntu),yes)
	SUBSTVARS = -Vcf:Suggests="lynx"
else
	SUBSTVARS = -Vcf:Recommends="lynx"
endif

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-static \
		--enable-mutool \
		--enable-auto-setup-driverless \
		--with-mutool-path=/usr/bin/mutool \
		--with-test-font-path=/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf

override_dh_installdocs:
	dh_installdocs -A AUTHORS README

override_dh_fixperms:
	dh_fixperms -Xusr/lib/cups/backend
	
	# Make the serial backend run as root, since /dev/ttyS* are
	# root:dialout and thus not accessible as user lp
	chmod 744 debian/cups-filters/usr/lib/cups/backend/serial

override_dh_makeshlibs:
	dh_makeshlibs -- -c4

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

override_dh_install:
	dh_install
	
ifeq ($(derives_from_ubuntu),yes)
	#  - Install Apport hook
	#  - Replace standard test page template by Ubuntu-branded one
	install -D -m 644 debian/local/apport-hook.py debian/cups-filters/usr/share/apport/package-hooks/source_cups-filters.py
	install -D -m 644 debian/local/default-testpage-ubuntu.pdf debian/cups-filters/usr/share/cups/data/default-testpage.pdf
else
	mkdir -p debian/cups-filters/usr/share/cups/data/
	rsvg-convert debian/local/default-testpage-debian.svg -f pdf > debian/cups-filters/usr/share/cups/data/default-testpage.pdf
endif

# Known not-present: m68k hurd-i386 kfreebsd-{amd64,i386}
ifneq ($(filter $(DEB_HOST_ARCH),amd64 i386 mips64el mipsel alpha hppa ia64 sparc64),)
	# Install the modules loader for lp, ppdev and parport_pc, only on allow-listed architectures where these are known-present
	install -D -m 644 debian/local/modules-load.conf debian/cups-filters/etc/modules-load.d/cups-filters.conf
endif

	dh_apparmor -pcups-browsed --profile-name=usr.sbin.cups-browsed

# Some cups-filters packages are in Ubuntu universe so only suggest them there
override_dh_gencontrol:
	dh_gencontrol -- $(SUBSTVARS)

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-filters_$$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
