#!/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

DEV_DPKG_VERSION := $(shell dpkg -s apache-dev | awk '/Version:/ {print $2}')
DEV_VERSION := $(shell echo $(DEV_DPKG_VERSION) | sed "s/^Version: \(.*\)-.*$$/\1/")
# (From the egcs packaging)
NEXTVERNO := $(shell echo $(DEV_VERSION) | 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

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

PERL_DEPEND = perl-$(DOLLAR_PERL_VER)

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

ifneq ($(wildcard /usr/lib/libperl5.so.*.*),)
  LIBPERL := -lperl5
else
  LIBPERL := DEFAULT
endif

build: build-stamp
build-stamp:
	dh_testdir

# This file isn't in the CVS archive.  Decrease my hack count by one by not
# preserving it any more.
#	# Preserve files
#	if [ -f src/modules/perl/mod_perl_version.h -a \
#	    '!' -f src/modules/perl/mod_perl_version.h.backup ]; \
#	then \
#	  echo 'Preserving mod_perl_version.h ...'; \
#	  cp src/modules/perl/mod_perl_version.h \
#	   src/modules/perl/mod_perl_version.h.backup; \
#	fi

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

	$(PERL) Makefile.PL USE_APXS=1 WITH_APXS=/usr/bin/apxs \
		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 # PERL_DEBUG=1
	$(MAKE)
	touch build-stamp

debian/control: debian/control.in
	sed -e 's/THISDEV/$(DEV_VERSION)/g' -e 's/NEXTDEV/$(NEXTVERNO)/g' \
	    -e 's/PERL_PACKNAME/$(PERL_PACKNAME)/g' \
	    -e 's/PERL_DEPEND/$(PERL_DEPEND)/g' \
		< debian/control.in > debian/control

clean:
	dh_testdir
	rm -f build-stamp
	dh_clean

	rm -f lib/Apache/MyConfig.pm t/docs/test.shtml \
		 t/net/config.pl t/net/perl/cgi.pl t/report t/conf/httpd.conf{,.old} \
		 t/httpd.conf apaci/find_source lib/mod_perl_hooks.pm
	find . -name .tmp.\* -exec rm -rf {} \;
	([ -f Makefile ] && $(MAKE) distclean) || true
	chmod -R u+w .
	rm -f debian/control
	$(MAKE) -f debian/rules debian/control

# See above.
#	if [ -f src/modules/perl/mod_perl_version.h.backup ]; then \
#	  echo 'Reverting mod_perl_version.h ...'; \
#	  mv -f src/modules/perl/mod_perl_version.h.backup \
#	   src/modules/perl/mod_perl_version.h; \
#	fi
# So just delete it.
	rm -f src/modules/perl/mod_perl_version.h

	if [ -f apaci/mod_perl.config.backup ]; then \
	  echo 'Reverting mod_perl.config ...'; \
	  mv -f apaci/mod_perl.config.backup apaci/mod_perl.config; \
	fi

# 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
	dh_installdirs

	# Add here commands to install the files into debian/tmp
	$(MAKE) 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 \) -exec \
	  rm -f '{}' \;
	# Why does this return nonzero?
	-find `pwd`/debian/tmp -type d -print0 | xargs -0 \
	  --no-run-if-empty rmdir -p --ignore-fail-on-non-empty

	mkdir -p debian/tmp/usr/lib/apache/1.3
	cp apaci/libperl.so debian/tmp/usr/lib/apache/1.3/mod_perl.so
	chmod 644 debian/tmp/usr/lib/apache/1.3/mod_perl.so
	cp debian/400mod_perl.info debian/tmp/usr/lib/apache/1.3/400mod_perl.info

	dh_installdocs
	cp -a eg/ debian/tmp/usr/share/doc/libapache-mod-perl/examples
	(cd debian/tmp/usr/share/doc/libapache-mod-perl && mv Changes changelog)

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

	find `pwd`/debian/tmp -type f -name .cvsignore -exec rm '{}' ';'

	find `pwd`/debian/tmp -type f -name mod_perl.exp -exec rm '{}' ';'

	dh_installmanpages
	dh_installchangelogs
	dh_strip
	dh_compress
	dh_fixperms
	dh_shlibdeps
	dh_installdeb
	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
