#!/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

SF_MIRRORS:= optusnet ovh puzzle superb-east
-include /usr/share/gnome-pkg-tools/1/rules/sf-get-source.mk

# 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)
export DEB_HOST_ARCH_OS   ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)

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

ifeq ($(DEB_HOST_ARCH_OS), linux)
  CONFFLAGS += --enable-pp-build
else
  CONFFLAGS += --disable-pp-build
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: Makefile.am configure.in
	dh_testdir
	touch NEWS README AUTHORS ChangeLog
	chmod +x debian/autogen.sh
	debian/autogen.sh

# 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
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	rm -f install-stamp install-arch-stamp install-indep-stamp
	rm -f *.1

	[ ! -f Makefile ] || $(MAKE) clean
	[ ! -f Makefile ] || $(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)

	rm -f NEWS README AUTHORS ChangeLog

	dh_clean

##
##  Package build
##

configure-stamp: patch-stamp configure
	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 \
		--docdir=\$${prefix}/share/doc/hplip \
		--with-docdir=\$${prefix}/share/doc/hplip \
		--enable-foomatic-drv-install \
		--disable-foomatic-rip-hplip-install \
		--with-drvdir=\$${prefix}/share/cups/drv \
		--with-hpppddir=\$${prefix}/share/ppd/hplip/HP \
		--datadir=\$${prefix}/share \
		--libexecdir=\$${prefix}/lib \
		--localstatedir=/var \
		--sysconfdir=/etc \
		--without-icondir \
		--disable-foomatic-ppd-install \
		--enable-hpcups-install \
		--enable-cups-drv-install \
		--enable-hpijs-install \
		--enable-foomatic-drv-install \
		--enable-foomatic-ppd-install \
		--enable-network-build \
		--enable-scan-build \
		--enable-gui-build \
		--enable-fax-build \
		--disable-qt3 \
		--enable-qt4 \
		--enable-policykit \
		--enable-udev-acl-rules

	touch configure-stamp

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

	$(MAKE) docdir=\$${prefix}/share/doc/hplip dist_firm_DATA= am__dist_firm_DATA_DIST=

	touch build-stamp

##
## Debian packaging
##

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

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

	# Install wrapper script for hp-plugin, to be called by update-notifier
	# on udev signal (Ubuntu only)
	if [ "`lsb_release -is 2>/dev/null`" = "Ubuntu" ]; then \
		install -d $(CURDIR)/debian/hplip/usr/bin/; \
	fi

	# Install Apport hook
	install -D -m 644 debian/local/apport-hook.py $(PKGTMP)/usr/share/apport/package-hooks/source_hplip.py

	# 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 )

	# Uncompress PPDs, this makes the package smaller for distribution
	-find $(PPDDIR) -name '*.ppd.gz' | xargs gunzip -f

	# Remove identical PPD files shipped under different names
	( cd "$(PKGTMP)" && fdupes -r1q usr/share/ppd | while read s; do \
	      set -- $$(echo $$s | tr ' ' '\n' | sort); \
	      f=$$1; shift; \
	      for d; do \
		  echo "Removing duplicate PPD file $$d, original is $$f"; \
		  rm $$d; \
	      done; \
	  done )

	# Edit the generated hplip.conf
	( cd "$(PKGTMP)/etc/hp" && \
	  cat hplip.conf | \
	  sed -e 's#^doc=.*#doc=/usr/share/doc/hplip-doc/HTML#' | \
	  sed -e 's#^ui-toolkit=.*#ui-toolkit=qt4#' \
	    > 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

	# Link all executables to /usr/bin and generate the corresponding
	# man pages
	export HPLIP_BUILD=1; \
	( cd ./debian/tmp/usr/share/hplip/; \
	    for file in *.py; do \
		if [ -x $$file ] && [ $$file != install.py ] && \
		   [ $$file != hpssd.py ] && [ $$file != dat2drv.py ] && \
		   [ $$file != __init__.py ]; then \
		    dest=hp-$${file%.py}; \
		    ln -s /usr/share/hplip/$$file \
			../../bin/$$dest 2>/dev/null || :; \
		    PYTHONPATH=../../lib/python2.5/site-packages/:../../lib/python2.6/dist-packages/  LD_LIBRARY_PATH=../../lib/ ../../bin/$$dest --help-man > $(CURDIR)/$$dest.1 ; \
		fi; \
	    done \
	)
	install -d ./debian/tmp/usr/sbin/
	cp hpssd.py ./debian/tmp/usr/sbin/hpssd

	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/share/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
	# executable-not-elf-or-script
	chmod -x $(PKGTMP)/usr/share/hplip/installer/distros.dat

	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)"

	mkdir -p "$(PKGTMP)/usr/lib/cups/filter/"
	install -m 755 hplipjs "$(PKGTMP)/usr/lib/cups/filter/"

	# Fax PPDs, install to destdir directly
	mkdir -p "$(CURDIR)/debian/hpijs/usr/share/ppd/hplip/HP"
	mkdir -p "$(CURDIR)/debian/hplip-cups/usr/share/ppd/hplip/HP"
	cp fax/ppd/HP-Fax*-hpijs.ppd.gz \
	   "$(CURDIR)/debian/hpijs/usr/share/ppd/hplip/HP/"
	cp fax/ppd/HP-Fax*-hpcups.ppd.gz \
	   "$(CURDIR)/debian/hplip-cups/usr/share/ppd/hplip/HP/"

	# Uncompress PPDs, this makes the package smaller for distribution
	-find $(PPDDIR) -name '*.ppd.gz' | xargs gunzip -f

	# Generate .fdi file for HAL setting "scanner" capability to all
	# HPLIP-supported devices (also without scanner), so that permissions
	# get automatically set so that always the user logged in on the
	# system's desktop has full access to the device.
	install -d -m 755 $(PKGTMP)/usr/share/hal/fdi/preprobe/10osvendor/
	sh debian/local/scripts/create_hal_global_fdi_from_hpmud_rules.sh \
	> $(PKGTMP)/usr/share/hal/fdi/preprobe/10osvendor/20-hplip-devices.fdi

	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 install-arch-stamp
	# Remove the Fax PPDs, we have handled them in the arch-dependent part
	-find $(PPDDIR) -name '*Fax*.ppd*' | xargs rm

	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
	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 /usr/share/hplip
