#!/usr/bin/make -f

# DH_VERBOSE := 1

pkg := $(shell dpkg-parsechangelog | sed -n 's/^Source: //p')
version=$(shell dpkg-parsechangelog -ldebian/changelog | grep Version: | cut -f2 -d' ' | cut -f1 -d- )
mandir=$(CURDIR)/debian/$(pkg)/usr/share/man/man1/
tmpompdir=$(CURDIR)/debian/tmp-omp/usr/bin

%:
	dh $@

# Build first with OMP multiprocessing
override_dh_auto_configure:
	dh_auto_configure -- -DIQTREE_FLAGS="omp"

override_dh_auto_build:
	dh_auto_build
	mkdir -p $(tmpompdir)
	cp -a `find $(CURDIR) -name iqtree-omp -type f -executable` $(tmpompdir)
	# build again without OMP
	dh_auto_clean
	dh_auto_configure
	dh_auto_build

override_dh_auto_install:
	dh_auto_install
	mv $(tmpompdir)/iqtree-omp debian/$(pkg)/usr/bin

override_dh_installexamples:
	dh_installexamples
	# remove example files in unusual dir
	rm -f debian/*/usr/models.nex
	rm -f debian/*/usr/example.[np][eh][xy]

override_dh_installman:
	mkdir -p $(mandir)
	help2man --no-info --no-discard-stderr --help-option="-h" \
	    --name='efficient phylogenetic software by maximum likelihood' \
	    --version-string="$(version)" $(CURDIR)/debian/$(pkg)/usr/bin/iqtree > $(mandir)/iqtree.1
	help2man --no-info --no-discard-stderr --help-option="-h" \
	    --name='efficient phylogenetic software by maximum likelihood (multiprocessor version)' \
	    --version-string="$(version)" $(CURDIR)/debian/$(pkg)/usr/bin/iqtree-omp > $(mandir)/iqtree-omp.1

override_dh_auto_test:
	# use only the first example for build time test to save time on autobuilders
#	if [ "`find $(CURDIR) -name iqtree -type f -executable`" = "" ] ; then \
#		iqtreeomp=`find $(CURDIR) -name iqtree-omp -type f -executable` ; \
#		ln -s iqtree-omp `dirname $$iqtreeomp`/iqtree ; \
#	fi
	sed '/ myprefix/,$$d' debian/Documents_source/example.sh > example.short
	echo 'time $(tmpompdir)/iqtree-omp -s example.phy -omp 2' >> example.short
	time sh example.short
	rm example.short

get-orig-source:
	uscan --verbose --force-download --repack --compression xz
