#!/usr/bin/make -f

#DH_VERBOSE = 1
include /usr/share/dpkg/default.mk

export DEB_CPPFLAGS_MAINT_APPEND= \
 -I/usr/include/hdf5/serial \
 -I/usr/include/eigen3 \
 -I/usr/include/minimap2
# dpkg-buildpackage would otherwise set -O2, overriding upstream's -O3
# Note: ask Steffen why he chose to add -flto for this package.
# Steffen replies: anything with many object files and compute-intensive should benefit from it
export DEB_CFLAGS_MAINT_APPEND = -O3 -flto
export DEB_CXXFLAGS_MAINT_APPEND = -O3 -flto
export DEB_LDFLAGS_MAINT_APPEND = -L/usr/lib/$(DEB_HOST_MULTIARCH)/hdf5/serial
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# These variables just need to be defined as anything but "install" in order
# to use system versions.
export HDF5=external
export EIGEN=external
export HTS=external
export MINIMAP2=external

%:
	dh $@ --with python3

override_dh_auto_clean:
	sed -i~ 's/^.depend: .*/.depend:/' Makefile
	touch .depend
	$(MAKE) clean
	$(RM) .depend
	mv Makefile~ Makefile

override_dh_auto_build:
	dh_auto_build
	$(MAKE) nanopolish_test

### When overriding auto_test make sure DEB_BUILD_OPTIONS will be respected
override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	echo "Since we need the executable created inside build time test in autopkgtest this is enforced in dh_auto_build to circumvent nocheck."
endif

override_dh_install:
	dh_install
	for pl in `find debian -name "*.pl"` ; do \
	    sed -i '1s?^#!/usr/bin/env.*perl?#!/usr/bin/perl?' $${pl} ; \
	done
	for pl in `grep -Rl '#![[:space:]]*/usr/bin/env *python' debian/*/usr/*` ; do \
	    sed -i '1s?^#!.*python.*?#!/usr/bin/python3?' $${pl} ; \
	done

override_dh_fixperms:
	dh_fixperms
	find debian -name *.fast5 -exec chmod -x \{\} \;
