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

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# These get included in the build flags by debhelper
export DEB_CFLAGS_MAINT_APPEND = -Wall

# For pybuild
export PYBUILD_NAME=pyfann


%:
	dh $@ --with autoreconf,python2


override_dh_autoreconf:
	dh_autoreconf --as-needed


override_dh_auto_configure-arch:
	dh_auto_configure


override_dh_auto_configure-indep:
	# Nothing to do


override_dh_auto_build-arch:
	# Library
	dh_auto_build
	# Python extension. Must be run twice, because distutils runs build_py
	# before build_ext, but build_py relies on output from build_ext (from
	# SWIG), and the order cannot be changed. This is a known issue, see
	# http://bugs.python.org/issue7562
	dh_auto_build --buildsystem=pybuild --sourcedirectory=python
	dh_auto_build --buildsystem=pybuild --sourcedirectory=python


override_dh_auto_build-indep:
	# Build just the documentation
	mkdir -p doc/html doc/proj
	naturaldocs -i src/include -o HTML doc/html -p doc/proj


override_dh_auto_test-arch:
	# Simulate partial autopkgtest run by providing an ADTTMP and setting
	# CPATH/LIBRARY_PATH/LD_LIBRARY_PATH for the build test, and
	# LD_LIBRARY_PATH/PYTHONPATH for the Python test.
	#
	# We don't create a separate $ADTTMP for all of the tests because it
	# is not possible -- we only have one Pybuild command, but it may
	# perform tests for more than one version, so we just manually clean it
	# before each test using PYBUILD_BEFORE_TEST.
	testrundir=$$(mktemp -d) ;\
		ADTTMP=$$testrundir \
		CPATH=$(CURDIR)/src/include \
		LIBRARY_PATH=$(CURDIR)/src/.libs \
		LD_LIBRARY_PATH=$(CURDIR)/src/.libs \
	/bin/sh $(CURDIR)/debian/tests/build ;\
	[ $$? -eq 0 ] || exit 1 ;\
	\
		ADTTMP=$$testrundir \
		LD_LIBRARY_PATH=$(CURDIR)/src/.libs \
		PYBUILD_SYSTEM=custom \
		PYBUILD_BEFORE_TEST="cd $$ADTTMP && rm -rf *" \
		PYBUILD_TEST_ARGS="PYINTERPRETER={interpreter} /bin/sh $(CURDIR)/debian/tests/python-build" ;\
	du_auto_test --buildsystem=pybuild ;\
	rm -rf "$$testrundir"


override_dh_auto_test-indep:
	# Nothing to do


override_dh_auto_install-arch:
	# Library
	dh_auto_install
	
	# Clear .la dependency_libs as required by Policy 10.2
	sed -i -e "s/dependency_libs='.*'/dependency_libs=''/" \
		debian/tmp/usr/lib/*/*.la
	
	# Python extension
	dh_auto_install --buildsystem=pybuild --sourcedirectory=python


override_dh_auto_install-indep:
	# Nothing to do


override_dh_install:
	dh_install --fail-missing
	

override_dh_installexamples-indep:
	dh_installexamples -i
	
	# Copy Python examples to a subdir in examples/
	cp -r python/examples \
		debian/libfann-doc/usr/share/doc/libfann-doc/examples/python
	
	# Modify dataset paths in examples to point to libfann-doc datasets. This
	# was not implemented as a patch to upstream to preserve the ability to
	# run upstream's own tests within the source dir.
	sed -i -r -f debian/example-paths.sed \
		debian/libfann-doc/usr/share/doc/libfann-doc/examples/*.c \
		debian/libfann-doc/usr/share/doc/libfann-doc/examples/python/*.py
	
	# Add Makefile & compile/run instructions for the examples
	cp debian/Makefile.examples \
		debian/libfann-doc/usr/share/doc/libfann-doc/examples/Makefile
	cp debian/README-libfann2-examples \
		debian/libfann-doc/usr/share/doc/libfann-doc/examples/README
	

override_dh_fixperms-indep:
	dh_fixperms -i
	# Remove unnecessary executable bits from training data sets
	chmod 644 debian/libfann-doc/usr/share/doc/libfann-doc/examples/datasets/*


override_dh_link:
	# dh_link does not remove pre-existing destination dirs, only files
	rm -rf debian/libfann-dbg/usr/share/doc/libfann-dbg
	rm -rf debian/python-pyfann-dbg/usr/share/doc/python-pyfann-dbg
	dh_link


override_dh_compress:
	dh_compress -Xexamples -Xjavascript


override_dh_strip:
	# Can't use --dbg-package because we have multiple debug packages
	dh_strip -k -a
	mv debian/libfann2/usr/lib/debug debian/libfann-dbg/usr/lib
	mv debian/python-pyfann/usr/lib/debug debian/python-pyfann-dbg/usr/lib


override_dh_auto_clean:
	# Perform regular clean process
	dh_auto_clean
	
	# Python clean
	dh_auto_clean --buildsystem=pybuild --sourcedirectory=python
	
	# SWIG stuff
	rm -rf python/pyfann/pyfann_wrap.cpp python/pyfann/libfann.py
	
	# Remove output generated by naturaldocs
	rm -rf doc/proj doc/html
