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

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# We use debian/compat instead
#export DH_COMPAT=3

DDEBUG=`if (echo $(DEB_BUILD_OPTIONS) | grep -q debug) then \
			echo "--enable-debug"; \
		else \
			echo ""; \
		fi`

build: debfiles-stamp build-stamp
build-stamp:
	dh_testdir

#	automake -a
#	autoreconf
	pythonver=`dpkg -s python-extclass|grep Depends|sed 's/^.*\(python[^ ]*\)-extclass .*/\/usr\/bin\/\1/'`
	./configure \
		--prefix=/usr \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		$(DDEBUG) \
		--sysconfdir=/etc \
		--enable-tproxy=netfilter\
		--with-python=$pythonver
	cd modules; make config-stamp
	# Add here commands to compile the package.
	$(MAKE)

	touch build-stamp

debfiles-stamp:
	echo -n "" > debian/zorp-modules.files
	echo -n "" > debian/zorp-modules.conffiles
	for i in `cat module.list` x; do \
		if test "$$i" != "x"; then \
			if test -r modules/$$i/debian/zorp-pro-modules.files; then \
				cat modules/$$i/debian/zorp-pro-modules.files >> debian/zorp-modules.files; \
			fi; \
			if test -r modules/$$i/debian/zorp-pro-modules.conffiles; then \
				cat modules/$$i/debian/zorp-pro-modules.conffiles >> debian/zorp-modules.conffiles; \
			fi; \
		fi; \
	done;
	rm debian/zorp-modules.conffiles
	# this is a hack to make it compile on potato (though no config files will be tagged as such)
	if [ "`dpkg -s debhelper | grep ^Version:`" == "Version: 2.0.86" ]; then \
		echo 2 >debian/compat; \
	fi
	touch debfiles-stamp

clean: debfiles-stamp
	dh_testdir
	dh_testroot
	rm -f build-stamp
	rm -f modules/config-stamp
	rm -d debian/zorp-modules.files
	rm -f debfiles-stamp

	# Add here commands to clean up after the build process.
#	-$(MAKE) distclean
#	find . \( -name config.log -o -name config.status -o -name config.cache \) -print0 |xargs -0r rm -f
#	-find \( -name ltmain.sh -o -name config.h.in -o -name stamp-h.in \
#	-o -name config.cache -o -name config.log -o -name config.status\
#	-o -name stamp-h -o -name config.sub -o -name config.guess \
#	-o -name Makefile.in -o -name aclocal.m4 -o -name configure \) -print0|grep -z -v ++pristine |xargs -0r rm
#	-find -name autom4te.cache -print0|grep -z -v ++pristine |xargs -r0 rm -rf
#	-find -type l -print0|grep -z -v ++pristine |xargs -0r rm
#	-rm -f modules/zorp-module-http-2.0.0/tests/Makefile zorpconfig.h.in

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Add here commands to install the package into debian/tmp.
	$(MAKE) install DESTDIR=`pwd`/debian/tmp/
	install -o root -g root -m 600 debian/instances.conf `pwd`/debian/tmp/etc/zorp
	
	dh_movefiles
	

# 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
#-k ChangeLog
	dh_link -i
	dh_strip -i
	dh_compress -i
	dh_fixperms -i
	if test $(DH_COMPAT) -eq 3; then \
		dh_python -i /usr/share/zorp; \
	fi;
	dh_installdeb -i
	dh_shlibdeps -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
	rm debian/zorp/usr/share/doc/zorp/INSTALL
	dh_installinit --package=zorp
	if test $(DH_COMPAT) -eq 3; then \
		dh_installman --package=zorp doc/man/zorp.8 doc/man/zorpctl.8 doc/man/instances.conf.5; \
		dh_installman --package=libzorp2-dev doc/man/zorp-config.1; \
	fi;
	dh_installchangelogs -a
#-k ChangeLog
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	# You may want to make some executables suid here.
	chmod 700 `pwd`/debian/zorp/etc/zorp
	mkdir -p `pwd`/debian/zorp/usr/share/lintian/overrides
	cp debian/zorp.overrides.lintian debian/zorp/usr/share/lintian/overrides/zorp
	mkdir -p debian/zorp/usr/share/linda/overrides
	cp debian/zorp.overrides.linda debian/zorp/usr/share/linda/overrides/zorp
	dh_makeshlibs -n
	dh_makeshlibs -plibzorp2 -V
	if test $(DH_COMPAT) -eq 3; then \
		dh_python -a /usr/share/zorp; \
		dh_python -p zorp-modules; \
	fi;
	dh_installdeb -a
	if test $(DH_COMPAT) -eq 3; then \
		dh_shlibdeps -a -Llibzorp2 -l`pwd`/debian/libzorp2/usr/lib; \
	else \
		dh_shlibdeps -a -l`pwd`/debian/libzorp2/usr/lib; \
	fi;
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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