#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

#export DH_VERBOSE=1

version := $(shell sed -n 's/Package: \(.*\)/\1/p' debian/control | head -1)

DEB_HOST_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

configure: configure-stamp
configure-stamp: patch-stamp
	dh_testdir

# Remove URL DTD in the documentation
	find -name *.xml -exec perl -i -pe 's,http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd,/usr/share/sgml/docbook/dtd/xml/4.1.2/docbookx.dtd,' {} \;
	find -name *.xml -exec perl -i -pe 's,http://basil.ireland.sun.com:8080/docbook/docbookx.dtd,/usr/share/sgml/docbook/dtd/xml/4.1.2/docbookx.dtd,' {} \;
	find -name *.omf -exec perl -i -pe 's,http://scrollkeeper.sourceforge.net/dtds/scrollkeeper-omf-1.0/scrollkeeper-omf.dtd,/usr/share/xml/scrollkeeper/dtds/scrollkeeper-omf.dtd,' {} \;

	./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var \
	--host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) \
	--libexecdir=\$${prefix}/lib/gnome-panel

	touch configure-stamp

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

	$(MAKE) -j 2

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	[ ! -f Makefile ] || $(MAKE) distclean

	debian/rules unpatch

	-find -name *.server | xargs -r rm
	-find -name *.server.in | xargs -r rm
	-find -name Makefile | xargs -r rm

	find -name *.xml -exec perl -i -pe 's,/usr/share/sgml/docbook/dtd/xml/4.1.2/docbookx.dtd,http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd,' {} \;
	find -name *.xml -exec perl -i -pe 's,/usr/share/sgml/docbook/dtd/xml/4.1.2/docbookx.dtd,http://basil.ireland.sun.com:8080/docbook/docbookx.dtd,' {} \;
	find -name *.omf -exec perl -i -pe 's,/usr/share/xml/scrollkeeper/dtds/scrollkeeper-omf.dtd,http://scrollkeeper.sourceforge.net/dtds/scrollkeeper-omf-1.0/scrollkeeper-omf.dtd,' {} \;

	dh_clean intltool-extract intltool-merge intltool-update \
	po/.intltool-merge-cache omf-install/*.omf gkb-new/gkb_xmmap

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs usr/share/lintian/overrides usr/share/man/man1

	GCONF_DISABLE_MAKEFILE_SCHEMA_INSTALL=1 \
	$(MAKE) install prefix=$(CURDIR)/debian/${version}/usr \
	sysconfdir=$(CURDIR)/debian/${version}/etc

# remove broken omf files
	#rm debian/gnome-applets/usr/share/omf/gnome-applets/gweather_applet-it.omf

# Install lintian override file
	cp debian/lintian debian/${version}/usr/share/lintian/overrides/${version}

	chmod a-x debian/${version}/usr/share/gnome/help/gtik2_applet2/C/legal.xml
	chmod a-x debian/${version}/usr/share/gnome/help/gtik2_applet2/C/gtik2_applet2.xml

	cp man/gkb_xmmap.1 debian/${version}/usr/share/man/man1

# Build architecture-independent files here.
binary-indep: build install

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs AUTHORS
#	dh_installmenu
#	dh_installman *.1
	dh_installchangelogs ChangeLog
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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

DPATCH_ARCH	 = $(shell dpkg-architecture -qDEB_BUILD_ARCH)
PATCHLIST_ALL	 = $(shell cat debian/patches/00list)
PATCHLIST_ARCH	 = $(shell cat debian/patches/00list.${DPATCH_ARCH} 2>/dev/null)
PATCHLIST	?= $(sort ${PATCHLIST_ALL} ${PATCHLIST_ARCH})

UNPATCHLIST	 = $(shell echo ${PATCHLIST} | tr ' ' '\n' | tac)

patch: patch-stamp
patch-stamp:
	test -d debian/patched || install -d debian/patched
	@echo "Patches applied in the Debian version of ${PACKAGE}:" > $@T
	@for patch in ${PATCHLIST}; do \
		stamp=debian/patched/$$patch.dpatch; \
		patch=debian/patches/$$patch.dpatch; \
		author=`sed -n "s,^#* *.*dpatch by *,,p" $$patch`; \
		test -x $$patch || chmod +x $$patch; \
		if test -f $$stamp; then \
			echo "$$patch already applied."; \
			echo -e "\n$$patch ($$author):" >> $@T; \
			sed -n 's/^#* *DP: */  /p' $$patch >> $@T; \
		else \
			echo -n "applying patch $$patch..."; \
			if $$patch -patch >$$stamp.new 2>&1; then \
				mv $$stamp.new $$stamp; \
				touch $$stamp; \
				echo -e "\n$$patch ($$author):" >> $@T; \
				sed -n 's/^#* *DP: */  /p' $$patch >> $@T; \
				echo " ok."; \
			else \
				echo " failed."; \
				exit 1; \
			fi; \
		fi; \
	done
	mv -f $@T $@

unpatch:
	@for patch in ${UNPATCHLIST}; do \
		stamp=debian/patched/$$patch.dpatch; \
		patch=debian/patches/$$patch.dpatch; \
		test -x $$patch || chmod +x $$patch; \
		if test -f $$stamp; then \
			echo -n "reverting patch $$patch..."; \
			if $$patch -unpatch 2>&1 >/dev/null; then \
				rm -f $$stamp; \
				echo " ok."; \
			else \
				echo " failed."; \
				exit 1; \
			fi; \
		fi; \
	done
	rm -rf patch-stamp debian/patched
