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

include /usr/share/cdbs/1/rules/debhelper.mk
DEB_PYTHON_MODULE_PACKAGE = python-nipype
DEB_PYTHON_MODULE_PACKAGES = python-nipype
include /usr/share/cdbs/1/class/python-distutils.mk

DEB_DESTDIR = $(CURDIR)/debian/python-nipype
export HOME=$(CURDIR)/build

PYVERS = $(shell pyversions -vr)
PYVER = $(shell pyversions -vd)

IPYTHONVER := $(shell ipython -V || echo 0) # recent only needed for sphinxext
IPYTHONPATH := $(shell dpkg --compare-versions $(IPYTHONVER) lt 1 \
				 && echo ":/usr/lib/python$(PYVER)/dist-packages/IPython1X" || echo '')

build/python-nipype::
	mkdir -p build
	echo "backend : Agg" >| build/matplotlibrc

#
# Documentation
#
doc: doc-stamp
doc-stamp: build/python-nipype
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	: # TODO: We need to set matplotlib backend to Agg before building...
	export PYTHONPATH=$$PWD$(IPYTHONPATH) MPLCONFIGDIR=$(CURDIR)/build; cd doc; LC_ALL=C.UTF-8 $(MAKE) html
	touch $@
endif

build/python-nipype-doc:: doc

install/python-nipype::
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	export PYTHONPATH=`/bin/ls -d $(DEB_DESTDIR)/usr/lib/python$(cdbs_python_current_version)/*-packages`$(IPYTHONPATH); \
	cd build && \
         MPLCONFIGDIR=$(CURDIR)/build LC_ALL=C.UTF-8 py.test -v --doctest-modules ../nipype
    # test_mesh_ops needs X for bloody tvtk.api, and then fails anyways with current mayavi2/tvtk:
    # https://github.com/nipy/nipype/issues/1218, so skipping it for now
endif
	: # no executable spreadsheets
	find debian -iname *csv -print0 | xargs -r -0 chmod a-x

# use jquery from Debian package
# symlinked by debhelper
install/python-nipype-doc::
	-rm doc/_build/html/_static/jquery.js
	-rm doc/_build/html/_static/underscore.js

# immediately useable documentation
# and exemplar data (they are small excerpts anyways)
DEB_COMPRESS_EXCLUDE := .py .pdf .html .css .jpg .txt .js .json .rtc .par .bin
# -doc package contents
DEB_INSTALL_DOCS_python-nipype-doc := doc/_build/html
#doc/_build/latex/nipype.pdf

clean::
	rm -rf build pypeline.log
	$(MAKE) -C doc clean

.PHONY: doc clean
