#!/usr/bin/make -f
# debian/rules for hplip
# GNU copyright 1997 to 1999 by Joey Hess.
# Copyright (c) 2003 Torsten Landschoff <torsten@debian.org>
# Copyright (c) 2004-2006 Henrique de Moraes Holschuh <hmh@debian.org>

PACKAGE=hplip
BASEPKG=hplip

# DPatch
DEB_SOURCE_PACKAGE:=$(PACKAGE)
include /usr/share/dpatch/dpatch.make

# DebHelper control
export DH_ALWAYS_EXCLUDE=CVS:.cvsignore
#export DH_VERBOSE=1

DBGPKG:=$(BASEPKG)-dbg
PKGTMP:=$(CURDIR)/debian/tmp
PPDDIR:=$(PKGTMP)/usr/share/ppd
CHANGELOG:=doc/release_notes.html

# Version information
# We cannot get the hpijs version like this because we could be generating
# it at runtime when we run autogen.sh
HPLIP_BINARY_VERSION?=$(shell dpkg-parsechangelog | grep -E "^Version:" | \
	tr -d ' \t' | cut -d ':' -f 2-)
ifeq	(,$(HPLIP_BINARY_VERSION))
	$(error Could not get debian package version from debian/changelog)
endif

HPLIP_VERSION:=$(shell echo "$(HPLIP_BINARY_VERSION)" | \
			sed -e 's/+b[0-9]\+$$//')
HPLIP_UPSTREAM_VERSION:=$(shell echo "$(HPLIP_VERSION)" | \
			  sed -e 's/-[^-]\+$$//')

# Autoconf
export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
CONFFLAGS =
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  CONFFLAGS += --build $(DEB_HOST_GNU_TYPE)
else
  CONFFLAGS += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

DEBUGFLAGS=-g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	DEBUGFLAGS += -O0
else
	DEBUGFLAGS += -O2
endif

CFLAGS:=-Wall -pipe $(DEBUGFLAGS)
CXXFLAGS:=$(CFLAGS)

##
##  Build-tree preparation
##

configure:
	dh_testdir
	chmod +x debian/autogen.sh
	debian/autogen.sh

debian/hpijs.version: configure
	@if test -r $@ ; then \
		touch $@ ; \
	else \
		echo "Cannot create debian/hpijs.version, please try running debian/autogen.sh manually" ; \
		exit 1 ; \
	fi

# Clean everything up, including everything auto-generated
# at build time that needs not to be kept around in the Debian diff
clean: clean-patched unpatch
clean-patched: configure debian/hpijs.version
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	rm -f install-stamp install-arch-stamp install-indep-stamp

	-$(MAKE) clean
	-$(MAKE) distclean
	find -type d -name autom4te.cache -print0 | xargs -0 -r rm -rf \;
	-rm -f prnt/hpijs/platform.h prnt/hpijs/auto-include.h
	find -type d -name build -print0 | xargs -0 -r rm -rf \;
	find \( -name config.sub -o -name config.guess \
			-o -name config.cache -o -name config.log \) -print0 \
		| xargs -0 -r rm -f \;

	# We prefer to regenerate these using pyuic
	(cd ui && for i in *.ui ; do rm -f $${i%.ui}.py ; done)

	dh_clean

##
##  Package build
##

configure-stamp: patch-stamp configure debian/hpijs.version
	dh_testdir

	ln -sf /usr/share/misc/config.sub .
	ln -sf /usr/share/misc/config.guess .
	rm -f config.cache

	./configure CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" \
		HPLIP_PPD_PATH=/usr/share/ppd \
		$(CONFFLAGS) \
	 	--config-cache \
		--disable-dependency-tracking \
		--prefix=/usr \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		--datadir=\$${prefix}/share \
		--libexecdir=\$${prefix}/lib \
		--localstatedir=/var \
		--sysconfdir=/etc \
		--enable-debug \
		--without-icon \
		--disable-foomatic-install \
		--disable-cups-install \
		--enable-network-build \
		--enable-pp-build \
		--enable-scan-build \
		--enable-gui-build \
		--enable-fax-build

	touch configure-stamp

build: build-stamp
build-stamp: configure-stamp
	dh_testdir

	$(MAKE)

	touch build-stamp

##
## Debian packaging
##

# common part of the install: Upstream makefiles, general stuff
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(MAKE) install "DESTDIR=$(PKGTMP)" docdir=/usr/share/doc

	# Remove files we are not to ship from the install tree
	@echo Removing non-shipped files, see debian/non-shipped-files.txt
	@cat debian/non-shipped-files.txt | \
		( cd "$(PKGTMP)" && xargs -d '\n' -t -r rm -fv )

	# Edit the generated hp.conf
	( cd "$(PKGTMP)/etc/hp" && \
	  sed -e 's#^doc=.*#doc=/usr/share/doc/hplip-doc/HTML#' \
	    < hplip.conf > hplip.conf.1 && \
	    mv -f hplip.conf.1 hplip.conf )

	# Lintian and linda overrides
	cd debian ; \
	for i in *.linda ; do \
	    [ -r "$$i" ] && { \
		mkdir -p "$${i%%.linda}/usr/share/linda/overrides" ;\
		install  -m 644 "$$i" "$${i%%.linda}/usr/share/linda/overrides/$${i%%.linda}" ;\
	    } ;\
	done ;\
	for i in *.lintian ; do \
	    [ -r "$$i" ] && { \
		mkdir -p "$${i%%.lintian}/usr/share/lintian/overrides" ;\
		install  -m 644 "$$i" "$${i%%.lintian}/usr/share/lintian/overrides/$${i%%.lintian}" ;\
	    } ;\
	done

	touch install-stamp

