#!/usr/bin/make -f

# DH_VERBOSE := 1

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

include /usr/share/dpkg/default.mk

mandir=$(CURDIR)/debian/$(DEB_SOURCE)/usr/share/man/man1/

%:
	dh $@

VARIANTS = omp serial mpi

override_dh_auto_configure: $(foreach variant,$(VARIANTS),dh_auto_configure_$(variant))
override_dh_auto_build:     $(foreach variant,$(VARIANTS),dh_auto_build_$(variant))
override_dh_auto_install:   $(foreach variant,$(VARIANTS),dh_auto_install_$(variant))
override_dh_auto_clean:     $(foreach variant,$(VARIANTS),dh_auto_clean_$(variant))

dh_auto_configure_mpi:
	dh_auto_configure -Bbuild.mpi -- -DIQTREE_FLAGS="mpi"

dh_auto_configure_omp:
	dh_auto_configure -Bbuild.omp -- -DIQTREE_FLAGS="omp"

dh_auto_configure_serial:
	dh_auto_configure -Bbuild.serial -- -DIQTREE_FLAGS=""

dh_auto_build_%:
	dh_auto_build -Bbuild.$(subst dh_auto_build_,,$@)
	# restore the old naming to avoid name space conflict with serial build
	if [ -x build.omp/iqtree ] ; then mv build.omp/iqtree build.omp/iqtree-omp ; fi

dh_auto_install_%:
	dh_auto_install -Bbuild.$(subst dh_auto_install_,,$@)

override_dh_install:
	dh_install
	find debian -name iqtree-mpi -exec chrpath --delete \{\} \;
	find debian -name iqtree-omp -exec chrpath --delete \{\} \;

dh_auto_clean_%:
	dh_auto_clean -Bbuild.$(subst dh_auto_clean_,,$@)

override_dh_installexamples:
	# somehow the example config is installed in /usr by cmake
	find debian/$(DEB_SOURCE) -name example.cf -delete
	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="$(DEB_VERSION_UPSTREAM)" $(CURDIR)/debian/$(DEB_SOURCE)/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="$(DEB_VERSION_UPSTREAM)" $(CURDIR)/debian/$(DEB_SOURCE)/usr/bin/iqtree-omp > $(mandir)/iqtree-omp.1
	help2man --no-info --no-discard-stderr --help-option="-h" \
	    --name='efficient phylogenetic software by maximum likelihood (multiprocessor version)' \
	    --version-string="$(DEB_VERSION_UPSTREAM)" $(CURDIR)/debian/$(DEB_SOURCE)/usr/bin/iqtree-mpi > $(mandir)/iqtree-mpi.1

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	# 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
ifneq ($(shell nproc), 1)
	sed '/ myprefix/,$$d' debian/Documents_source/example.sh > example.short
	echo 'time $(CURDIR)/build.omp/iqtree-omp -s example.phy -omp 2	-redo' >> example.short
	time sh example.short
	rm example.short
endif
endif