#!/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}')

# 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{installarchlib}";')

USR_BIN_PERL_VER := $(shell /usr/bin/perl -e 'printf "%.3f", $$]')
DOLLAR_PERL_VER  := $(shell $(PERL) -e 'printf "%.3f", $$]')

ifeq ($(DOLLAR_PERL_VER),$(USR_BIN_PERL_VER))
  PERL_PACKNAME =
else
  PERL_PACKNAME = -$(PERL_VER)
endif

build: build-stamp
build-stamp:
	dh_testdir

	if [ -f $(P)/apaci/mod_perl.config -a '!' -f $(P)/apaci/mod_perl.config.backup ]; \
	then \
	  echo 'Preserving mod_perl.config ...'; \
	  cp $(P)/apaci/mod_perl.config $(P)/apaci/mod_perl.config.backup; \
	fi

	$(MAKE) -C $(A) -f debian/rules \
	  CONFCMD="cd $(shell pwd)/$(P) && $(PERL) Makefile.PL \
		   USE_DSO=0 USE_APACI=1 DO_HTTPD=1 \
		   APACHE_SRC=$(shell pwd)/$(A)/\$$(B)/src \
		   APACI_ARGS=\"\$$(CONFARGS)\" \
		   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" \
		EXTRA_CONFARGS="--disable-shared=perl" \
		build
	$(MAKE) -C $(P)
	touch build-stamp

debian/control: debian/control.in
	sed -e 's/THISAPACHE/$(APACHE_VER)/g' -e 's/NEXTAPACHE/$(NEXT_VER)/g' \
	    -e 's/PERL_PACKNAME/$(PERL_PACKNAME)/g' \
	    -e 's/PERLVER/$(DOLLAR_PERL_VER)/g' \
		< debian/control.in > debian/control

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	dh_clean

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

	rm -f debian/control
	$(MAKE) -f debian/rules debian/control

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

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

	# Add here commands to install the files into debian/tmp
	$(MAKE) -C $(A) -f debian/rules binary-pre-arch

	cp -a $(A)/debian/tmp debian/tmp

	$(MAKE) -C $(P) PREFIX=`pwd`/debian/tmp/usr \
		INSTALLMAN3DIR=`pwd`/debian/tmp/usr/share/man/man3 \
		INSTALLMAN1DIR=`pwd`/debian/tmp/usr/share/man/man1 install_perl

	find `pwd`/debian/tmp \( -name .packlist -o -name perllocal.pod \
	  -o -name .cvsignore \) -exec rm -f '{}' \;

	dh_installdocs
	dh_installchangelogs

	mv debian/tmp/usr/share/doc/apache debian/tmp/usr/share/doc/apache-perl/apache
	mkdir debian/tmp/usr/share/doc/apache-perl/modperl
	cp -a $(P)/eg/ debian/tmp/usr/share/doc/apache-perl/modperl/examples
	cp $(P)/Changes debian/tmp/usr/share/doc/apache-perl/modperl/changelog
	cp $(P)/ToDo debian/tmp/usr/share/doc/apache-perl/modperl/TODO
	cp $(P)/README debian/tmp/usr/share/doc/apache-perl/modperl/README
	cp $(P)/debian/copyright debian/tmp/usr/share/doc/apache-perl/modperl/copyright
	cp $(P)/debian/changelog debian/tmp/usr/share/doc/apache-perl/modperl/changelog.Debian

	# Why does this happen?
	if [ -f $(archlib)/auto/Apache/include/include/ap_config_auto.h ]; then	\
	  mv $(archlib)/auto/Apache/include/include/ap_config_auto.h		\
	    $(archlib)/auto/Apache/include/ap_config_auto.h;			\
	fi

	# Remove other headers, leaving modules/perl/*.h and ap_config_auto.h
	find $(archlib) -path '*/modules/perl' -prune -o -name ap_config_auto.h -prune -o \
	 \( -type f -name \*.h -o -name os-inline.c \) -exec rm -f '{}' ';'

	find `pwd`/debian/tmp -type f -name mod_perl.exp -exec rm '{}' ';'
	find `pwd`/debian/tmp -type f \( -name \*.pm -o -name \*.pl -o -name \*.h \) \
	  -exec chmod a-x '{}' \;

	# Clean up empty directories.  Did I mention that find was evil?
	for d in `find $(archlib) -type d -empty -print`; do \
		rmdir --ignore-fail-on-non-empty --parents $$d; \
	done

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

	# Placed down here so we don't get clobbered by debhelper's opinion
	# of what we ought to do for our own documentation.
	cp -f $(A)/debian/tmp/DEBIAN/{pre,post}{inst,rm} debian/tmp/DEBIAN/

	sed 's#doc/apache#doc/apache-perl#g' \
		< debian/tmp/DEBIAN/postinst > debian/tmp/DEBIAN/postinst.tmp
	sed 's#doc/apache#doc/apache-perl#g' \
		< debian/tmp/DEBIAN/prerm > debian/tmp/DEBIAN/prerm.tmp
	mv -f debian/tmp/DEBIAN/postinst.tmp debian/tmp/DEBIAN/postinst
	mv -f debian/tmp/DEBIAN/prerm.tmp debian/tmp/DEBIAN/prerm
	chmod 755 debian/tmp/DEBIAN/{pre,post}{inst,rm}

	sed 's#doc/apache#doc/apache-perl/apache#g' \
		< debian/tmp/usr/sbin/apacheconfig > debian/tmp/usr/sbin/apacheconfig.tmp
	mv -f debian/tmp/usr/sbin/apacheconfig.tmp debian/tmp/usr/sbin/apacheconfig
	chmod 755 debian/tmp/usr/sbin/apacheconfig

	dh_gencontrol
	dh_md5sums
	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
