#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-

export DH_VERBOSE=1

PYVER = $(shell pyversions -vd)

# To fix distutils fragile nature: when seing LDFLAGS=, which is set
# by dpkg-buildpackage it stops providing -shared flag, so for the
# minimalistic fix (alternative to NMU-ed #535699 with explicit
# -shared) we just unexport LDFLAGS
unexport LDFLAGS

# Mega rule
%:
	dh --buildsystem=python_distutils $@

override_dh_auto_test:
    : # Do not test just after build, lets install and then test

override_dh_auto_build:
	dh_auto_build
	echo "backend : Agg" >| build/matplotlibrc

#
# Documentation
#
doc-stamp:
	: # I: Generate documentation
	export PYTHONPATH=$$PWD/`/bin/ls -d build/lib.*$(PYVER)` \
		   MPLCONFIGDIR=$(CURDIR)/build HOME=$(CURDIR)/build; \
     cd doc; $(MAKE) html
	: # I: Use jquery from Debian package
	-rm doc/build/html/_static/jquery.js
	touch $@


#override_dh_auto_install:
#	dh_auto_install

test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	: # I: Unittest only the default python version for now against "installed" materials
	cd debian; \
	 PYTHONPATH=$$(/bin/ls -d $(CURDIR)/debian/tmp/usr/lib/python$(PYVER)/*-packages) \
	 MPLCONFIGDIR=$(CURDIR)/build \
	  ../tools/nipnost nipy
else
	: # Skip unittests due to nocheck
endif

override_dh_install: test
	: # I: Move libraries into the python-nipy-lib and -lib-dbg packages
	find debian/tmp/ -iname *.so | \
	  while read so; do \
	    d=$$(dirname $$so); \
		if [ $${so%_d.so} = $${so} ]; then suf=""; else suf="-dbg"; fi; \
	    d=$$(echo $$d | sed -e "s,debian/tmp/,debian/python-nipy-lib$$suf/,g"); \
	    mkdir -p $$d; echo "Moving $$so under $$d"; mv $$so $$d; \
	  done
	: # I: Move leftovers into python-nipy
	mkdir -p debian/python-nipy
	mv debian/tmp/* debian/python-nipy/
	: # I: Fix up interpreter which might be -dbg one
	sed -i -e 's,/usr/bin/python-dbg,/usr/bin/python,g' debian/python-nipy/usr/bin/*
	: # I: Assure versioned dependency on NumPy
	if [ -x /usr/bin/dh_numpy ]; then dh_numpy;	fi

	dh_install


## immediately useable documentation and exemplar scripts/data
override_dh_compress:
	: # I: Avoiding compression of what should not be compressed
	dh_compress -X.py -X.html -X.pdf -X.css -X.jpg -X.txt -X.js -X.json -X.rtc -Xobjects.inv


override_dh_clean:
	: # I: Custom cleaning
	rm -rf build doc-stamp nipy/neurospin/__config__.py
	$(MAKE) -C doc clean
	dh_clean


# We build documentation only for -indep
binary-indep: doc-stamp

binary: binary-arch binary-indep
