#!/usr/bin/make -f

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

#export DEB_BUILD_MAINT_OPTIONS=hardening=+all
#DPKG_EXPORT_BUILDFLAGS = 1
#include /usr/share/dpkg/buildflags.mk
#breaks the build

export DH_OPTIONS
export PKG_CONFIG_LIBDIR=${CURDIR}
export PKG_CONFIG_ALLOW_SYSTEM_LIBS=true
export PKG_CONFIG_SYSROOT_DIR=${CURDIR}/debian/tmp/
export PERL_MM_OPT=INSTALLDIRS=vendor

%:
	dh $@ --with autoreconf,python3 #--parallel

override_dh_auto_install:
	dh_auto_install -- install-data

override_dh_install:
	# dh_install -X*.a -X*.la -Xpkgconfig
	dh_install -ppython3-jellyfish
	pybuild -d swig/python --name jellyfish
	
	dh_auto_configure --sourcedirectory=swig/perl5
	dh_auto_build --sourcedirectory=swig/perl5
	dh_auto_install --sourcedirectory=swig/perl5
	chrpath --delete debian/tmp/usr/lib/*/perl5/*/auto/jellyfish/jellyfish.so
	dh_install
	d-shlibmove --commit \
		    --multiarch \
		    --devunversioned \
		    --movedev "debian/tmp/usr/include/$(DEBPKGNAME)*/$(DEBPKGNAME)/*.hpp" usr/include/$(DEBPKGNAME) \
		    --movedev "debian/tmp/usr/include/$(DEBPKGNAME)*/$(DEBPKGNAME)/*.h" usr/include/$(DEBPKGNAME) \
			debian/tmp/usr/lib/*/lib$(DEBPKGNAME)-*.so

override_dh_auto_configure:
	dh_auto_configure -- --enable-swig --enable-perl-binding

override_dh_auto_clean:
	set -e && for i in $(build3vers); do \
	  cd swig/python ; \
          python$$i ./setup.py clean --all 2> /dev/null; \
	  cd ../.. ; \
	done
	dh_auto_clean

override_dh_clean:
	dh_clean
	if [ -d debian/tmp_save_tests ] ; then \
	    mv debian/tmp_save_tests/* tests ; \
	    rmdir debian/tmp_save_tests ; \
	fi
	rm -f tests/compat.sh
	rm -f swig/python/jellyfish*

override_dh_auto_build:
	mkdir -p debian/tmp_save_tests
	cp -a tests/* debian/tmp_save_tests
	dh_auto_build -- all bin/generate_sequence bin/test_all
#	cd doc ;  RUBYLIB=$PWD PATH=$PATH:../bin make clean all ; cd ..
#	broken

# jellyfish contains a *really* big test which should *not* run on autobuilders
# if you have a *really* large machine you can try to activate this test by
# uncommenting the following.
#override_dh_auto_test:
#	BIG=1 dh_auto_test	