# install arch-dependent packages specific files
install-arch: install-arch-stamp
install-arch-stamp: install-stamp
	# Remove stray #! lines on python libraries, and fix permissions
	find "$(PKGTMP)/usr/lib/hplip" -maxdepth 1 -mindepth 1 -type d -print0 | \
		xargs -0 -r -i@ find @ -type f -name '*.py' | \
		while read -r i ; do \
			chmod a-x "$$i" && \
			sed < "$$i" > "$$i.tmp" -e '/^#!/ {1 d}' && \
			mv -f "$$i.tmp" "$$i" ; \
		done

	mkdir -p "$(PKGTMP)/usr/share/pixmaps"
	install -m 644 debian/*.xpm "$(PKGTMP)/usr/share/pixmaps"
	mkdir -p "$(PKGTMP)/usr/share/applications"
	install -m 644 debian/*.desktop "$(PKGTMP)/usr/share/applications"

	mkdir -p "$(PKGTMP)/etc/sane.d/dll.d"
	install -m 644 debian/$(BASEPKG).sane "$(PKGTMP)/etc/sane.d/dll.d/$(PACKAGE)"

	# Fax support: HPLIP upstream is changing their minds too fast to
	# trust their makefiles on this.
	[ -e "$(PKGTMP)/usr/lib/hplip/fax/hpfax" ] && \
	  mv -f "$(PKGTMP)/usr/lib/hplip/fax/hpfax" "$(PKGTMP)/usr/lib/cups/backend/"

	# Fax PPD, install to destdir directly
	mkdir -p "$(CURDIR)/debian/hpijs/usr/share/ppd/hpijs/HP"
	sed < "fax/ppd/HP-Fax-hplip.ppd" \
		-e '/^[*]NickName:/ s/" *$$/ - HPLIP $(HPLIP_UPSTREAM_VERSION)"/' \
		| gzip -9 \
		> "$(CURDIR)/debian/hpijs/usr/share/ppd/hpijs/HP/HP-Fax-hplip.ppd.gz"

	# Python update helper
	mkdir -p "$(PKGTMP)/usr/share/python/runtime.d/"
	install -m 755 debian/hplip.rtupdate "$(PKGTMP)/usr/share/python/runtime.d/"

	dh_install -a "--sourcedir=$(PKGTMP)" --list-missing >install-arch-stamp 2>&1 || \
		( echo "Last five lines of error log:" ; tail -n 5 install-arch-stamp ; exit 1 )

# install arch-independent packages specific files
install-indep: install-indep-stamp
install-indep-stamp: install-stamp
	# PPDs
	# 1. Append hplip identifier to the long nickname
	# 2. Install to basedir/<Manufacturer>/
	# 3. Normalize case of PPD file names
	# 4. Compress all PPDs
	# 
	# Fax PPD: shipped by arch-dep hpijs package
	find prnt/hpijs/ppd -type f -name '*-hpijs.ppd' | \
	while read -r i ; do \
		PPDSUITE="hpijs" ;\
		PPDVENDOR=$$(sed -r -n \
			-e '/^[*]Manufacturer/ { s/[^:]+:[[:blank:]]+"([^"]*)".*/\1/;s/[[:blank:]]//g;p }' \
			< "$$i" ) ;\
		mkdir -p "$(PPDDIR)/$${PPDSUITE}/$${PPDVENDOR}" ;\
		PPDFILE=$$(echo "$$i" | sed -r -e 's#.*/([^/]+)$$#\1#' \
			-e 's#(^|/)hp[-_]#HP-#i' \
			-e s/series/Series/i -e s/color/Color/i \
			-e s/office/Office/i -e s/business/Business/i \
			-e s/jet/Jet/i -e s/ink/Ink/i -e s/desk/Desk/i \
			-e s/smart/Smart/i -e s/photo/Photo/i -e s/psc/PSC/i \
			-e s/laser/Laser/i -e s/design/Design/i \
			-e s/mfp/MFP/i -e 's/[-_]hpijs/-hpijs/i' ) ;\
		sed < "$$i" -e '/^[*]NickName:/ s/" *$$/ - HPLIP $(HPLIP_UPSTREAM_VERSION)"/' \
			| gzip -9 \
			> "$(PPDDIR)/$${PPDSUITE}/$${PPDVENDOR}/$${PPDFILE}.gz" ;\
		chmod 644 "$(PPDDIR)/$${PPDSUITE}/$${PPDVENDOR}/$${PPDFILE}.gz" ;\
	done

	dh_install -i "--sourcedir=$(PKGTMP)" --list-missing >install-indep-stamp 2>&1 || \
		( echo "Last five lines of error log:" ; tail -n 5 install-indep-stamp ; exit 1 )

