#!/usr/bin/make -f
# Made with the aid of dh_make, by Craig Small
# Sample debian/rules that uses debhelper. GNU copyright 1997 by Joey Hess.
# Some lines taken from debmake, by Cristoph Lameter.

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

A := $(wildcard apache-*)
P := $(wildcard libapache-mod-perl-*)

APACHE_VER := $(shell echo $(A) | sed "s/apache-//")
NEXT_VER   := $(shell echo $(APACHE_VER) | awk -F. '{OFS="."; $$NF = $$NF + 1; print}')

MP_VER     := $(shell echo $(P) | sed "s/libapache-mod-perl-//")
NMP_VER    := $(shell echo $(MP_VER) | awk -F. '{OFS="."; $$NF = $$NF + 1; print}')

ABLD := $(A)/build-tree/apache_$(APACHE_VER)

# This mess only has the desired result if the latest perl is installed.
ifndef PERL
  PERL = /usr/bin/perl
endif

archlib = debian/tmp$(shell $(PERL) -MConfig -e 'print "$$Config{installvendorarch}";')

PERL_DEPEND_VERSION := $(shell $(PERL) $(P)/debian/perl_version)

build: build-stamp
build-stamp:
	dh_testdir

# Disabled until EAPI_MM is fixed
#		   export EAPI_MM=SYSTEM &&
	$(MAKE) -C $(A) -f debian/rules \
	  CONFCMD="cat $(shell pwd)/debian/config.layout.mp >> \
		     $(shell pwd)/$(ABLD)/config.layout && \
		   cd $(shell pwd)/$(P) && \
		   export CFLAGS=\"-I/usr/include/db1\" && \
		   export LIBS=\"-ldb1\" && \
		   $(PERL) Makefile.PL INSTALLDIRS=vendor \
		   USE_DSO=0 USE_APACI=1 DO_HTTPD=1 \
		   APACHE_SRC=$(shell pwd)/$(A)/\$$(B)/src \
		   APACI_ARGS=\"\$$(subst target=apache,target=apache-perl, \
		     \$$(subst layout=Debian,layout=DebianModPerl,\$$(CONFARGS))) \
		   --enable-rule=EAPI --disable-shared=perl\" \
		   PERL_SECTIONS=1 PERL_SSI=1 PERL_METHOD_HANDLERS=1 ALL_HOOKS=1 \
		   PERL_TIE_TABLES=1 PERL_DIRECTIVE_HANDLERS=1 PERL_STACKED_HANDLERS=1 \
		   PERL_TRACE=0 PERL_LOG_API=1 PERL_URI_API=1 PERL_UTIL_API=1 \
		   PERL_TABLE_API=1 PERL_FILE_API=1" \
		build					# PERL_DEBUG=1
	$(MAKE) -C $(P)
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	dh_clean

	rm -f debian/apache-perl.init

	# In case I forgot when making the package.
	cp debian/ubersed $(A)/debian/ubersed

	(cd $(P) && $(MAKE) -f debian/rules clean)
	(cd $(A) && $(MAKE) -f debian/rules clean)

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

# Build architecture-dependent files here.
binary-arch: build
	dh_testdir
	dh_testroot
	dh_clean -k

	dh_installdirs usr/sbin etc/cron.daily etc/init.d \
		var/cache/apache-perl var/log/apache-perl \
		etc/apache-perl usr/share/man/man8
	install -m 755 $(ABLD)/src/apache-perl debian/tmp/usr/sbin/
	chmod +x debian/ubersed
	debian/ubersed < $(ABLD)/src/support/apachectl \
		> debian/tmp/usr/sbin/apache-perl-ctl
	chmod +x debian/tmp/usr/sbin/apache-perl-ctl

	debian/ubersed < $(ABLD)/src/support/apachectl.8 \
		> debian/tmp/usr/share/man/man8/apache-perl-ctl.8

	debian/ubersed < $(ABLD)/src/support/httpd.8 \
		> debian/tmp/usr/share/man/man8/apache-perl.8

	cat $(A)/debian/cron.daily \
		| sed 's,/apache,/apache-perl,g' \
		> debian/tmp/etc/cron.daily/apache-perl
	chmod 755 debian/tmp/etc/cron.daily/apache-perl

	cp -f debian/extra-init.d debian/apache-perl.init
	cat $(A)/debian/init \
		| sed	-e 's,^NAME=.*,NAME=apache-perl,' \
			-e 's,^DAEMON=.*,DAEMON=/usr/sbin/apache-perl,' \
			-e 's,^CONF=.*,CONF=/etc/apache-perl/httpd.conf,' \
			-e 's,^APACHECTL=.*,APACHECTL=/usr/sbin/apache-perl-ctl,' \
			-e 's,/var/run/apache.pid,$$PIDFILE,' \
			-e '/^test -f $$DAEMON || exit 0$$/ i\' \
			-e  'test -f $$CONF || exit 0' \
		>> debian/apache-perl.init
	dh_installinit -a -n

	# Backwards compatibility, in case the existing config files
	# reference it.  The examples reference /etc/mime.types instead.
	ln -s ../mime.types debian/tmp/etc/apache-perl/mime.types
	install -m 644 debian/cron.conf debian/tmp/etc/apache-perl/cron.conf

	dh_installdebconf
	dh_installdocs
	dh_installexamples debian/doc/*
	dh_installchangelogs

	dh_strip
	dh_compress -a \
                --exclude=httpd.conf --exclude=access.conf --exclude=srm.conf
	dh_fixperms
	dh_shlibdeps

	dh_gencontrol -- -VTHISAPACHE="$(APACHE_VER)" \
		-VNEXTAPACHE="$(NEXT_VER)" \
		-VTHISMODPERL="$(MP_VER)" \
		-VNEXTMODPERL="$(NMP_VER)" \
		-VPERLDEPEND="$(PERL_DEPEND_VERSION)"
	dh_md5sums
	dh_installdeb
	dh_builddeb

source diff:                                                                  
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

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