#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.
#
# Modified to make a template file for a multi-binary package with separated
# build-arch and build-indep targets  by Bill Allombert 2001

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# work around libpng crash on our test PNGs with 8 bit colormaps (LP #710881)
export NO_PNG_PKG_MANGLE=1

# Enabling PIE globally doesn't work, but ./configure already enables PIE
# where necessary.
export DEB_BUILD_MAINT_OPTIONS = hardening=+all,-pie
DPKG_EXPORT_BUILDFLAGS=1
include /usr/share/dpkg/buildflags.mk
LDFLAGS+= -Wl,--as-needed
# The build system uses only DSOFLAGS but not LDFLAGS to build some libraries.
# Add LDFLAGS to enable (hardening) build flags.
export DSOFLAGS = $(LDFLAGS)

include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/class/autotools.mk

DEB_CONFIGURE_EXTRA_FLAGS := --with-optim=$(DEB_OPTFLAGS) --libdir=/usr/lib/$(DEB_HOST_MULTIARCH) --mandir=/usr/share/man --with-docdir=/usr/share/cups/doc-root --localedir=/usr/share/cups/locale --enable-slp --enable-libpaper --enable-ssl --enable-gnutls --disable-openssl --enable-threads --enable-static --enable-debug --enable-dbus --with-dbusdir=/etc/dbus-1 --enable-gssapi --enable-avahi --with-pdftops=/usr/bin/gs --disable-launchd --with-cups-group=lp --with-system-groups=lpadmin --with-printcap=/var/run/cups/printcap --with-log-file-perm=0640 --with-local_protocols='CUPS dnssd' --with-remote_protocols='CUPS dnssd'
ifeq ($(DEB_HOST_ARCH_OS),hurd)
DEB_CONFIGURE_EXTRA_FLAGS += --disable-libusb
else
DEB_CONFIGURE_EXTRA_FLAGS += --enable-libusb
endif

DEB_MAKE_INSTALL_TARGET := install BUILDROOT=$(DEB_DESTDIR)
DEB_INSTALL_CHANGELOGS_ALL := CHANGES.txt

ifeq ($(shell dpkg-vendor --query vendor), Ubuntu)
DEB_DH_INSTALLINIT_ARGS := --upstart-only
else
DEB_DH_INSTALLINIT_ARGS := -u'start 50 2 3 4 5 . stop 80 1 .'
endif

DEB_DH_STRIP_ARGS := --dbg-package=cups-dbg
DEB_DH_FIXPERMS_ARGS := -Xusr/lib/cups/backend-available
DEB_DH_COMPRESS_ARGS := -Xusr/share/doc/libcups2-dev/examples/scripting
DEB_DH_INSTALL_SOURCEDIR := debian/tmp
ifneq ($(findstring $(DEB_BUILD_ARCH), arm m68k),)
DEB_MAKE_CHECK_TARGET := check || true
else
DEB_MAKE_CHECK_TARGET := check
endif
DPKG_GENSYMBOLS_CHECK_LEVEL=4
export DPKG_GENSYMBOLS_CHECK_LEVEL

