#!/usr/bin/make -f
srcpkg := $(shell LC_ALL=C dpkg-parsechangelog | grep '^Source:' | cut -d ' ' -f 2,2)
debver := $(shell LC_ALL=C dpkg-parsechangelog | grep '^Version:' | cut -d ' ' -f 2,2 )
uver := $(shell echo $(debver) | cut -d '-' -f 1,1 | sed -e 's,.dfsg,,g' )

PYVER   := $(shell pyversions -vd)

DH_VERBOSE = 1

TESTS_EXCLUDES := $(shell python -c 'import numpy as np; print np.little_endian' \
	| grep -q False \
	&& echo "-E 'cmp-(HeadMat(|Inv)|SurfSourceMat|v2eeg|SurfGainEEG|Head2MEGMat|SurfSource2MEGMat|SurfGainMEG)-Head1'")

# one ring to rule them all ...
%:
	dh $@


# The CMake flags for Debian (started from fslview's rules)
# - Search for VTK instead of a fixed path to be compatible with post-etch
#   versions and Ubuntu.
override_dh_auto_configure:
	dh_auto_configure -- \
			  -DCMAKE_EXE_LINKER_FLAGS:STRING="$(LDFLAGS)" \
              -DCMAKE_MODULE_LINKER_FLAGS:STRING="$(LDFLAGS)" \
              -DCMAKE_SHARED_LINKER_FLAGS:STRING="$(LDFLAGS)" \
              -DVTK_DIR:PATH=$$(dirname $$(find /usr/lib -name VTKConfig.cmake 2>/dev/null|head -n1)) \
              -DATLAS_INCLUDE_PATH:PATH=/usr/include/atlas \
              -DBUILD_SHARED:BOOL=ON \
              -DBUILD_TESTING:BOOL=ON \
              -DPYTHON_WRAP:BOOL=ON \
              -DPython_ADDITIONAL_VERSIONS:STRING=$(PYVER) \
              -DUSE_VTK:BOOL=OFF \
              -DCMAKE_SKIP_RPATH:BOOL=OFF
	@echo "Generating debian/pyversion to overcome absent 'current'"
	echo $(PYVER) >| debian/pyversions

override_dh_auto_test:
	dh_auto_test -- ARGS="$(TESTS_EXCLUDES)"

override_dh_install:
	: # I: Assure versioned dependency on NumPy
	if [ -x /usr/bin/dh_numpy ]; then dh_numpy;	fi
	dh_install

# little trick to make use of the distutils class without having
# a distutils package
#makebuilddir/python-openmeeg::
#	touch setup.py
#
#clean::
#	-rm setup.py

# Fetch upstream source straight from SVN since there is no source
# tarball any longer
get-orig-source:
	utarball=../$(srcpkg)-$(uver).tar.gz; \
	git archive --format=tar --prefix=$(srcpkg)-$(uver)/ svn/release-2.1 | \
		gzip -9 > $$utarball; \
	echo "I: run git-import-orig $$utarball"

override_dh_clean:
	dh_clean
	rm -f debian/pyversions
