#!/usr/bin/make -f
# rules to package gutenprint
#
# 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

# Use dpatch.
include /usr/share/dpatch/dpatch.make

# 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 libgutenprint suffix
UPSTREAM_VERSION = $(shell dpkg-parsechangelog | grep ^Version | sed "s/.* //" | sed "s/-[^-]*$$//")
#UPSTREAM_VERSION = 5.0.0
UPSTREAM_VERSION_COMPACT = $(shell echo '$(UPSTREAM_VERSION)' | sed "s/\(.*\..*\)\..*/\1/"
#LIBRARY_VERSION =  -$(UPSTREAM_VERSION)
LIBRARY_VERSION = 2
LIBRARYUI_VERSION = 1

GENERATED_FILES = \
	debian/libgutenprint2.install

autoconfigure: autoconfigure-stamp
autoconfigure-stamp: debian/control $(GENERATED_FILES)
	dh_testdir
	mkdir debian/build; \
	cd debian/build; \
	../../configure --host=$(DEB_HOST_GNU_TYPE) \
	  --build=$(DEB_BUILD_GNU_TYPE) \
	  --prefix=/usr --mandir=\$${prefix}/share/man \
	  --disable-static --enable-shared --disable-rpath \
	  --with-modules=dlopen \
	  --enable-libgutenprintui --enable-libgutenprintui2 \
	  --without-gimp --with-gimp2 --with-cups --with-ijs \
	  --with-foomatic --with-foomatic3 \
	  --disable-test \
	  --enable-cups-level3-ppds --disable-translated-cups-ppds \
	  --disable-cups-level3-ps --disable-static-genppd \
	  DIALOG=/usr/bin/dialog
	touch autoconfigure-stamp


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

	cd debian/build; \
	$(MAKE)

	touch build-stamp

clean: realclean unpatch
realclean: 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
	-$(MAKE) distclean

	rm -f $(GENERATED_FILES)

	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
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -i
	dh_installexamples -i
	dh_installchangelogs -i NEWS
	dh_link -i
	dh_compress -i -X.pdf -X.odt
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installdocs -a
	  cp src/cups/README debian/cupsys-driver-gutenprint/usr/share/doc/cupsys-driver-gutenprint/README.cups
	  mv debian/cupsys-driver-gutenprint/usr/share/man/man8/cups-genppd.8 debian/cupsys-driver-gutenprint/usr/share/man/man8/cups-genppd.5.0.8
	  mv debian/cupsys-driver-gutenprint/usr/share/man/man8/cups-genppdconfig.8 debian/cupsys-driver-gutenprint/usr/share/man/man8/cups-genppdconfig.5.0.8
	  mv debian/cupsys-driver-gutenprint/usr/share/man/man8/cups-genppdupdate.8 debian/cupsys-driver-gutenprint/usr/share/man/man8/cups-genppdupdate.5.0.8
	  mv debian/ijsgutenprint/usr/share/man/man1/ijsgutenprint.1 debian/ijsgutenprint/usr/share/man/man1/ijsgutenprint.5.0.1
	  ln -s ijsgutenprint.5.0.1.gz debian/ijsgutenprint/usr/share/man/man1/ijsgutenprint.1.gz
	  ln -s ijsgutenprint.5.0 debian/ijsgutenprint/usr/bin/ijsgutenprint
	dh_installexamples -a
	dh_installchangelogs -a NEWS
	dh_strip -a
	dh_link -a
	dh_compress -a -X.pdf -X.odt
	dh_fixperms -a
# Strip DT_RPATH from CUPS binaries.
	find debian/cupsys-driver-gutenprint/usr/lib -type f -perm +0111 -print0 | xargs -0 chrpath --delete
	chrpath --delete debian/cupsys-driver-gutenprint/usr/sbin/cups-genppd.5.0
	dh_makeshlibs -a -V -X usr/lib/gutenprint
	dh_installdeb -a
	dh_shlibdeps -a -X usr/lib/gutenprint -l$(CURDIR)/debian/libgutenprint$(LIBRARY_VERSION)/usr/lib:$(CURDIR)/debian/libgutenprintui1-$(LIBRARYUI_VERSION)/usr/lib:$(CURDIR)/debian/libgutenprintui2-$(LIBRARYUI_VERSION)/usr/lib
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch

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

debian/control: debian/control.in debian/changelog debian/rules
	sed $(SEDPATTERN) <$< >$@

debian/libgutenprint2.install: debian/libgutenprint2.install.in debian/changelog
	sed $(SEDPATTERN) <$< >$@

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