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

PACKAGE2_NAME = python-dipy
PACKAGE_ROOT_DIR = debian/${PACKAGE2_NAME}
INSTALL_PATH = $(CURDIR)/debian/tmp

ifeq (,$(findstring get-orig-source, $(MAKECMDGOALS)))
export  http_proxy=http://127.0.0.1:9/
endif

# default Python
PYTHON=$(shell pyversions -d)
#PYVERS = $(shell pyversions -vr)
#PYVER = $(shell pyversions -vd)
#PY3VERS = $(shell py3versions -vr)
#PY3VER = $(shell py3versions -vd)

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 )
upver = $(shell echo $(debver) | cut -d '-' -f 1,1 )

MIN_CYTHONVER = 0.25.1

LAST_CHANGE = $(shell dpkg-parsechangelog -S Date)
BUILD_DATE  = $(shell LC_ALL=C date -u "+%B %d, %Y" -d "$(LAST_CHANGE)")
SPHINXOPTS := -D today=\"$(BUILD_DATE)\"

%:
	dh --buildsystem=python_distutils $@  --with python2

clean_generated:
	find dipy -iname *.c | xargs grep -l -e 'Generated by Cython' | xargs -r rm -f
	rm -f pyx-stamps

override_dh_auto_clean: clean_generated
	dh_auto_clean $@

_cythonize%:
	debian/rules clean_generated # force removal of previous copies
	make build-stamp-source && \
	D=debian/cythonized-files$(*:2=) && \
		git rm -rf $$D; \
		find dipy/ -name *.c | while read f; do \
			grep -q 'Generated by Cython' "$$f" || continue; \
			mkdir -p "$$D/$$(dirname $$f)"; \
			cp "$$f" "$$D/$$(dirname $$f)"; \
			git add -f "$$D/$$f"; \
		done; \
		cp pyx-stamps $$D/; \
		echo "$(upver)" >| $$D/VERSION; \
		git add -f $$D/VERSION $$D/pyx-stamps


_uncythonize%:
	echo "D: HERE"
	echo "$*" | grep -q '^3' && PY=3 || PY= ; \
	CYTHONVER=$$(dpkg -l cython$$PY 2>/dev/null | awk '/^ii/{print $$3;}' || echo 0); \
	dpkg --compare-versions "$$CYTHONVER" lt "$(MIN_CYTHONVER)" && { \
		echo "I: Using pre-Cython-ed files for Python $*"; \
		cd debian/cythonized-files$$PY/ ; \
		find . -name *.c | while read f; do cp $$f ../../$$f; done; \
		cp pyx-stamps ../../; } || :

cythonize: _cythonize2 # _cythonize3

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

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

override_dh_auto_install:
	dh_auto_install
	: # Prune duplicate LICENSE file
	find debian/ -name LICENSE -delete
	: # Only now lets build docs
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	: # Copy pregenerated examples
	cp -rp doc-examples/*/examples_built/* doc/examples_built/
	export PYTHONPATH=$$(/bin/ls -d $(INSTALL_PATH)/usr/lib/$(PYTHON)/*-packages) \
		   MPLCONFIGDIR=$(CURDIR)/build HOME=$(CURDIR)/build; \
	cd doc; $(MAKE) html-after-examples SPHINXOPTS="$(SPHINXOPTS)"
	-rm doc/_build/html/_static/jquery.js
	-rm -r doc/_build/html/_sources
	: # objects inventory is of no use for the package
	-rm doc/_build/html/objects.inv
endif

	: # Run tests later on
	: # cd build to prevent use of local/not-built source tree
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	cd build; \
	for PYTHON in $(shell pyversions -r); do \
		echo "I: Running Dipy unittests using $$PYTHON"; \
        PATH=$(INSTALL_PATH)/usr/bin:$$PATH \
		PYTHONPATH=$$(/bin/ls -d $(INSTALL_PATH)/usr/lib/$$PYTHON/*-packages) \
		MPLCONFIGDIR=/tmp/ \
			$$PYTHON /usr/bin/nosetests -sv --exclude 'test_(peaksFromModelParallel|fetcher)' dipy; \
	done
endif

## remove .so libraries from main package, and call dh_numpy*
## while removing 2 if not present
_dh_python%:
	if [ -d debian/$(PACKAGE$*_NAME)/usr/lib ]; then \
	   find debian/$(PACKAGE$*_NAME)/usr/lib -name "*.so" -delete; \
	fi
	[ -e /usr/bin/dh_numpy$(*:2=) ] && dh_numpy$(*:2=) -p$(PACKAGE$*_NAME)-lib || :
	dh_python$*

## "Instantiate" both rules so dh sees them
override_dh_python2: _dh_python2

## immediately useable documentation
## and exemplar data (they are small excerpts anyway)
override_dh_compress:
	dh_compress -X.py -X.html -X.css -X.jpg -X.txt -X.js -X.json -X.rtc -X.par -X.bin -Xobjects.inv

override_dh_clean:
	: # I: Custom cleaning
	rm -rf build doc-stamp
	$(MAKE) -C doc clean
	dh_clean

get-orig-source:
	-quilt pop -a
	uscan --verbose --download-current
#	: # I Testing for uncommited changes
#	@git diff --quiet HEAD
#	: # I Composing main tarball
#	mkdir -p tarballs
#	git archive --format=tar --prefix=$(srcpkg)-$(upver)/ HEAD \
#	| gzip -9 > ../tarballs/$(srcpkg)_$(upver).orig.tar.gz
#	: # I Building examples figures and tarball
#	python setup.py build
#	export PYTHONPATH=$$(/bin/ls -d $(CURDIR)/build/lib.*$(PYVER)); \
#		cd doc; $(MAKE) examples-tgz
#	mv dist/dipy-*-doc-examples.tar.gz ../tarballs/$(srcpkg)_$(upver).orig-doc-examples.tar.gz
	wget -O ../tarballs/$(srcpkg)_$(upver).orig-doc-examples.tar.gz \
		http://nipy.sourceforge.net/dipy/dipy-$(upver)-doc-examples.tar.gz
	: # Symlinking back into build-area for git-buildpackage not supporting multi-tarball atm
	[ -d ../build-area ] && ln -sf ../tarballs/$(srcpkg)_$(upver).orig*.tar.gz ../build-area

inject-doc-examples:
	mkdir -p doc-examples
	tar -C doc-examples --strip-components 1 \
	 -xzvf ../tarballs/$(srcpkg)_$(upver).orig-doc-examples.tar.gz
