#!/usr/bin/make -f

PACKAGE = $(firstword $(shell dh_listpackages))
TMP     = $(CURDIR)/debian/$(PACKAGE)
LMP     = $(CURDIR)/lib/ModPerl
MPR     = $(CURDIR)/ModPerl-Registry
APR     = $(CURDIR)/Apache-Reload

%:
	dh $@ --parallel --with apache2

override_dh_auto_configure:
	! [ -e $(LMP)/DummyVersions.pm ] \
	    || cp $(LMP)/DummyVersions.pm debian/DummyVersions.pm.save
	! [ -e $(LMP)/MethodLookup.pm  ] \
	    || cp $(LMP)/MethodLookup.pm  debian/MethodLookup.pm.save
	dh_auto_configure -- \
		INSTALLDIRS=vendor \
		MP_TRACE=0 \
		MP_USE_DSO=1 \
		MP_USE_STATIC=0 \
		MP_CCOPTS="$(CFLAGS) -Wall" \
		MP_APR_CONFIG=/usr/bin/apr-config

override_dh_auto_build:
	dh_auto_build --parallel -- MODPERL_OPTIMIZE="$(CFLAGS)" OPTIMIZE="$(CFLAGS)"


override_dh_auto_test:
	[ -d $(CURDIR)/t/run ] || mkdir $(CURDIR)/t/run
	[ -d $(CURDIR)/t/logs ] || mkdir $(CURDIR)/t/logs
	touch $(CURDIR)/t/logs/error_log
	[ -d $(MPR)/t/run ] || mkdir $(MPR)/t/run
	[ -d $(MPR)/t/logs ] || mkdir $(MPR)/t/logs
	touch $(MPR)/t/logs/error_log
	[ -d $(APR)/t/run ] || mkdir $(APR)/t/run
	[ -d $(APR)/t/logs ] || mkdir $(APR)/t/logs
	touch $(APR)/t/logs/error_log
	APACHE_TEST_EXTRA_ARGS="-httpd_conf $(CURDIR)/debian/apache2.conf" \
		LANG=C \
		dh_auto_test --max-parallel=1 || ( RET=$$?; \
            echo "--- test suite failed with code $$RET, t/logs/error_log starts ---"; \
            cat t/logs/error_log; \
            echo "--- test suite failed with code $$RET, t/logs/error_log ends   ---"; \
            exit $$RET )

override_dh_clean:
	dh_clean
	rm -rf t/htdocs/hooks lib/Apache WrapXS debian/docs
	find . -name 'pod2htmd.tmp' -exec rm '{}' \;
	find . -name 'pod2htmi.tmp' -exec rm '{}' \;
	! [ -e debian/DummyVersions.pm.save ] \
	    || mv debian/DummyVersions.pm.save $(LMP)/DummyVersions.pm
	! [ -e debian/MethodLookup.pm.save  ] \
	    || mv debian/MethodLookup.pm.save  $(LMP)/MethodLookup.pm
	# these shouldn't be needed really
	rm -rf $(CURDIR)/t/logs $(CURDIR)/t/run $(MPR)/t/logs $(MPR)/t/run $(APR)/t/logs $(APR)/t/run
	find . -name 'Makefile.old' -exec rm '{}' \;
	rm -f Apache-Test/Apache-TestItSelf/t/SMOKE t/core

override_dh_auto_install:
	rm -f t/core
	dh_auto_install

override_dh_install:
	mkdir -p $(CURDIR)/debian/docs
	perl debian/transform_pod2html.pl $(CURDIR) docs debian
	dh_install
	mv $(TMP)/usr/bin/mp2bug $(TMP)/usr/share/libapache2-mod-perl2/
	rm -r $(TMP)/usr/bin/
	install -m755 debian/libapache2-mod-perl2.bug $(TMP)/usr/share/bug/libapache2-mod-perl2
	rm $(TMP)/usr/lib/perl5/Apache2/Reload.pm
	rm $(TMP)/usr/lib/perl5/Apache/SizeLimit.pm
	rm $(TMP)/usr/lib/perl5/Apache/Reload.pm

override_dh_installman:
	dh_installman
	rm $(TMP)/usr/share/man/man3/Apache2::Reload.3pm
	rm $(TMP)/usr/share/man/man3/Apache::SizeLimit.3pm
	rm $(TMP)/usr/share/man/man3/Apache::Reload.3pm

override_dh_installexamples:
	dh_installexamples
	prename 's,\.,,' debian/libapache2-mod-perl2-dev/usr/share/doc/libapache2-mod-perl2-dev/examples/code/.debug-*

# These rules are needed because otherwise the build process
# tries to re-make the 'build' directory and fails
.PHONY: build

build:
	dh build