#	dh_makeshlibs -i
	dh_installdeb -i
	dh_shlibdeps -i --dpkg-shlibdeps-params=--ignore-missing-info
	dh_gencontrol -i -- \
		-V"hplip:source:Version=$(HPLIP_VERSION)" \
		-V"hpijs:source:Version=$(HPLIP_VERSION)" \
		-V"hplip:Upstream-Version=$(HPLIP_UPSTREAM_VERSION)" \
		-V"hpijs:Upstream-Version=$(HPLIP_UPSTREAM_VERSION)" \
		-V"hplip:binary:Version=$(HPLIP_BINARY_VERSION)" \
		-V"hpijs:binary:Version=$(HPLIP_BINARY_VERSION)"
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent packages
binary-arch: install-arch
	dh_testdir -a
	dh_testroot

	mkdir -p debian/hplip/usr/share/bug/hplip
	cp debian/bug/* debian/hplip/usr/share/bug/hplip
	mkdir -p debian/hplip/usr/share/bug/hplip-cups
	cp debian/bug/presubj debian/hplip/usr/share/bug/hplip-cups
	mkdir -p debian/hplip/usr/share/bug/hplip-gui
	cp debian/bug/presubj debian/hplip/usr/share/bug/hplip-gui
	mkdir -p debian/hplip/usr/share/bug/hpijs
	cp debian/bug/presubj debian/hplip/usr/share/bug/hpijs

#	dh_installdebconf -a
	dh_installdocs -a
	dh_installexamples -a
	dh_installmenu -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)
	# must come after dh_pysupport and dh_python, or the postinst
	# ordering will be screwed up and break.
	#dh_installinit -phplip --init-script=hplip -- multiuser 19
	dh_installudev
	dh_link -a
	dh_strip -a --dbg-package=$(DBGPKG)
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a --dpkg-shlibdeps-params=--ignore-missing-info
	dh_gencontrol -a -- \
		-V"hplip:source:Version=$(HPLIP_VERSION)" \
		-V"hpijs:source:Version=$(HPLIP_VERSION)" \
		-V"hplip:Upstream-Version=$(HPLIP_UPSTREAM_VERSION)" \
		-V"hpijs:Upstream-Version=$(HPLIP_UPSTREAM_VERSION)" \
		-V"hplip:binary:Version=$(HPLIP_BINARY_VERSION)" \
		-V"hpijs:binary: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
