#!/usr/bin/make -f

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

update-patch-series:
	mkdir -p $(CURDIR)/debian/patches
	rm -f $(CURDIR)/debian/patches/*.patch
	git format-patch -o $(CURDIR)/debian/patches patches ^upstream | \
	  xargs -n 1 -r basename > $(CURDIR)/debian/patches/series
	for patch in $$(ls $(CURDIR)/debian/patches/*.patch); do \
	  lines=$$(cat $$patch | wc -l); \
	  head -n $$(($$lines - 3)) $$patch > $${patch}.chomped; \
	  mv $${patch}.chomped $$patch; \
	done

override_dh_autoreconf:
	[ -d m4local ] || mkdir -p m4local
	dh_autoreconf

override_dh_auto_configure:
	dh_auto_configure -- \
	  --libdir=/usr/lib/$(DEB_HOST_MULTIARCH) \
	  --disable-static --enable-shared --disable-rpath \
	  --disable-static-genppd \
	  --with-modules=dlopen \
	  --enable-libgutenprintui2 --with-gimp2 \
	  --with-cups --enable-cups-level3-ppds --enable-globalized-cups-ppds \
	  --enable-cups-1_2-enhancements --disable-cups-ppds \
	  --with-foomatic --with-foomatic3 \
	  --disable-test \
	  --enable-nls

override_dh_install-indep:
	dh_install -i
	rm -f debian/gutenprint-locales/usr/share/locale/*/*.po

override_dh_installdocs:
	# Let it be installed by dh_installdocs
	cp src/cups/README debian/README.cups
	dh_installdocs -pescputil --link-doc=libgutenprint2
	dh_installdocs -pijsgutenprint --link-doc=libgutenprint2
	dh_installdocs -plibgutenprintui2-dev --link-doc=libgutenprintui2-1
	dh_installdocs --remaining-packages

override_dh_clean:
	- rm -f debian/README.cups
	dh_clean

override_dh_install-arch:
ifeq ($(DEB_BUILD_ARCH_OS),linux)
	# Install common files
	dh_install -pprinter-driver-gutenprint --ignore=debian/printer-driver-gutenprint.install.linux
endif
	dh_install -a
# Remove rpath
	find debian/printer-driver-gutenprint/usr/lib -type f -perm /0111 -print0 | xargs -0 chrpath --delete
# Documentation fixup
	mv debian/ijsgutenprint/usr/share/man/man1/ijsgutenprint.1 debian/ijsgutenprint/usr/share/man/man1/ijsgutenprint.5.2.1
	ln -s ijsgutenprint.5.2.1.gz debian/ijsgutenprint/usr/share/man/man1/ijsgutenprint.1.gz
	ln -s ijsgutenprint.5.2 debian/ijsgutenprint/usr/bin/ijsgutenprint
# Install ppd-updater file
	install -D --mode=644 debian/printer-driver-gutenprint.ppd-updater $(CURDIR)/debian/printer-driver-gutenprint/usr/share/cups/ppd-updaters/printer-driver-gutenprint.ppd-updater

override_dh_installchangelogs:
	dh_installchangelogs NEWS

override_dh_compress:
	dh_compress -X.pdf -X.odt

override_dh_auto_test:
	- dh_auto_test

override_dh_perl:
	dh_perl -d

override_dh_makeshlibs:
	dh_makeshlibs -a -V -X usr/lib/$(DEB_HOST_MULTIARCH)/gutenprint

override_dh_shlibdeps:
	dh_shlibdeps -a -X usr/lib/$(DEB_HOST_MULTIARCH)/gutenprint -ldebian/libgutenprint2/usr/lib/$(DEB_HOST_MULTIARCH):debian/libgutenprintui2-1/usr/lib/$(DEB_HOST_MULTIARCH)

override_dh_fixperms:
	dh_fixperms
# Make the gutenprint52+usb backend run as root, since /dev/bus/usb/*
# are root:root in udev < 147, and the usblp kernel module is not used
# here; udev 147 makes most of those printers accessible to lp, but
# apparently not all of them
	[ ! -f debian/printer-driver-gutenprint/usr/lib/cups/backend/gutenprint52+usb ] || \
		chmod go-x debian/printer-driver-gutenprint/usr/lib/cups/backend/gutenprint52+usb