# Build architecture-independent packages
binary-indep: install-indep debian/hpijs.version
	dh_testdir -i
	dh_testroot
#	dh_installdebconf -i
	dh_installdocs -i
	dh_installexamples -i
	dh_installmenu -i
#	dh_installlogrotate -i
#	dh_installemacsen -i
#	dh_installpam -i
#	dh_installmime -i
#	dh_installinit -i
#	dh_installcron -i
	dh_installman -i
#	dh_installinfo -i
	dh_installchangelogs -i $(CHANGELOG)
	dh_link -i
	dh_strip -i --dbg-package=$(DBGPKG)
	dh_compress -i
	dh_fixperms -i
#	dh_perl -i
#	dh_pysupport -i
#	dh_python -i /usr/lib/hplip
#	dh_makeshlibs -i
	dh_installdeb -i
	dh_shlibdeps -i
	dh_gencontrol -phpijs-ppds -- \
		-v"$$(cat debian/hpijs.version)+$(HPLIP_BINARY_VERSION)" \
		-V"hplip:source:Version=$(HPLIP_VERSION)" \
		-V"hpijs:source:Version=$$(cat debian/hpijs.version)+$(HPLIP_VERSION)" \
		-V"hplip:Upstream-Version=$(HPLIP_UPSTREAM_VERSION)" \
		-V"hpijs:Upstream-Version=$$(cat debian/hpijs.version)" \
		-V"hplip:binary:Version=$(HPLIP_BINARY_VERSION)" \
		-V"hpijs:binary:Version=$$(cat debian/hpijs.version)+$(HPLIP_BINARY_VERSION)"
	dh_gencontrol -i -Nhpijs-ppds -- \
		-V"hplip:source:Version=$(HPLIP_VERSION)" \
		-V"hpijs:source:Version=$$(cat debian/hpijs.version)+$(HPLIP_VERSION)" \
		-V"hplip:Upstream-Version=$(HPLIP_UPSTREAM_VERSION)" \
		-V"hpijs:Upstream-Version=$$(cat debian/hpijs.version)" \
		-V"hplip:binary:Version=$(HPLIP_BINARY_VERSION)" \
		-V"hpijs:binary:Version=$$(cat debian/hpijs.version)+$(HPLIP_BINARY_VERSION)"
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent packages
binary-arch: install-arch debian/hpijs.version
	dh_testdir -a
	dh_testroot
#	dh_installdebconf -a
	dh_installdocs -a
	dh_installexamples -a
	dh_installmenu -a
	dh_desktop -a
#	dh_installlogrotate -a
#	dh_installemacsen -a
#	dh_installpam -a
#	dh_installmime -a
#	dh_installcron -a
	dh_installman -a
#	dh_installinfo -a
	dh_installchangelogs -a $(CHANGELOG)
	dh_pysupport -a /usr/lib/hplip
	dh_python -a /usr/lib/hplip
	# must come after dh_pysupport and dh_python, or the postinst
	# ordering will be screwed up and break.
	dh_installinit -phplip --init-script=hplip -- defaults 19
#	dh_perl -a
	dh_link -a
	dh_strip -a --dbg-package=$(DBGPKG)
	dh_compress -a
	dh_fixperms -a
#	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -phpijs -- \
		-v"$$(cat debian/hpijs.version)+$(HPLIP_BINARY_VERSION)" \
		-V"hplip:source:Version=$(HPLIP_VERSION)" \
		-V"hpijs:source:Version=$$(cat debian/hpijs.version)+$(HPLIP_VERSION)" \
		-V"hplip:Upstream-Version=$(HPLIP_UPSTREAM_VERSION)" \
		-V"hpijs:Upstream-Version=$$(cat debian/hpijs.version)" \
		-V"hplip:binary:Version=$(HPLIP_BINARY_VERSION)" \
		-V"hpijs:binary:Version=$$(cat debian/hpijs.version)+$(HPLIP_BINARY_VERSION)"
	dh_gencontrol -a -Nhpijs -- \
		-V"hplip:source:Version=$(HPLIP_VERSION)" \
		-V"hpijs:source:Version=$$(cat debian/hpijs.version)+$(HPLIP_VERSION)" \
		-V"hplip:Upstream-Version=$(HPLIP_UPSTREAM_VERSION)" \
		-V"hpijs:Upstream-Version=$$(cat debian/hpijs.version)" \
		-V"hplip:binary:Version=$(HPLIP_BINARY_VERSION)" \
		-V"hpijs:binary:Version=$$(cat debian/hpijs.version)+$(HPLIP_BINARY_VERSION)"
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch
.PHONY: build clean clean-patched binary-indep binary-arch binary install install-arch install-indep
