#!/usr/bin/make -f

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

export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

%:
	dh $@ --parallel --with autoreconf,systemd

override_dh_auto_configure:
	dh_auto_configure -- \
		--disable-silent-rules \
		--libdir=/usr/lib/$(DEB_HOST_MULTIARCH) \
		--mandir=/usr/share/man \
		--enable-static \
		--with-test-font-path=/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf

override_dh_installchangelogs:
	dh_installchangelogs NEWS

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 700 debian/cups-filters/usr/lib/cups/backend/serial

override_dh_makeshlibs:
	dh_makeshlibs -- -c4

override_dh_auto_clean:
	[ ! -r Makefile ] || make distclean
	rm -f debian/*.upstart # master copy is in source tree

override_dh_install:
ifeq ($(derives_from_ubuntu),yes)
	# Use upstart script on Ubuntu; we need to hide it away for Debian
	# builds, as dh_installinit does not have a --sysvinit-only
	cp utils/cups-browsed-upstart.conf debian/cups-browsed.upstart
endif
	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

	# Install the modules loader for lp, ppdev and parport_pc
	install -D -m 644 debian/local/modules-load.conf debian/cups-filters/etc/modules-load.d/cups-filters.conf

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

	# Install ippusbxd.
	# As there is not enough time any more before Feature Freeze for
	# Utopic to create a proper ippusbxd package in Debian and sync it
	# into Ubuntu we add ippusbxd (1 single executable) to cups-filters,
	# binary package cups-filters-ippusbxd. We do not use straight
	# ippusbxd here as ippusbxd has lower version numbers than
	# cups-filters and so a transition of the ippusbxd binary package
	# from the cups-filters source package to the ippusbxd source
	# package would be more complicated.

	( cd debian/local/ippusbxd; \
	  gcc -o2 -std=c99 -Wall -Wextra -pedantic -pedantic-errors -o ippusbxd -I /usr/include/libusb-1.0 *.c -pthread -lusb-1.0 -ludev; \
	)
	install -D -m 755 debian/local/ippusbxd/ippusbxd debian/cups-filters-ippusbxd/usr/sbin/ippusbxd
	install -D -m 644 debian/local/ippusbxd/readme.md debian/cups-filters-ippusbxd/usr/share/doc/cups-filters-ippusbxd/readme.md
	install -D -m 644 debian/local/ippusbxd/license.md debian/cups-filters-ippusbxd/usr/share/doc/cups-filters-ippusbxd/license.md
	install -D -m 644 debian/local/ippusbxd/behaviour debian/cups-filters-ippusbxd/usr/share/doc/cups-filters-ippusbxd/behaviour
