#!/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

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure -- --enable-threads --enable-lfs --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/;' *
	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 00README

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"