#!/usr/bin/make -f

# Copyright (C) Andreas Tille <tille@debian.org>
# License: GPL

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

##MFILES:=Makefile.gcc \ # we use a wrapper to the faster versions instead of shipping the "plain" version
MFILES:=Makefile.PTHREADS.gcc \
	Makefile.SSE3.PTHREADS.gcc \
	Makefile.AVX.PTHREADS.gcc

BINDIR=$(CURDIR)/debian/tmpbuild

%:
	dh $@

override_dh_auto_clean:
	for mfile in $(MFILES); do $(MAKE) -f $${mfile} clean ; done
	rm -rf $(BINDIR)

override_dh_auto_build:
	mkdir -p $(BINDIR)
	for mfile in $(MFILES); do \
	    $(MAKE) -f $${mfile} ; \
	    curexe=`ls $(exe)* | head -n 1` ; \
	    if [ -e $(BINDIR)/$${curexe} ] ; then \
		echo "**** ERROR: $${curexe} was created before!!" && false ; \
	    else \
		mv $${curexe} $(BINDIR)/$${curexe} ; \
	    fi ; \
	    $(MAKE) -f $${mfile} clean ; \
	done

override_dh_installman:
	help2man --no-info --help-option="-h" --version-option="-v" \
	         --name="Randomized Axelerated Maximum Likelihood" \
	         $(CURDIR)/debian/$(DEBPKGNAME)/usr/bin/$(exe) > $(CURDIR)/debian/$(DEBPKGNAME)/usr/share/man/man1/$(exe).1
	for cexe in `ls $(BINDIR)/$(exe)*` ; do \
	    curexe=`basename $${cexe}` ; \
	    if [ $${curexe} != $(exe) ] ; then \
		ln -s $(exe).1.gz $(CURDIR)/debian/$(DEBPKGNAME)/usr/share/man/man1/$${curexe}.1.gz ; \
	    fi ; \
	done

override_dh_installexamples:
	dh_installexamples
	# fix perl path in example scripts
	for pscript in `ls usefulScripts/*.pl` ; do \
	    sed 's?/usr/local/bin/perl?/usr/bin/perl?' $${pscript} > $(CURDIR)/debian/$(DEBPKGNAME)/usr/share/doc/$(DEBPKGNAME)/examples/`basename $${pscript}` ; \
	done

get-orig-source:
	mkdir -p ../tarballs
	uscan --verbose --force-download --repack --compress xz --destdir=../tarballs
