#!/usr/bin/make -f

i=$(shell pwd)/debian/tmp
b=$(shell pwd)/debian/build

debian/rules: debian/*.in debian/debconfigure
	@echo "--- Running debconfigure"
	cd debian ; chmod +x debconfigure ; ./debconfigure

configure: patched-stamp configure.in
	@echo "--- Making configure script and configuring"
	chmod +x autogen.sh
	./autogen.sh --prefix=/@PREFIX@ --with-gtk-prefix=/@GTKPREFIX@ \
	  --sysconfdir=/@SYSCONFDIR@

Makefile: configure
	@echo "--- Configuring"
	./configure --prefix=/@PREFIX@ --with-gtk-prefix=/@GTKPREFIX@ \
	  --sysconfdir=/@SYSCONFDIR@

build: configure Makefile build-debstamp
build-debstamp:
	@echo "--- Compiling"
	dh_testdir
ifeq (@DEBTYPE@,release)
	sed < libtool > libtool-2 \
	  -e 's/^hardcode_libdir_flag_spec.*$$/hardcode_libdir_flag_spec=" -D__LIBTOOL_IS_A_FOOL__ "/' \
	  -e '/^archive_cmds="/s/"$$/ \\$$deplibs"/'
	mv libtool-2 libtool
endif
	$(MAKE) all
	touch build-debstamp

clean: configure Makefile
	@echo "--- Cleaning"
	dh_testdir
	-rm -f build-debstamp install-debstamp
	$(MAKE) -f debian/rules reverse-patches
	-$(MAKE) distclean
	-rm -f `find . -name "*~"`
	-rm -rf `find . -name "\.deps"`
	-rm -rf `find . -name "\.libs"`
	-rm -rf `find . -name "*\.rej"`
	-find . -name 'Makefile.in' | xargs rm -f
	-find . -type l | xargs rm -f
	-rm -f aclocal.m4 config.guess config.h.in config.sub configure \
		ltconfig ltmain.sh
	-cd macros; rm -f macros.dep
	-rm -rf debian/tmp `find debian/* -type d ! -name CVS ! -name patches` debian/files* core
	-rm -f debian/*substvars

install: build install-debstamp-virgin

install-debstamp install-debstamp-virgin: build-debstamp
	@echo "--- Installing"
	dh_testdir
	dh_testroot
	dh_clean
	rm -rf $(b)
	$(MAKE) install prefix=$(i)/@PREFIX@ exec_prefix=$(i)/@PREFIX@ \
	  localstatedir=$(i)/@LOCALSTATEDIR@ sysconfdir=$(i)/@SYSCONFDIR@
	find $(i) -name '*.la' | xargs rm -f
	touch install-debstamp
	touch install-debstamp-virgin

install-save: install-debstamp-virgin
	rm -rf $(i).saved
	cp -a $(i) $(i).saved

install-saved:
	rm -rf $(i)
	cp -a $(i).saved $(i)
	rm -rf $(b)
	touch install-debstamp
	touch install-debstamp-virgin

binary-indep: install

binary-arch: install \
		@SNAP@libghttp1 \
		@SNAP@libghttp-dev


#
# libghttp1
#

@SNAP@libghttp1: install-debstamp
	@echo "--- Building: $@"
	rm -f install-debstamp-virgin
	dh_installdocs       -p$@ -P$(b)/$@ \
					README NEWS AUTHORS TODO
	dh_installchangelogs -p$@ -P$(b)/$@ \
					ChangeLog
	dh_movefiles         -p$@ -P$(b)/$@
	dh_strip             -p$@ -P$(b)/$@ 
	dh_compress          -p$@ -P$(b)/$@ 
	dh_fixperms          -p$@ -P$(b)/$@ 
	dh_installdeb        -p$@ -P$(b)/$@
	dh_shlibdeps         -p$@ -P$(b)/$@
	dh_gencontrol        -p$@ -P$(b)/$@
	dh_makeshlibs        -p$@ -P$(b)/$@ -V
	dh_md5sums           -p$@ -P$(b)/$@
	dh_builddeb          -p$@ -P$(b)/$@


#
# libghttp-dev
#

@SNAP@libghttp-dev: install-debstamp
	@echo "--- Building: $@"
	rm -f install-debstamp-virgin
	dh_installdocs       -p$@ -P$(b)/$@ \
					README NEWS AUTHORS TODO \
					doc/ghttp.html
	dh_installchangelogs -p$@ -P$(b)/$@ \
					ChangeLog
	dh_movefiles         -p$@ -P$(b)/$@
	dh_compress          -p$@ -P$(b)/$@ 
	dh_fixperms          -p$@ -P$(b)/$@ 
	dh_installdeb        -p$@ -P$(b)/$@
	dh_shlibdeps         -p$@ -P$(b)/$@
	dh_gencontrol        -p$@ -P$(b)/$@
	dh_md5sums           -p$@ -P$(b)/$@
	dh_builddeb          -p$@ -P$(b)/$@


binary: binary-indep binary-arch

.PHONY: binary clean binary-indep binary-arch build install install-save install-saved

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

patch_dir	= debian/patches

# which patches should be applied?
#debian_patches = 

ifeq (@DEBTYPE@,release)
	debian_patches += autogen
endif

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
