#!/usr/bin/make -f
# DH_VERBOSE=1

export CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
export CFLAGS:=$(shell dpkg-buildflags --get CFLAGS)
export CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS)
export LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

pkgdata:=hmmer2
sampledir:=$(CURDIR)/debian/$(pkgdata)/usr/share/doc/$(pkgdata)/examples

# lkajan: The following violates point 4.9 of the Debian policy: "get-orig-source [...] may be invoked in any directory [...]" as uscan needs to find the watch file.
.PHONY: get-orig-source
get-orig-source:
	set -e; \
	t=$$(mktemp -d) || exit 1; \
	trap "rm -rf -- '$$t'" EXIT; \
	pwd; \
	uscan --no-conf --force-download --rename --destdir "$$t"; \
	( cd "$$t"; \
		gunzip *.tar.gz; \
		tar --owner=root --group=root --mode=a+rX --delete -f *.tar --wildcards 'hmmer-*/Userguide.pdf'; \
		xz --best *.tar; \
	); \
	mv $$t/*.tar.xz ./

.PHONY: override_dh_auto_configure
override_dh_auto_configure:
	dh_auto_configure -- --enable-threads --enable-lfs --enable-pvm

.PHONY: override_dh_auto_install
override_dh_auto_install:
	dh_auto_install -- prefix=$(CURDIR)/debian/tmp
	# lkajan: rename binaries and man pages like this: s/hmm/hmm2/ - except hmmer.1, rename that to hmmer2.1:
	rename 's/^hmm/hmm2/;' $(CURDIR)/debian/tmp/bin/*
	cd $(CURDIR)/debian/tmp/bin; rename 's/^hmm/hmm2/;' *
	cd $(CURDIR)/debian/tmp/share/man/man1; rename 's/^hmm/hmm2/;' *
	mv $(CURDIR)/debian/tmp/share/man/man1/hmm2er.1 $(CURDIR)/debian/tmp/share/man/man1/hmmer2.1
	# lkajan: resolve man page hypen issue:
	sed -i -e 's/--/\\-\\-/g;s/-\([[:alpha:]]\)\b/\\-\1/g;' $(CURDIR)/debian/tmp/share/man/man1/*

.PHONY: override_dh_installchangelogs
override_dh_installchangelogs:
	dh_installchangelogs -k 00README

%:
	dh $@ --parallel --with autotools-dev

override_dh_installexamples:
	dh_installexamples
	mkdir -p $(sampledir);
	sed -i "s#hmm#hmm2#g" testsuite/exercises.sqc
	rm -f testsuite/*.ssi;
	cp -a testsuite/* $(sampledir)/;
	mkdir $(sampledir)/tutorial;
	cp -a tutorial/* $(sampledir)/tutorial;
	sed -i "s#../tut#./tut#g" $(sampledir)/exercises.sqc
	find $(sampledir) -name 'Makefile' | xargs sed -i "s^$(CURDIR)^.^g"
