#!/usr/bin/make -f
# -*- makefile -*-

# Order of patches is important. Hideous, huh?

debian_patches =

export DH_COMPAT=3

build: build-stamp
build-stamp: config-stamp
	dh_testdir
	$(MAKE) -j 2
	touch build-stamp

clean: reverse-patches
	dh_testdir
	dh_testroot
	dh_clean *-stamp debian/shlibs.local \
	xml-i18n-extract xml-i18n-merge xml-i18n-update

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

config: config-stamp
config-stamp: patched-stamp
	dh_testdir

	srcdir=`pwd` PKG_NAME="gnome-control-center" ./autogen.sh \
	--prefix=/usr --localstatedir=/var/lib --sysconfdir=/etc \
	--mandir=/usr/share/man --datadir=/usr/share

	touch config-stamp

install: install-stamp
install-stamp: build-stamp
	dh_testdir
	dh_installdirs
	$(MAKE) install DESTDIR=`pwd`/debian/gnome-control-center

	touch install-stamp

arrange: arrange-stamp
arrange-stamp: install-stamp
	dh_movefiles --sourcedir=debian/gnome-control-center

# Prune empty directories
	-find debian -type d | sort -r | xargs rmdir --ignore-fail-on-non-empty
	touch arrange-stamp

#	mv debian/gnome-control-center/usr/share/control-center/capplets/Gnome.directory \
#	debian/gnome-control-center/usr/share/control-center/capplets/.directory

binary: binary-stamp
binary-stamp: binary-arch-stamp binary-indep-stamp

binary-arch-stamp: binary-arch
binary-arch: arrange-stamp
	dh_testdir
	dh_testroot
	dh_installdocs
#	dh_installexamples
	dh_installmenu
#	dh_installemacsen
#	dh_installinit
#	dh_installcron
#	dh_installman
	dh_undocumented
	dh_installchangelogs ChangeLog
	rm -rf debian/libconfig-archiver-dev/usr/share/doc/libconfig-archiver-dev
	dh_strip
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_makeshlibs -V 'libconfig-archiver0 (>= 1:1.5.2)'
	dh_shlibdeps -ldebian/libconfig-archiver0/usr/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-indep-stamp: binary-indep
binary-indep: arrange-stamp
	dh_testdir

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

# ---------------------------------------------------------------------------
# various rules to unpack addons and (un)apply patches.
# borrowed from egcs package

patch_dir	= debian/patches

apply-patches: patched-stamp
reverse-patches:
	@for stamp in none patched-*; do \
	  case "$$stamp" in none|patched-stamp|patched-\*) continue; esac; \
	  patch=`echo $$stamp | sed -e 's/patched-//'`; \
	  echo "trying to revert patch $$patch ..."; \
	  if [ -x $(patch_dir)/$$patch.dpatch ]; then true; else \
	    chmod +x $(patch_dir)/$$patch.dpatch; fi; \
	  if $(patch_dir)/$$patch.dpatch -unpatch; then \
	    echo "reverted $$patch patch."; \
	    rm -f $$stamp; \
	  else \
	    echo "error in reverting $$patch patch."; \
	    exit 1; \
	  fi; \
	done
	rm -f patched-stamp

patched-%: $(patch_dir)/%.dpatch
	@if [ -x $< ]; then true; else chmod +x $<; fi
	@if [ -f $@ ]; then \
	  echo "$* patches already applied."; exit 1; \
	fi
	$< -patch
	@echo "$* patches applied." > $@

patched-stamp: $(foreach p,$(debian_patches),patched-$(p))
	@echo -e "\nPatches applied:" >> pxxx
	@for i in none $(debian_patches); do \
	  if [ -r debian/patches/$$i.dpatch ]; then \
	    echo -e "\n$$i:" >> pxxx; \
	    sed -n 's/^# *DP: */  /p' debian/patches/$$i.dpatch >> pxxx; \
	  fi \
	done
	mv -f pxxx patched-stamp