post-patches::
	set -e; if dpkg-vendor --is ubuntu && ! [ -e debian/patches/ubuntu/stamp-applied ]; then \
	    echo '---- Applying Ubuntu specific patches'; \
	    for p in debian/patches/ubuntu/*; do \
	        patch -p1 --no-backup-if-mismatch < $$p; \
	    done; \
	    touch debian/patches/ubuntu/stamp-applied; \
	fi
	# Rebuild ./configure to get build system patches working
	aclocal
	autoconf

clean::
	if [ -e debian/patches/ubuntu/stamp-applied ]; then \
	    echo '---- Unapplying Ubuntu specific patches'; \
	    for p in debian/patches/ubuntu/*; do \
	        patch -Rp1 --no-backup-if-mismatch < $$p; \
	    done; \
	    rm debian/patches/ubuntu/stamp-applied; \
	fi

cleanbuilddir::
	rm -f man/client.conf.man packaging/cups.list
	rm -f conf/mime.convs conf/snmp.conf init/org.cups.cups-lpd.plist
	[ ! -f Makedefs ] || make distclean
	rm -f debian/*.upstart # master copy is in debian/local

common-install-prehook-impl::
	(cd fonts && $(MAKE) install BUILDROOT=$(DEB_DESTDIR))

	# Use upstart script on Ubuntu; we need to hide it away for Debian
	# builds, as dh_installinit does not have a --sysvinit-only
	if dpkg-vendor --is ubuntu; then \
	    cp debian/local/*.upstart debian; \
	fi

binary-post-install/cups::
	(cd $(DEB_DESTDIR)/../cups/usr/share/doc/cups && ln -sf ../../cups/doc-root online-docs)

	# Remove all files which get replaced by the ones in the cups-filters
	# package
	rm debian/$(cdbs_curpkg)/usr/lib/cups/backend*/serial
	rm debian/$(cdbs_curpkg)/usr/lib/cups/backend*/parallel
	rm debian/$(cdbs_curpkg)/usr/lib/cups/filter/bannertops
	rm debian/$(cdbs_curpkg)/usr/lib/cups/filter/commandtoescpx
	rm debian/$(cdbs_curpkg)/usr/lib/cups/filter/commandtopclx
	rm debian/$(cdbs_curpkg)/usr/lib/cups/filter/imagetops
	rm debian/$(cdbs_curpkg)/usr/lib/cups/filter/imagetoraster
	rm debian/$(cdbs_curpkg)/usr/lib/cups/filter/pdftops
	rm debian/$(cdbs_curpkg)/usr/lib/cups/filter/rastertoescpx
	rm debian/$(cdbs_curpkg)/usr/lib/cups/filter/rastertopclx
	rm debian/$(cdbs_curpkg)/usr/lib/cups/filter/texttops
	rm -rf debian/$(cdbs_curpkg)/usr/share/cups/banners
	rm debian/$(cdbs_curpkg)/usr/share/cups/data/testprint
	rm debian/$(cdbs_curpkg)/usr/share/cups/data/psglyphs
	rm debian/$(cdbs_curpkg)/usr/share/cups/fonts/*

	# Comment out conversion rules for removed filters
	perl -p -i -e 's:^(.*\s+(pdftops|texttops|imagetops|bannertops|imagetoraster)\s*)$$:#\1:' debian/$(cdbs_curpkg)/usr/share/cups/mime/mime.convs

	# Ensure that we don't ship anything in /var/run
	rm -rf $(DEB_DESTDIR)/../cups/var/run

	# PPD directories
	#mkdir -p "$(DEB_DESTDIR)/../cups/usr/share/ppd/cups-included/"

	# PPD-updating triggers directory
	mkdir -p "$(DEB_DESTDIR)/../cups/usr/share/cups/ppd-updaters/"

	#dh_usrlocal

	# Install AppArmor, ufw profile, and Apport hook on Ubuntu
	if dpkg-vendor --is ubuntu; then \
	   install -D -m 644 debian/local/apparmor-profile debian/$(cdbs_curpkg)/etc/apparmor.d/usr.sbin.cupsd; \
	   install -D -m 644 debian/local/cups.ufw.profile debian/$(cdbs_curpkg)/etc/ufw/applications.d/cups; \
	   install -D -m 644 debian/local/apport-hook.py debian/$(cdbs_curpkg)/usr/share/apport/package-hooks/source_cups.py; \
	fi

	# Make the usb backend run as root, since /dev/bus/usb/* are
	# root:root in udev < 147, and cups does not use the usblp kernel
	# module any more; udev 147 makes most of those printers accessible to
	# lp, but apparently not all of them
	chmod go-x debian/$(cdbs_curpkg)/usr/lib/cups/backend-available/usb; \

binary-post-install/cups-common::
	rm debian/$(cdbs_curpkg)/usr/share/cups/charsets/utf-8

binary-post-install/libcupsimage2-dev::
	rm -r debian/libcupsimage2-dev/usr/share/doc/libcupsimage2-dev
	ln -s libcupsimage2 debian/libcupsimage2-dev/usr/share/doc/libcupsimage2-dev
binary-post-install/cups-bsd::
	rm -r debian/cups-bsd/usr/share/doc/cups-bsd
	ln -s libcups2 debian/cups-bsd/usr/share/doc/cups-bsd
binary-post-install/libcups2-dev::
	rm -f debian/$(cdbs_curpkg)/usr/share/doc/$(cdbs_curpkg)/examples/scripting/php/*.o
	rm -f debian/$(cdbs_curpkg)/usr/share/doc/$(cdbs_curpkg)/examples/scripting/php/*.so
	# debian/libcups2-dev.install entry cannot rename files on-the-fly
	cp cups/language-private.h debian/$(cdbs_curpkg)/usr/include/cups/i18n.h
binary-post-install/cups-client::
	rm -r debian/cups-client/usr/share/doc/cups-client
	ln -s libcups2 debian/cups-client/usr/share/doc/cups-client
