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

PACKAGE2_NAME = python-sklearn
PACKAGE3_NAME = python3-sklearn
PACKAGE2_ROOT_DIR = debian/${PACKAGE2_NAME}
PACKAGE3_ROOT_DIR = debian/${PACKAGE3_NAME}

PYVERS = $(shell pyversions -vr)
PYVER = $(shell pyversions  -vd)
PY3VERS = $(shell py3versions -vr)
PY3VER = $(shell py3versions -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)

MIN_CYTHONVER = 0.24

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

# Some tests are known to fail randomly so need to be excluded ATM
# A bit aggressive exclusion of some tests which exhibit a problem I have no time to deal with on arm64
NOSEARGS := $(shell dpkg --print-architecture | grep -q -v arm64 || echo --exclude='test_dump')
# https://github.com/scikit-learn/scikit-learn/issues/7544
NOSEARGS += $(shell dpkg --print-architecture | grep -q -v i386 || echo --exclude='test_gpr')
# https://github.com/scikit-learn/scikit-learn/issues/7572
NOSEARGS += $(shell dpkg --print-architecture | grep -q -v armel || echo --exclude='test_matthews_corrcoef')
# --exclude='test_(sparse_svc_clone_with_callable_kernel|spectral_clustering_sparse|ovr_fit_predict)'
# https://buildd.debian.org/status/fetch.php?pkg=scikit-learn&arch=ppc64el&ver=0.18-3&stamp=1475603905
NOSEARGS += $(shell dpkg --print-architecture | grep -q -v ppc64el || echo --exclude='test_non_meta_estimators')


# try to prevent unsunctioned downloads
export  http_proxy=http://127.0.0.1:9/
export  https_proxy=http://127.0.0.1:9/

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

clean_generated:
	find sklearn/ -regex '.*\.c\(\|pp\)' | xargs grep -l -e 'Generated by Cython'  | xargs -r rm -f

_cythonize%: clean_generated
	debian/rules clean_generated # force removal of previous copies
	python$(*:2=) setup.py build_src
	D=debian/cythonized-files$(*:2=) && \
		git rm -rf $$D; \
		find sklearn/ -regex '.*\.c\(\|pp\)' | 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; \
		echo "$(UVER)" >| $$D/VERSION; git add $$D/VERSION

_uncythonize%:
	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 $*"; \
		touch PKG-INFO; \
		cd debian/cythonized-files$$PY/ ; \
		find . -regex '.*\.c\(\|pp\)' | while read f; do cp $$f ../../$$f; done; } || :

cythonize: _cythonize2 _cythonize3

override_dh_auto_configure: _uncythonize2 _uncythonize3
	dh_auto_configure

override_dh_autoreconf:
	dh_autoreconf debian/rules -- clean_generated

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%} ${PY3VERS:%=python-install%} ${PYVERS:%=python-test%} ${PY3VERS:%=python-test%}
# Per Python version logic -- install, test, move .so into -lib
python-install%: _uncythonize%
	python$* setup.py install --install-layout=deb  --root=$(CURDIR)/debian/tmp

python-test%: python-install%
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	: # Run unittests here against installed scikits.learn
	echo "$*" | grep -q '^3' && PY=3 || PY=$*; \
	export PYTHONPATH=`/bin/ls -d $$PWD/debian/tmp/usr/lib/python$$PY/*/`; \
	export MPLCONFIGDIR=$(CURDIR)/build HOME=$(CURDIR)/build; \
	export JOBLIB_MULTIPROCESSING=2; \
	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/debian/tmp/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/stable/_static/jquery.js
	-rm doc/_build/html/stable/_static/underscore.js
	dh_installdocs -A AUTHORS.rst README*.rst

override_dh_installchangelogs:
	dh_installchangelogs doc/whats_new.rst

## remove .so libraries from main package, and call dh_numpy*
## while removing 2 if not present
_dh_python%:
	[ ! -e debian/python$(*:2=)-sklearn ] \
	    || find debian/python$(*:2=)-sklearn/usr/lib -name "*.so" -delete;
	[ -e /usr/bin/dh_numpy$(*:2=) ] && dh_numpy$(*:2=) -ppython$(*:2=)-sklearn-lib || :
	: # Prune scikits/__init__.py to avoid conflicts
	find debian -wholename \*scikits/__init__.py -delete
	: # Move scikits. space into a compatibility package
	if [ $* -eq 2 ]; then \
	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; \
	fi
	dh_python$*

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

## 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
