#!/usr/bin/make -f

# DH_VERBOSE := 1

export DEB_BUILD_MAINT_OPTIONS = hardening=+bindnow
export LC_ALL=C.UTF-8

include /usr/share/dpkg/default.mk

# a few ISO-8859-1 files needing conversion to UTF-8
ISOFILES  = res/TemplateBatchFiles/PARRIS.bf
ISOFILES += res/TemplateBatchFiles/TemplateModels/MEC.mdl
ISOFILES += res/TemplateBatchFiles/TemplateModels/models.lst

%:
	dh $@ --buildsystem=cmake

override_dh_auto_clean:
ifeq (amd64,$(DEB_HOST_ARCH))
	dh_auto_clean --builddirectory obj-avx
	dh_auto_clean --builddirectory obj-sse3
endif
	dh_auto_clean

override_dh_auto_configure:
ifeq (amd64,$(DEB_HOST_ARCH))
	dh_auto_configure -- -DINSTALL_PREFIX=/usr -DNOAVX=ON -DNOSSE3=ON
	dh_auto_configure --builddirectory obj-avx -- -DINSTALL_PREFIX=/usr -DNOAVX=OFF -DNOSSE3=OFF
	dh_auto_configure --builddirectory obj-sse3 -- -DINSTALL_PREFIX=/usr -DNOAVX=ON -DNOSSE3=OFF
else
	dh_auto_configure -- -DINSTALL_PREFIX=/usr -DNOAVX=ON -DNOSSE3=ON
endif

override_dh_auto_build:
	# convert a few ISO-8859-1 files to address national-encoding
	set -e \
	; for file in $(ISOFILES) \
	; do    iconv -f ISO-8859-1 -t UTF-8 < $${file} > "$${file}.new" \
	;	mv "$${file}.new" "$${file}" \
	; done
ifeq (amd64,$(DEB_HOST_ARCH))
	dh_auto_build -- MP MPI
	dh_auto_build --builddirectory obj-avx -- MP MPI
	dh_auto_build --builddirectory obj-sse3 -- MP MPI
else
	dh_auto_build -- MP MPI
endif

override_dh_install-arch:
	dh_install -a
	chrpath --delete obj-*/HYPHYMPI
ifeq (amd64,$(DEB_HOST_ARCH))
	mkdir -p debian/hyphy-mpi/usr/lib/$(DEB_SOURCE)/bin
	mkdir -p debian/hyphy-pt/usr/lib/$(DEB_SOURCE)/bin
	cp obj-avx/HYPHYMPI debian/hyphy-mpi/usr/lib/$(DEB_SOURCE)/bin/HYPHYMPI-avx
	cp obj-sse3/HYPHYMPI debian/hyphy-mpi/usr/lib/$(DEB_SOURCE)/bin/HYPHYMPI-sse3
	cp obj-avx/hyphy debian/hyphy-pt/usr/lib/$(DEB_SOURCE)/bin/hyphy-avx
	cp obj-sse3/hyphy debian/hyphy-pt/usr/lib/$(DEB_SOURCE)/bin/hyphy-sse3
	mv debian/hyphy-mpi/usr/bin/HYPHYMPI debian/hyphy-mpi/usr/lib/$(DEB_SOURCE)/bin/
	mv debian/hyphy-pt/usr/bin/hyphy debian/hyphy-pt/usr/lib/$(DEB_SOURCE)/bin/
	cd $(CURDIR)/debian/hyphy-mpi/usr/bin && \
		ln -s ../lib/$(DEB_SOURCE)/bin/simd-dispatch HYPHYMPI
	cd $(CURDIR)/debian/hyphy-pt/usr/bin && \
		ln -s ../lib/$(DEB_SOURCE)/bin/simd-dispatch hyphy
endif

override_dh_install-indep:
	dh_install -i
ifeq (amd64,$(DEB_HOST_ARCH))
	dh_install -phyphy-common debian/bin/simd-dispatch /usr/lib/$(DEB_SOURCE)/bin
endif

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
ifeq (amd64,$(DEB_HOST_ARCH))
	set -e ; for SIMD in avx sse3 ; do \
		if lscpu | grep -q $${SIMD} ; then \
			find "obj-$${SIMD}/" \
			-not -path "*/CMakeFiles/*" \
			-type f,l \
			-executable \
			-exec cp -v '{}' . \; && \
			./run_unit_tests.sh ; \
		fi ; \
	done
endif
	find "obj-$(DEB_HOST_GNU_TYPE)/" \
		-not -path "*/CMakeFiles/*" \
		-type f,l \
		-executable \
		-exec cp -v '{}' . \;
	./run_unit_tests.sh
endif

override_dh_installdocs-arch:
	dh_installdocs -a
	for news in debian/NEWS.Debian.* ; do \
	    pkg=`echo $${news} | sed 's/.*NEWS.Debian.//'` ; \
	    cp -a $${news} debian/$${pkg}/usr/share/doc/$${pkg}/NEWS.Debian ; \
	done

override_dh_installdocs-indep:
	dh_installdocs -i