#!/usr/bin/make -f
# debian/rules for phyml

export DEB_BUILD_MAINT_OPTIONS = hardening=+all,-pie

DEBPKGNAME     := $(shell dpkg-parsechangelog | awk '/^Source:/ {print $$2}')

%:
	dh $@ --with autoreconf

override_dh_autoreconf:
	dh_autoreconf --as-needed

override_dh_auto_configure:
override_dh_auto_build:
	# According to README you need different configure options to get different executables
	# Here we hack in a full build process for phyml executable
	# The hack includes saving the documentation from beeing cleaned up
	mv doc/phyml-manual.pdf .
	dh_auto_configure
	dh_auto_build -- LDFLAGS="$(LDFLAGS)"
	# move phyml binary to temporary dir inside debian/
	mkdir -p $(CURDIR)/debian/bin
	mv src/phyml $(CURDIR)/debian/bin/phyml
	$(MAKE) distclean
	dh_auto_configure -- --enable-beagle
	dh_auto_build -- LDFLAGS="$(LDFLAGS)"
	# move phyml binary to temporary dir inside debian/
	mkdir -p $(CURDIR)/debian/bin
	mv src/phyml-beagle $(CURDIR)/debian/bin/phyml-beagle
	$(MAKE) distclean
	dh_auto_configure -- --enable-mpi
	dh_auto_build -- LDFLAGS="$(LDFLAGS)"
	# move phyml binary to temporary dir inside debian/
	# mkdir -p $(CURDIR)/debian/bin
	mv src/phyml-mpi $(CURDIR)/debian/bin/phyml-mpi
	$(MAKE) distclean
	dh_auto_configure -- --enable-phytime
	dh_auto_build -- LDFLAGS="$(LDFLAGS)" CPPFLAGS="-DPHYREX $(CPPFLAGS)"
	mv phyml-manual.pdf doc

override_dh_auto_install:
	dh_auto_install
	# install phyml binary we have kept safe above
	mkdir -p $(CURDIR)/debian/$(DEBPKGNAME)/usr/lib/$(DEBPKGNAME)/bin
	mv $(CURDIR)/debian/bin/* $(CURDIR)/debian/$(DEBPKGNAME)/usr/lib/$(DEBPKGNAME)/bin
	chrpath --delete debian/$(DEBPKGNAME)/usr/lib/$(DEBPKGNAME)/bin/phyml*

override_dh_auto_clean:
	# save pdf from cleaning
	mv doc/phyml-manual.pdf .
	dh_auto_clean
	mv phyml-manual.pdf doc
	# remove temporary bin dir for phyml (see above hack)
	rm -rf debian/bin

override_dh_installdocs:
	dh_installdocs
	mv $(CURDIR)/debian/phyml/usr/share/doc/phyml/phyml-manual.pdf $(CURDIR)/debian/phyml/usr/share/doc/phyml/phyml_manual.pdf

override_dh_compress:
	dh_compress --exclude=.pdf
