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

export PYBUILD_NAME=dipy

PACKAGE3_NAME = python3-$(PYBUILD_NAME)

PACKAGE3_ROOT_DIR = debian/${PACKAGE3_NAME}
INSTALL_PATH = $(CURDIR)/debian/tmp

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

# default Python
PY3VERS = $(shell py3versions -vr)
PY3VER = $(shell py3versions -vd)

include /usr/share/dpkg/default.mk

MIN_CYTHONVER = 0.25.1

BUILD_DATE := "$(date --utc --date="@${SOURCE_DATE_EPOCH:-$(date +%s)}" +%B %d, %Y)"
SPHINXOPTS := -D today=\"$(BUILD_DATE)\"

%:
	dh $@ --buildsystem=pybuild --with python3

override_dh_auto_clean:
	dh_auto_clean || true
	rm -rf .pybuild __pycache__

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	: # Do not test just after build, lets install and then test
endif

override_dh_auto_build:
	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)))
	export PYTHONPATH=$(shell pybuild --print build_dir --interpreter python3) \
		   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)))
	set -x ; \
	export http_proxy=http://127.0.0.1:9/ ; \
	export https_proxy=https://127.0.0.1:9/ ; \
	for PYTHON in $(PY3VERS); do \
	    export PYTHONPATH=$$(pybuild --print build_dir --interpreter python$$PYTHON); \
	    echo "I: Running Dipy unittests using $$PYTHON"; \
	    cd build; \
	    PATH=$(INSTALL_PATH)/usr/bin:$$PATH \
	        HOME=/tmp \
		MPLCONFIGDIR=/tmp/ xvfb-run -a -s "-screen 0 1280x1024x24 -noreset" \
			python$$PYTHON /usr/bin/nosetests3 -sv --exclude 'test_(peaksFromModelParallel|fetcher)' dipy; \
	    cd .. ; \
	done
endif

## move .so libraries into -lib package, and call dh_numpy*
## while removing 2 if not present
_dh_python%:
	set -e \
	; test -d debian/$(PACKAGE$*_NAME)/usr/lib || exit 0 \
	; for lib in $$(find debian/$(PACKAGE$*_NAME)/usr/lib -name "*.so") \
	; do	sdir=$$(dirname $$lib) \
	;	tdir=debian/$(PACKAGE$*_NAME)-lib/$${sdir#*$(PACKAGE$*_NAME)/} \
	;	mkdir -p "$${tdir}" \
	;	mv -v "$${lib}" "$${tdir}" \
	; done
	[ -e /usr/bin/dh_numpy$(*:2=) ] && dh_numpy$(*:2=) -p$(PACKAGE$*_NAME)-lib || :
	dh_python$*

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

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

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