#!/usr/bin/make -f

# export DH_VERBOSE=1

# GNOME policy
export GCONF_DISABLE_MAKEFILE_SCHEMA_INSTALL=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 (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

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

	#in patches/05_libtool
	#libtoolize --copy --force
	#aclocal-1.9
	#autoconf

	CPPFLAGS=-I/usr/include/libpng12 ./configure \
	--host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) \
	--prefix=/usr --mandir=\$${prefix}/share/man --sysconfdir=/etc \
	--datadir=\$${prefix}/share CFLAGS="$(CFLAGS)"

# To remove rpath warning with lintian
	perl -i -pe 's/^(hardcode_libdir_flag_spec\s*=\s*).+$$/$$1" -D__LIBTOOL_IS_A_FOOL__ "/' libtool


# Big hack to read locale file in the right plcae
	perl -i -pe 's,/usr/lib/locale,/usr/share/locale,' config.h

	touch configure-stamp

build: build-stamp
build-stamp: configure-stamp
	dh_testdir
	cd po; intltool-update -p

	$(MAKE)

	touch build-stamp

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

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

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

	debian/rules unpatch

	dh_clean po/.intltool-merge-cache intltool-extract intltool-merge \
	intltool-update doc/C/gthumb-C.omf.out components/catalog-view/GNOME_GThumb_CatalogView.xml \
	components/image-viewer/GNOME_GThumb_Viewer.xml po/de.gmo

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(MAKE) install DESTDIR=$(CURDIR)/debian/gthumb
	cp debian/gthumb.xpm debian/gthumb/usr/share/pixmaps

	rm debian/gthumb/usr/lib/gthumb/modules/*.a
	rm debian/gthumb/usr/lib/gthumb/modules/*.la
	rm debian/gthumb/usr/lib/libgthumb.a debian/gthumb/usr/lib/libgthumb.la
	rm -rf debian/gthumb/usr/share/application-registry
	rm -rf debian/gthumb/usr/var
	rm -rf debian/gthumb/var/lib/scrollkeeper

	# Add translation domain to .desktop and .server files
	DOMAIN=$$(grep --max-count 1 '^GETTEXT_PACKAGE[[:space:]]*=' $(CURDIR)/po/Makefile | sed 's/^.*=[[:space:]]\([^[:space:]]\)/\1/'); \
	for d in $$(find debian/gthumb -type f -name "*.desktop" ); do \
		echo "Adding translation domain $$DOMAIN to $$d..."; \
		echo "X-Ubuntu-Gettext-Domain=$$DOMAIN" >> $$d; \
	done; \
	for d in $$(find debian/gthumb -type f -name "*.server" ); do \
		echo "Adding translation domain $$DOMAIN to $$d..."; \
		sed -i "s/<oaf_server\>/<oaf_server ubuntu-gettext-domain=\"$$DOMAIN\"/" $$d; \
	done; \

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs README NEWS
	dh_installexamples
	dh_installmenu
	dh_installman
	dh_installinfo
	dh_installchangelogs ChangeLog
	dh_scrollkeeper
	dh_desktop
	dh_icons
	dh_gconf
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	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

DEB_BUILD_ARCH	?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)
DPATCH_ARCH	:= ${DEB_BUILD_ARCH}
PATCHLIST_ALL	 = $(shell test -f debian/patches/00list && grep -v ^\# debian/patches/00list)
PATCHLIST_ARCH	 = $(shell test -f debian/patches/00list.${DPATCH_ARCH} && grep -v ^\# debian/patches/00list.${DPATCH_ARCH})
PATCHLIST	?= $(shell echo ${PATCHLIST_ALL} ${PATCHLIST_ARCH})

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

# Expand them right now
PATCHLIST	:= ${PATCHLIST}
UNPATCHLIST	:= ${UNPATCHLIST}

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 \
		patchdir=$${patch%/*}; \
		test "x$$patchdir" = "x$$patch" || \
			install -d debian/patched/$$patchdir; \
		stamp=debian/patched/$${patch%%.dpatch}.dpatch; \
		test -e debian/patches/$$patch || ext=.dpatch; \
		patch=debian/patches/$$patch$$ext; \
		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}.dpatch; \
		test -e debian/patches/$$patch || ext=.dpatch; \
		patch=debian/patches/$$patch$$ext; \
		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 patch-stampT debian/patched
