#!/usr/bin/make -f
# rules to package Gimp-Print
# $Id: rules,v 1.1.1.2 2004/07/23 06:26:25 jlovell Exp $
#
# Uncomment this to turn on verbose mode. 
#export DH_VERBOSE=1

# This has to be exported to make some magic below work.
export DH_OPTIONS

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE       ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE      ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)


ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -g
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

# Get upstream version, and define libgimpprint suffix
UPSTREAM_VERSION = $(shell dpkg-parsechangelog | grep ^Version | sed "s/.* //" | sed "s/-[^-]*$$//")
UPSTREAM_VERSION_COMPACT = $(shell echo '$(UPSTREAM_VERSION)' | sed "s/\(.*\..*\)\..*/\1/"
LIBRARY_VERSION =  -$(UPSTREAM_VERSION)
#LIBRARY_VERSION = 1

autoconfigure: autoconfigure-stamp
autoconfigure-stamp: debian/control debian/libgimpprint.install debian/libgimpprint-doc.doc-base.developer debian/libgimpprint-doc.doc-base.reference debian/libgimpprint-doc.install
	dh_testdir
# Create versioned symlinks
	cd debian; \
	for file in libgimpprint*; do \
	ln -sfv $$file `echo $$file | sed -e 's/\(libgimpprint[[:alpha:]]*\)\(.*\)/\1$(LIBRARY_VERSION)\2/'`; \
	done
	rm debian/libgimpprint$(LIBRARY_VERSION)*.in
	mkdir debian/build; \
	cd debian/build; \
	../../configure --host=$(DEB_HOST_GNU_TYPE) \
	  --build=$(DEB_BUILD_GNU_TYPE) \
	  --prefix=/usr --mandir=\$${prefix}/share/man \
	  --enable-static --enable-shared --disable-rpath \
	  --with-modules=dlopen \
	  --with-gimp --with-cups --with-ijs \
	  --with-foomatic --with-foomatic3 \
	  --disable-test \
	  --enable-cups-level3-ppds --disable-translated-ppds \
	  --disable-cups-level3-ps --disable-static-genppd \
	  --enable-maintainer-mode 
	touch autoconfigure-stamp


build: build-stamp debian/control
build-stamp: autoconfigure-stamp
	dh_testdir

	cd debian/build; \
	$(MAKE)

	touch build-stamp

clean: debian/control
	dh_testdir
	rm -f build-stamp autoconfigure-stamp

	-$(MAKE) distclean
	-test -r /usr/share/misc/config.sub && \
	  cp -f /usr/share/misc/config.sub scripts/config.sub
	-test -r /usr/share/misc/config.guess && \
	  cp -f /usr/share/misc/config.guess scripts/config.guess

	rm -rf debian/build debian/install
	rm -rf debian/libgimpprint.install debian/libgimpprint-doc.doc-base.developer debian/libgimpprint-doc.doc-base.reference debian/libgimpprint-doc.install
	rm -rf debian/libgimpprint$(LIBRARY_VERSION).files
	rm -rf debian/libgimpprint$(LIBRARY_VERSION).postinst
	-$(MAKE) distclean

	rm -rf $(CURDIR)/debian/libgimpprint*$(LIBRARY_VERSION)*
	dh_clean

install: DH_OPTIONS=
install: build debian/control
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	cd debian/build; \
	$(MAKE) DESTDIR=`pwd`/../install install

	dh_install


# Build architecture-independent files here.
binary-indep: build install ChangeLog
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -i
	dh_installexamples -i
	dh_installchangelogs -i NEWS
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install ChangeLog
	dh_testdir -a
	dh_testroot -a
	dh_installdocs -a
	  cp src/cups/README debian/cupsys-driver-gimpprint/usr/share/doc/cupsys-driver-gimpprint/README.cups
	  mv debian/cupsys-driver-gimpprint/usr/share/man/man8/cups-genppd.8 debian/cupsys-driver-gimpprint/usr/share/man/man8/cups-genppd.5.0.8
	  mv debian/cupsys-driver-gimpprint/usr/share/man/man8/cups-genppdconfig.8 debian/cupsys-driver-gimpprint/usr/share/man/man8/cups-genppdconfig.5.0.8
	  mv debian/cupsys-driver-gimpprint/usr/share/man/man8/cups-genppdupdate.8 debian/cupsys-driver-gimpprint/usr/share/man/man8/cups-genppdupdate.5.0.8
	dh_installexamples -a
	dh_installchangelogs -a NEWS
	dh_strip -a
	dh_link -a
	dh_compress -a
	dh_fixperms -a
# Strip DT_RPATH from CUPS binaries.
	find debian/cupsys-driver-gimpprint/usr/lib -type f -perm +0111 -print0 | xargs -0 chrpath --delete
	chrpath --delete debian/cupsys-driver-gimpprint/usr/sbin/cups-genppd.5.0
	dh_makeshlibs -a -V -X usr/lib/gimp-print
	dh_installdeb -a
	dh_shlibdeps -a -X usr/lib/gimp-print -l`pwd`/debian/libgimpprint$(LIBRARY_VERSION)/usr/lib
	dh_perl -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch

# Make sure ChangeLog is up-to-date.
ChangeLog: changelog-stamp
changelog-stamp: build
	$(MAKE) ChangeLog

SEDPATTERN = -e "s/\#UPSTREAM_VERSION\#/$(UPSTREAM_VERSION)/g" -e "s/\#LIBRARY_VERSION\#/$(LIBRARY_VERSION)/g"

# Make sure debian/control is up-to-date.
debian/control: debian/control.in debian/changelog
	sed $(SEDPATTERN) <$< >$@

# Make sure debian/libgimpprint-doc.doc-base is up-to-date.
debian/libgimpprint-doc.doc-base.developer: debian/libgimpprint-doc.doc-base.developer.in debian/changelog
	sed $(SEDPATTERN) <$< >$@

# Make sure debian/libgimpprint-doc.doc-base is up-to-date.
debian/libgimpprint-doc.doc-base.reference: debian/libgimpprint-doc.doc-base.reference.in debian/changelog
	sed $(SEDPATTERN) <$< >$@

# Make sure debian/libgimpprint-doc.install is up-to-date.
debian/libgimpprint.install: debian/libgimpprint.install.in debian/changelog
	sed $(SEDPATTERN) <$< >$@

# Make sure debian/libgimpprint-doc.install is up-to-date.
debian/libgimpprint-doc.install: debian/libgimpprint-doc.install.in debian/changelog
	sed $(SEDPATTERN) <$< >$@


# Make sure that configure is up-to-date.
configure: configure.ac scripts/install-sh
	if test -f configure.ac; then \
	  if test -f autogen.sh; then \
	    NOCONFIGURE=TRUE /bin/sh autogen.sh; \
	  fi \
	fi

scripts/install-sh:
	if test -f autogen.sh; then \
	  NOCONFIGURE=TRUE /bin/sh autogen.sh; \
	fi

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