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

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


# For get-orig-source
PKGDIR := $(dir $(firstword $(MAKEFILE_LIST)))..
UPVER := $(shell dpkg-parsechangelog -l$(PKGDIR)/debian/changelog -S Version | \
		sed -re 's/\+ds-.*//')

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

export DEB_CFLAGS_MAINT_APPEND = -Wall


%:
	dh $@


override_dh_auto_configure-arch:
	dh_auto_configure -- -DLIB_SUFFIX=/${DEB_HOST_MULTIARCH}


override_dh_auto_configure-indep:
	# Nothing to do


override_dh_auto_build-arch:
	# Library
	dh_auto_build


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
	testrundir=$$(mktemp -d) ;\
		ADTTMP=$$testrundir \
		CPATH=$(CURDIR)/src/include \
		LIBRARY_PATH=$(CURDIR)/obj-$(DEB_HOST_GNU_TYPE)/src/ \
		LD_LIBRARY_PATH=$(CURDIR)/obj-$(DEB_HOST_GNU_TYPE)/src/ \
	/bin/sh $(CURDIR)/debian/tests/build ;\
	res=$$? ;\
	rm -rf "$$testrundir" ;\
	exit $$res


override_dh_auto_test-indep:
	# Nothing to do


override_dh_auto_install-arch:
	# Library
	dh_auto_install


override_dh_auto_install-indep:
	# Nothing to do


override_dh_install:
	dh_install --fail-missing
	

override_dh_installexamples-indep:
	dh_installexamples -i
	
	# 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 \
		-e 's|\.\./datasets|/usr/share/doc/libfann-doc/examples/datasets|' \
		-e 's|"xor.data"|"/usr/share/doc/libfann-doc/examples/xor.data"|' \
		-e 's|"scaling.data"|"/usr/share/doc/libfann-doc/examples/scaling.data"|' \
		debian/libfann-doc/usr/share/doc/libfann-doc/examples/*.c
	
	# Add Makefile and compile/run instructions for the examples
	cp debian/libfann-doc.examples.Makefile \
		debian/libfann-doc/usr/share/doc/libfann-doc/examples/Makefile
	cp debian/libfann-doc.examples.README \
		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
	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


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

# Remove upstream's non-DFSG-compliant files
get-orig-source:
	uscan \
		--noconf \
		--download-version $(UPVER) \
		--force-download \
		--check-dirname-level=0 \
		--destdir=$(CURDIR) \
		--rename \
		--repack \
		--compression xz \
		--copyright-file=$(PKGDIR)/debian/files-excluded \
		$(PKGDIR)
