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

include /usr/share/cdbs/1/rules/debhelper.mk
DEB_PYTHON_SYSTEM = pysupport
# for earlier cdbs versions
DEB_PYTHON_MODULE_PACKAGE = python-mvpa
# for cdbs >= 0.4.54
DEB_PYTHON_MODULE_PACKAGES = python-mvpa python-mvpa-lib
include /usr/share/cdbs/1/class/python-distutils.mk

# enable the LIBSVM wrapper and disable building provided copy
export PYMVPA_NO_3RD=1
DEB_PYTHON_BUILD_ARGS += --with-libsvm
DEB_PYTHON_INSTALL_ARGS_ALL += --with-libsvm

doc: doc-stamp
doc-stamp:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	$(MAKE) manpages apidoc htmldoc
	touch $@
endif

clean::
	$(MAKE) distclean

# run at install otherwise at build it tends to do them twice for some
# reason
install/python-mvpa::
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	set -e; for buildver in $(cdbs_python_build_versions); do \
		echo "I: Running PyMVPA unittests using python$$buildver"; \
        cd $(CURDIR) && cd $(DEB_SRCDIR)/build/lib.*-$$buildver && \
		ln -s $(CURDIR)/mvpa/data mvpa/ && \
		MVPA_TESTS_LOWMEM=yes MVPA_TESTS_QUICK=yes MVPA_TESTS_LABILE=no PYTHONPATH=. \
         $(call cdbs_python_binary,python$$buildver) mvpa/tests/main.py && \
		rm mvpa/data; \
    done
endif
	[ -x /usr/bin/dh_numpy ] && dh_numpy || :

build/python-mvpa-doc:: doc

# use jquery from Debian package
# symlinked by debhelper
install/python-mvpa-doc::
	-rm build/html/_static/jquery.js
	install -m 644 ./doc/misc/references.bib debian/python-mvpa-doc/usr/share/bib/pymvpa.bib

install/python-mvpa-lib::
	# move libraries into the python-mvpa-lib package
	set -e; for lib in $$(find debian/python-mvpa/usr -name '*.so'); do \
	   sdir=$$(dirname $$lib) ; \
	   tdir=debian/python-mvpa-lib/$${sdir#*python-mvpa/} ; \
	   mkdir -p $$tdir ; \
	   echo "Moving '$$lib' into '$$tdir'." ; \
	   mv $$lib $$tdir ; \
	done

# we need to move libraries away first, hence dependence on -lib
# Also without this step, movemodules from pysupport would not
# move arch-indep part of the module under pyshared
install/python-mvpa:: install/python-mvpa-lib

binary-install/python-mvpa-lib::
	# assure that there is no mvpa module itself
	# actually remove everything under ../share/.. since that is provided
	# by python-mvpa. That prevents e.g. duplicate egg-info files.
#	rm -rf debian/python-mvpa-lib/usr/share/python-support/python-mvpa-lib/*

# install directly into package directory (despite multiple packages)
DEB_DESTDIR = $(CURDIR)/debian/python-mvpa
# 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 objects.inv
# -doc package contents
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
DEB_INSTALL_DOCS_python-mvpa-doc := build/html
DEB_INSTALL_EXAMPLES_python-mvpa-doc := doc/examples/*
endif
DEB_INSTALL_DOCS += debian/upstream
