#!/usr/bin/make -f
# -*- makefile -*-

#export DH_VERBOSE=1

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# set environment for waf
export LINKFLAGS=-Wl,--as-needed
WAF_OPTIONS = --verbose --destdir=debian/tmp --prefix=/usr --enable-fftw3f
WAF_OPTIONS += --libdir=/usr/lib/$(DEB_HOST_MULTIARCH)
WAF_CMD = ./waf

export PYBUILD_NAME=aubio
export PYBUILD_AFTER_INSTALL_python2=rm -vrf '{destdir}/usr/bin'
export PYBUILD_AFTER_INSTALL_python3=mv '{destdir}/usr/bin/aubiocut' '{dir}/debian/tmp/usr/bin'

export PYBUILD_BEFORE_TEST=make create_test_sounds; cp -prv '{dir}/python/tests' '{build_dir}'
export PYBUILD_AFTER_TEST=rm -rf '{build_dir}/tests'; rm -rf '{dir}/python/tests/sounds'
export PYBUILD_TEST_ARGS_python2=cd '{build_dir}'; nose2-2.7 --verbose
export PYBUILD_TEST_ARGS_python3=cd '{build_dir}'; nose2-3 --verbose

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

override_dh_auto_clean:
	dh_auto_clean --buildsystem=pybuild
	-$(WAF_CMD) distclean
	rm -rf doc/web/
	rm -rf python/ext/config.h
	# extra rules to remove files manually
	#-find waf -name '*.pyc' -delete
	#rm -rf .waf* .lock-waf*
	#rm -rf build/ dist/ .waf* .lock-waf*
	#rm -rf python/gen/ aubio.egg-info
	#rm -rf python/lib/*.pyc python/lib/aubio/*.so
	#rm -rf python/tests/sounds
	#rm -rf debian/tmp
	#rm -rf manpages.refs manpages.links

override_dh_auto_configure:
	$(WAF_CMD) configure $(WAF_OPTIONS)

override_dh_auto_build:
	$(WAF_CMD) build $(WAF_OPTIONS)
	dh_auto_build --buildsystem=pybuild

override_dh_auto_test:
	# run tests
	PYBUILD_SYSTEM=custom \
	LD_LIBRARY_PATH=$(CURDIR)/build/src:$(LD_LIBRARY_PATH) \
		dh_auto_test --buildsystem=pybuild

override_dh_auto_install:
	# library
	$(WAF_CMD) install $(WAF_OPTIONS)
	# python extension
	dh_auto_install --buildsystem=pybuild
	# call dh_numpy to add dependency on numpy abi
	if [ -x /usr/bin/dh_numpy ]; then dh_numpy; fi
