#!/usr/bin/make -f

# DH_VERBOSE := 1

pkg=$(shell dpkg-parsechangelog | awk '/^Source:/ {print $$2}')
export PYBUILD_NAME=$(pkg)
PYVERS = $(shell pyversions -r)
PY3VERS = $(shell py3versions -r)

%:
	dh $@ --buildsystem=cmake --with python2,python3

override_dh_auto_clean:
	dh_auto_clean
	cd src/lib ; \
	for py in $(PYVERS) $(PY3VERS); do \
		$${py} -B setup.py clean -a; \
	done

override_dh_auto_configure:
	dh_auto_configure -- -DINSTALL_PREFIX=/usr

override_dh_auto_build:
	# no idea about the purpose of the dynamic library - executables are not simply linked against it
	dh_auto_build -- HYPHYGTK MP2 MPI GTEST # LIB
	# create Python modules
	cd src/lib ; \
	for py in $(PYVERS) $(PY3VERS); do \
		$${py} -B setup.py build; \
	done
	# Build R module - unfortunately this script is broken
	# cd src/lib ; bash build.sh LIBRARY R

override_dh_auto_test:
	find obj-* -name HYPHYGTEST -exec \{\} \;

override_dh_auto_install:
	dh_auto_install
	cd src/lib ; \
	for py in $(PYVERS); do \
		$$py -B setup.py install --skip-build --root ../../debian/python-$(PYBUILD_NAME) \
				      --install-layout deb; \
	done ; \
	for py in $(PY3VERS); do \
		$$py -B setup.py install --skip-build --root ../../debian/python3-$(PYBUILD_NAME) \
				      --install-layout deb; \
	done
	#mkdir -p $(CURDIR)/debian/lib$(pkg)/usr/lib
	#find debian -name "lib*.so" -exec mv \{\} $(CURDIR)/debian/lib$(pkg)/usr/lib \;

override_dh_install:
	# smallcaps for executables
	for bindir in `find . -type d -name bin` ; do \
	    for bin in $${bindir}/* ; do \
	        # ln -s `basename $${bin}` $${bindir}/`basename $${bin} | tr [A-Z] [a-z]` ; \
	        mv $${bindir}/`basename $${bin}` $${bindir}/`basename $${bin} | tr [A-Z] [a-z]` ; \
	    done ; \
	done
	dh_install

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