#!/usr/bin/make -f

#export DH_VERBOSE=1

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

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

CFLAGS = -Wall -g

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

ifeq ($(DEB_BUILD_GNU_TYPE),alpha-linux)
	CFLAGS += -mieee
endif

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

	./configure --prefix=/usr --mandir=\$${prefix}/share/man \
	--infodir=\$${prefix}/share/info --host=$(DEB_HOST_GNU_TYPE) \
	--build=$(DEB_BUILD_GNU_TYPE) --disable-gtk-doc CFLAGS="$(CFLAGS)"

	touch configure-stamp

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

	$(MAKE)

	touch build-stamp

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

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

	debian/rules unpatch

	dh_clean docs/reference/*.txt docs/reference/sgml/libgnomecanvas-doc.bottom \
	docs/reference/sgml/libgnomecanvas-doc.top libgnomecanvas/gnome-canvas-marshal.c \
	libgnomecanvas/gnome-canvas-marshal.h

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs -plibgnomecanvas2-doc usr/share/doc/libgnomecanvas2-doc/html

	$(MAKE) install prefix=$(CURDIR)/debian/${version}/usr

	dh_movefiles --sourcedir=debian/${version}

# Remove uneeded a and la files
	find debian/${version}/usr/lib -name *.a | xargs rm
	find debian/${version}/usr/lib -name *.la | xargs rm

	mv debian/libgnomecanvas2-0/usr/share/gtk-doc/html/libgnomecanvas/* \
	debian/libgnomecanvas2-doc/usr/share/doc/libgnomecanvas2-doc/html

	find debian -type d | xargs rmdir -p --ignore-fail-on-non-empty

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -i
	dh_installchangelogs -i ChangeLog
	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
	dh_testdir -a
	dh_testroot -a
	dh_installdocs -a
	dh_installchangelogs -a ChangeLog
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a -V 'libgnomecanvas2-0 (>= 2.1.90)'
	dh_installdeb -a
	dh_shlibdeps -ldebian/${version}/usr/lib
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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

DPATCH_ARCH	 = $(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 -f patch-stamp 
