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

PACKAGE_NAME = python-sklearn
PACKAGE_ROOT_DIR = debian/${PACKAGE_NAME}

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

SPHINX_VER := $(shell python -c 'from sphinx import __version__; print __version__')
NODOC = $(shell dpkg --compare-versions $(SPHINX_VER) lt 1.1.3 && echo "nodoc" || echo "")
export DEB_BUILD_OPTIONS += $(NODOC)

CYTHON_VER := $(shell dpkg -l cython 2>/dev/null | awk '/^ii/{print $$3;}' || echo 0)
RECONF = $(shell dpkg --compare-versions $(CYTHON_VER) ge 0.19 && echo ",autoreconf" || echo "")

# Some tests are known to fail randomly so need to be excluded ATM
NOSEARGS := 
# --exclude='test_(sparse_svc_clone_with_callable_kernel|spectral_clustering_sparse|ovr_fit_predict)'

# Mega rule
%:
	: # Explicit build system to avoid use of all-in-1 Makefile
	dh $@ --buildsystem=python_distutils --with python2$(RECONF)

cythonize:
	@echo "D: removing previously generated by Cython sources"
	find -iname *.c | xargs grep -l 'Generated by Cython' | xargs rm
	make cython

override_dh_autoreconf:
	dh_autoreconf debian/rules -- cythonize

override_dh_clean:
	rm -rf build doc/_build doc/auto_examples *-stamp *.egg-info sklearn/datasets/__config__.py
	dh_clean

override_dh_auto_install: ${PYVERS:%=python-install%} ${PYVERS:%=python-test%}
# Per Python version logic -- install, test, move .so into -lib
python-install%:
	python$* setup.py install --install-layout=deb --root=$(PACKAGE_ROOT_DIR)

python-test%: python-install%
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	: # Run unittests here against installed scikits.learn
	export PYTHONPATH=`/bin/ls -d $$PWD/$(PACKAGE_ROOT_DIR)/usr/lib/python$*/*/`; \
	export MPLCONFIGDIR=$(CURDIR)/build HOME=$(CURDIR)/build; \
	export JOBLIB_MULTIPROCESSING=0; \
	cd build/; python$* /usr/bin/nosetests -s -v $(NOSEARGS) sklearn && \
	  { python$* /usr/bin/nosetests -v --with-doctest --doctest-extension=rst ../doc/ || : ; } # doctests are failing ATM
else
	: # Skip unittests due to nocheck
endif


override_dh_installdocs:
	: # Build Documentation using installed scikits.learn
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
ifneq (,$(findstring -a,$(DH_INTERNAL_OPTIONS)))
	: # not building documentation in -a
else
	export PYTHONPATH=`/bin/ls -d $$PWD/$(PACKAGE_ROOT_DIR)/usr/lib/python$(PYVER)/*`; \
	export MPLCONFIGDIR=$(CURDIR)/build HOME=$(CURDIR)/build; \
	 cd doc; $(MAKE) html
endif
else
	mkdir -p doc/_build/html
	echo "<HTML><BODY><P>sklearn documentation was not built.<P>Please visit <A HREF='http://scikit-learn.org'>http://scikit-learn.org</A></BODY></HTML>" >| doc/_build/html/index.html
endif
	: # Use jquery from Debian package, so prune shipped one
	-rm doc/_build/html/_static/jquery.js
	dh_installdocs -A AUTHORS.rst README*.rst

override_dh_installchangelogs:
	dh_installchangelogs doc/whats_new.rst

## move binary libraries into -lib
override_dh_python2:
	: # Move platform-specific libraries into -lib
	set -e; \
	for lib in $$(find $(PACKAGE_ROOT_DIR)/usr -name '*.so'); do \
	   sdir=$$(dirname $$lib) ; \
	   tdir=$(PACKAGE_ROOT_DIR)-lib/$${sdir#*$(PACKAGE_NAME)/} ; \
	   mkdir -p $$tdir ; \
	   echo "Moving '$$lib' into '$$tdir'." ; \
	   mv $$lib $$tdir ; \
	done
	if [ -x /usr/bin/dh_numpy ]; then dh_numpy -ppython-sklearn-lib; fi
	: # Prune scikits/__init__.py to avoid conflicts
	find debian -wholename \*scikits/__init__.py -delete
	: # Move scikits. space into a compatibility package
	set -e; \
	find debian -wholename \*scikits -type d | while read skd; do \
		skbd=$$(dirname $$skd); \
		skbd_=$$(echo $$skbd | sed -e 's/sklearn/scikits-learn/g'); \
		mkdir -p $$skbd_; \
		mv $$skd $$skbd_; \
	done
	dh_python2

## immediately useable documentation and exemplar scripts/data
override_dh_compress:
	dh_compress -X.py -X.html -X.pdf -X.css -X.jpg -X.txt -X.js -X.json -X.rtc -Xobjects.inv


## to prepare next Debian upstream source tarball
dfsg: dfsg-releases
dfsg-%:
	git checkout dfsg
	-git merge --no-commit $*
	-git rm -rf sklearn/externals/joblib
	# -git rm -f ./sklearn/svm/src/libsvm/svm.*
	git commit -m "Merge $* into DFSG (pruning externals: joblib)" -a
	git checkout debian
