#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

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

include /usr/share/dpkg/architecture.mk
PERLVER := $(shell perl -MConfig -e 'print $$Config{version}')
ARCHLIB := $(shell perl -I/usr/lib/$(DEB_HOST_MULTIARCH)/perl/cross-config-$(PERLVER) -MConfig -e 'print $$Config{vendorarch}')

%:
	dh $@

# we want to use the system version of included modules, so
# we move the inc away and move it back afterwards
override_dh_auto_clean:
	dh_auto_clean
	[ ! -d $(CURDIR)/inc.save ] || mv $(CURDIR)/inc.save $(CURDIR)/inc

override_dh_auto_configure:
	[ ! -d $(CURDIR)/inc ] || mv $(CURDIR)/inc $(CURDIR)/inc.save
	dh_auto_configure

override_dh_auto_install:
	dh_auto_install
	mv $(TMP)/usr/bin/primes.pl $(TMP)/usr/bin/primes
	mkdir -p $(TMP)/usr/share/man/man1
	PERL5LIB=$(TMP)/$(ARCHLIB) help2man -n 'Display all primes' --no-info --no-discard-stderr $(TMP)/usr/bin/primes | gzip -9 > $(TMP)/usr/share/man/man1/primes.1.gz
	PERL5LIB=$(TMP)/$(ARCHLIB) help2man -n 'Print prime factors' --no-info --no-discard-stderr $(TMP)/usr/bin/factor.pl | gzip -9 > $(TMP)/usr/share/man/man1/factor.pl.1.gz
	find $(TMP)/usr/bin -type f -print0 | \
		 xargs -r0 sed -i -e '1s|^#!/usr/bin/env perl|#!/usr/bin/perl|'

override_dh_installexamples:
	dh_installexamples
	find $(TMP)/usr/share/doc/$(PACKAGE)/examples -type f -name "*.pl" -print0 | \
		xargs -r0 sed -i -e '1s|^#!/usr/bin/env perl|#!/usr/bin/perl|'
