#!/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
export TEST_WITH_XVFB=true

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)\"

# These tests attempt to make use of the network, notably during setup phase.
SKIP_NET_TESTS := \
	not test_tractogram.py \
	and not test_io_info \
	and not test_concatenate_flow \
	and not test_convert_tractogram_flow \
	and not test_spike \
	and not test_normalization \
	and not test_kernel_input \
	and not test_fbc \
	and not test_concatenate

# 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 test_streamwarp.py \
	and not test_lpca.py \
	and not test_sfm.py \
	and not test_reconst_csa_csd.py \
	and not test_reconst_rumba.py

SKIP_32BIT_TESTS := not test_check_img_shapes

ifneq (,$(findstring $(DEB_HOST_ARCH_BITS),32))
TEST_SPEC := ${SKIP_NET_TESTS} and $(SKIP_LONG_TESTS) and $(SKIP_32BIT_TESTS)
else
TEST_SPEC := ${SKIP_NET_TESTS} and $(SKIP_LONG_TESTS)
endif

ifeq ($(DEB_HOST_ARCH),s390x)
  TEST_SPEC += and not test_dki.py
endif

export PYBUILD_TEST_ARGS=-k '$(TEST_SPEC)' --pyargs dipy

%:
	dh $@ --buildsystem=pybuild

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

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)"
	# 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
	# Cleanup files landing in non-standard path
	rm -rfv 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 -fv debian/python-dipy-doc/usr/share/doc/python-dipy-doc/html/_static/doctools.js
	rm -fv debian/python-dipy-doc/usr/share/doc/python-dipy-doc/html/_static/language_data.js
	rm -fv 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
