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

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

# This has to be exported to make some magic below work.
export DH_OPTIONS

CFLAGS = -Wall -g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

PYVERS = $(shell pyversions -r -v)


%:
	dh $@ --with autotools_dev

override_dh_auto_build:
	# Perform regular build process
	dh_auto_build
	
	# Build python extensions in python/
	cd python; \
	for py in $(PYVERS); do \
		python$$py setup.py build; \
	done
	
	# Build documentation
	mkdir -p doc/html doc/proj
	naturaldocs -i src/include -o HTML doc/html -p doc/proj

override_dh_auto_clean:
	# Perform regular clean process
	dh_auto_clean
	
	# Remove python extensions build directory
	rm -rf python/build
	
	# Remove output generated by naturaldocs
	rm -rf doc/proj doc/html

override_dh_auto_install:
	# Install python extensions from python/, so dh_pysupport can pick them up
	cd python; \
	for py in $(PYVERS); do \
		python$$py setup.py install -f --root $(CURDIR)/debian/python-pyfann \
			--install-layout=deb; \
	done
	
	# Continue with regular installation process
	dh_auto_install

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

override_dh_installexamples:
	# Perform regular examples install process
	dh_installexamples
	
	# Remove unnecessary executable bits from training data sets
	chmod 644 debian/libfann-dev/usr/share/doc/libfann-dev/examples/datasets/*
	
	# Modify dataset paths in examples to point to libfann-dev 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 -f debian/example-paths.sed \
		debian/libfann-dev/usr/share/doc/libfann-dev/examples/*.c \
		debian/python-pyfann/usr/share/doc/python-pyfann/examples/*.py
	
	# Add Makefile & compile/run instructions for the examples
	cp debian/Makefile.examples \
		debian/libfann-dev/usr/share/doc/libfann-dev/examples/Makefile
	cp debian/README-libfann2-examples \
		debian/libfann-dev/usr/share/doc/libfann-dev/examples/README
	
	# Add a warning to the python examples that they require data from
	# libfann-dev
	cp debian/README-pyfann-examples-data \
		debian/python-pyfann/usr/share/doc/python-pyfann/examples/README

override_dh_link:
	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:
	# Don't compress example files and datasets; documentation .js
	dh_compress -Xexamples -Xjavascript
