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

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
ifeq ($(DEB_TARGET_ARCH_CPU),i386)
  export DEB_CFLAGS_MAINT_APPEND  = -ffloat-store
endif

pkgdata:=hmmer2
sampledir:=$(CURDIR)/debian/$(pkgdata)/usr/share/doc/$(pkgdata)/examples
include /usr/share/dpkg/pkg-info.mk
UPSTREAM_VERSION := $(shell echo $(DEB_VERSION_UPSTREAM) | sed -e 's,+dfsg,,g')

export AUTOHEADER = 'true'

%:
	dh $@

override_dh_clean:
	dh_clean
	if [ -e configure_not_used ] ; then mv configure_not_used configure; fi
	rm -f configure.ac src/config.h

# Trying to address #1025739 but the resulting configure file does not work
override_dh_autoreconf:
	cp -a debian/missing-sources/configure.ac .
	mv configure configure_not_used
#	autoupdate
#	autoreconf -f -i || true
	dh_autoreconf

override_dh_auto_configure:
	dh_auto_configure -- --enable-threads --enable-lfs PTHREAD_CFLAGS="-D_XOPEN_SOURCE=500" # --enable-pvm
	# avoid duplicated definition of PACKAGE_NAME (basically conflicting with biosquid when used together)
	sed -i -e '/^#define PACKAGE_NAME /i #ifndef PACKAGE_NAME' \
	       -e '/^#define PACKAGE_NAME /a #endif' \
	       -e '/^#define PACKAGE_VERSION /i #ifndef PACKAGE_VERSION' \
	       -e '/^#define PACKAGE_VERSION /a #endif' \
	       -e '/^#define _LARGEFILE_SOURCE /i #ifndef _LARGEFILE_SOURCE' \
	       -e '/^#define _LARGEFILE_SOURCE /a #endif' \
	       -e '/^#define _LARGEFILE64_SOURCE /i #ifndef _LARGEFILE64_SOURCE' \
	       -e '/^#define _LARGEFILE64_SOURCE /a #endif' \
	       -e '/^#define VERSION /i #ifndef VERSION' \
	       -e '/^#define VERSION /a #endif' \
	       src/config.h

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/;' *
#	rename 's/^hmm(.+)2/hmm2$$1/;' $(CURDIR)/debian/tmp/bin/*
#	cd $(CURDIR)/debian/tmp/bin; rename 's/^hmm(.+)2/hmm2$$1/;' *
#	cd $(CURDIR)/debian/tmp/share/man/man1; rename 's/^hmm(.+)2/hmm2$$1/;' *
#	rename 's/^hmm(.+)2/hmm2$$1/;' $(CURDIR)/debian/tmp/share/man/man1/*
	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/*

override_dh_installchangelogs:
	dh_installchangelogs -k NOTES

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	dh_auto_test
	rm -f testsuite/*.ssi
	cd testsuite && make clean
endif

override_dh_install:
	sed -e "s/@DEB_HOST_MULTIARCH@/$(DEB_HOST_MULTIARCH)/" \
		-e "s/@VERSION@/$(UPSTREAM_VERSION)/" debian/pkgconfig/libhmmer2.pc.in > debian/pkgconfig/libhmmer2.pc
	dh_install
	rm -f debian/pkgconfig/libhmmer2.pc

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