#!/usr/bin/make -f

PACKAGE = $(shell dh_listpackages)
TMP     = $(CURDIR)/debian/$(PACKAGE)

POD2TEXT = Changes Config Faq Features FeaturesD \
   Intro IntroEmbperlObject SVN TipsAndTricks

ARCHLIB := $(shell perl -MConfig -e 'print $$Config{vendorarch}')

export DEB_BUILD_MAINT_OPTIONS = hardening=+all,-bindnow

%: %.pod
	pod2text < $< > $@

%:
	dh $@ --no-parallel

override_dh_auto_configure:
	. /etc/apache2/envvars && yes y | EPHTTPD=/usr/sbin/apache2 dh_auto_configure

override_dh_auto_install: $(POD2TEXT)
	# fix occasional upstream permission errors
	find eg blib/lib -type f -a -perm /111 -exec chmod -x \{\} \;

	dh_auto_install

	# the CGI scripts enable use_redirect_env, so should be safe to
	# install by default
	install -m 755 *cgi.pl debian/libembperl-perl/usr/lib/cgi-bin/

	# move German manpages to usr/share/man/de/man{1,2,3}
	#
	# find is needed here instead of pure shell wildcards, because
	# "[a-z]" also matches capital letters iff /bin/sh is a bash
	# and the locale settings are set to a locale which makes
	# matches case-insensitive by default, e.g. LC_ALL=de_DE.utf-8.
	# See https://bugs.debian.org/828636 for details.
	@set -e;\
	for f in $$(find $(TMP)/usr/share/man/man3/ -name '*[a-z]D.3pm'); do \
	    f_de=`echo $$f | sed 's,man\(.\)/\([^/]*\)D\.\([^/]*\)$$,de/man\1/\2.\3,'` ;\
	    echo "mv $$f $$f_de" ;\
	    mv $$f $$f_de ;\
	done

	# ship Apache config in mods-available
	sed -e 's,@ARCHLIB@,$(ARCHLIB),g' debian/zembperl.load.in > debian/zembperl.load
	install -m 644 debian/zembperl.conf debian/zembperl.load \
	    $(TMP)/etc/apache2/mods-available/

	# strip .pl off executables (and their manpages)
	rename -v 's/\.pl//' $(TMP)/usr/bin/emb*
	rename -v 's/\.pl//' $(TMP)/usr/share/man/man1/emb*

override_dh_clean:
	chmod a+x `cat debian/exec-bit`
	dh_clean