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

include /usr/share/dpkg/default.mk
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export PYBUILD_NAME=dipy
export MPLBACKEND=Agg
export MPLCONFIGDIR=/tmp
export HOME=/tmp

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

PACKAGE3_NAME = python3-$(PYBUILD_NAME)
PACKAGE3_ROOT_DIR = $(CURDIR)/debian/${PACKAGE3_NAME}
INSTALL_PATH = $(CURDIR)/debian/tmp
PY3VERS = $(shell py3versions -vr)
BUILD_DATE := $(shell \
	date \
		--utc \
		--date="@$${SOURCE_DATE_EPOCH:-$(shell date +%s)}" \
		'+%B %d, %Y' \
)
SPHINXOPTS := -D today=\"$(BUILD_DATE)\"

# WARNING: test specifications below should match d/t/run-unit-test!

# These tests hog resources because they are too long, so it is preferable to
# skip them to give the infrastructure some breath.
SKIP_LONG_TESTS := \
	not dipy/align/tests/test_streamwarp.py \
	and not dipy/denoise/tests/test_lpca.py \
	and not dipy/reconst/tests/test_sfm.py \
	and not dipy/workflows/tests/test_reconst_csa_csd.py \
	and not dipy/workflows/tests/test_reconst_rumba.py

# These tests are too stringent for running properly on s390x.
SKIP_S390X_TESTS := \
	not dipy/align/tests/test_streamlinear.py \
	and not dipy/align/tests/test_streamline.py \
	and not dipy/tracking/tests/test_distances.py \
	and not dipy/viz/tests/test_fury.py \
	and not dipy/workflows/tests/test_align.py \
	and not dipy/workflows/tests/test_segment.py \
	and not dipy/align/tests/test_whole_brain_slr.py \
	and not dipy/data/tests/test_data.py

ifneq (,$(findstring $(DEB_BUILD_ARCH),s390x))
TEST_SPEC := $(SKIP_LONG_TESTS) and $(SKIP_S390X_TESTS)
else
TEST_SPEC := $(SKIP_LONG_TESTS)
endif

%:
	dh $@ --buildsystem=pybuild

override_dh_auto_clean:
	dh_auto_clean || true
	rm -rf .pybuild __pycache__
	find dipy -name "*\.c" -delete

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:
	chmod 0644 doc/examples/*.py
	dh_auto_build
	find -name func_coef.nii.gz -print -exec chmod 0644 '{}' ';'

execute_after_dh_auto_install:
	# Prune duplicate LICENSE file
	find debian/ -name LICENSE -delete
	# Only now lets build docs
ifeq (python-dipy-doc_,$(filter python-dipy-doc,$(shell dh_listpackages))_$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	cd doc/ \
	&& PYTHONPATH=$(shell pybuild --print build_dir --interpreter python3) \
		$(MAKE) html SPHINXOPTS="$(SPHINXOPTS)"
	# objects inventory is of no use for the package
	-rm doc/_build/html/objects.inv
	# references to cloudflare and github are privacy breaches
	sed 's@https://cdnjs\.cloudflare\.com/ajax/libs/mathjax/[^/]\+/@file:///usr/share/javascript/mathjax/unpacked/@' \
		-i doc/_build/html/*.html \
		   doc/_build/html/interfaces/*.html \
		   doc/_build/html/theory/*.html
	sed 's@https://cdn\.jsdelivr\.net/npm/mathjax[@]3/es5/tex-mml-chtml\.js@file:///usr/share/nodejs/mathjax-full/es5/tex-mml-chtml.js@' \
		-i doc/_build/html/*.html \
		   doc/_build/html/interfaces/*.html \
		   doc/_build/html/reference/*.html \
		   doc/_build/html/theory/*.html
	# FIXME: references to github are for images part of dipy_data, which
	# is not available in debian; these may need packaging to solve the
	# privacy breach here.
endif
	# Run tests later on
	# cd build to prevent use of local/not-built source tree
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	set -ex \
	; for PYTHON in $(PY3VERS) \
	; do	PYPKG="$(PACKAGE3_ROOT_DIR)/usr/lib/python$$PYTHON/dist-packages" \
	;	export PYTHONPATH="$$PYPKG" \
	;	echo "I: Running Dipy unittests using $$PYTHON" \
	;	cd "$$PYPKG" \
	;	PATH=$(PACKAGE3_ROOT_DIR)/usr/bin:$$PATH \
		xvfb-run -a -s "-screen 0 1280x1024x24 -noreset" \
			python$$PYTHON -m pytest -v -k '$(TEST_SPEC)' dipy \
	;	rm .pytest_cache -rf \
	; done
endif
	# Cleanup files landing unnecessarily in the python3 modules directory.
	rm -v debian/$(PACKAGE3_NAME)/usr/lib/python3*/dist-packages/*.nii.gz
	rm -v debian/$(PACKAGE3_NAME)/usr/lib/python3*/dist-packages/*.trk
	rm -v debian/$(PACKAGE3_NAME)/usr/lib/python3*/dist-packages/affine.txt
	# Cleanup files landing in non-standard path
	rm -rv debian/$(PACKAGE3_NAME)/usr/doc

## "Instantiate" both rules so dh sees them
override_dh_python3:
	set -e \
	; test -d debian/$(PACKAGE3_NAME)/usr/lib || exit 0 \
	; for lib in $$(find debian/$(PACKAGE3_NAME)/usr/lib -name "*.so") \
	; do	sdir=$$(dirname $$lib) \
	;	tdir=debian/$(PACKAGE3_NAME)-lib/$${sdir#*$(PACKAGE3_NAME)/} \
	;	mkdir -p "$${tdir}" \
	;	mv -v "$${lib}" "$${tdir}" \
	; done
	dh_numpy3 -p$(PACKAGE3_NAME)-lib
	dh_python3

# immediately useable documentation and example data
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

execute_before_dh_link-indep:
	rm -v debian/python-dipy-doc/usr/share/doc/python-dipy-doc/html/_static/doctools.js
	rm -v debian/python-dipy-doc/usr/share/doc/python-dipy-doc/html/_static/language_data.js
	rm -v debian/python-dipy-doc/usr/share/doc/python-dipy-doc/html/_static/searchtools.js

execute_after_dh_fixperms-arch:
	# Fix a couple of executables neither elf nor scripts.
	find debian/$(PACKAGE3_NAME)/usr/lib/python3*/dist-packages/ \
		-name '*.py' \
		-exec chmod -x '{}' '+'

execute_before_dh_clean:
	rm -rf build doc-stamp
	$(MAKE) -C doc clean

override_dh_dwz:
	dh_dwz || true

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
