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

PACKAGE2_NAME = python-skimage
PACKAGE3_NAME = python3-skimage
PKG_TMP = $(CURDIR)/debian/tmp

# don't optimize pngs, palette changes break tests and documented examples
export NO_PNG_PKG_MANGLE=1

PY2VERS = $(shell pyversions -vr)
PY3VER = $(shell py3versions  -vd)
PY3VERS = $(shell py3versions -vr)

export DEB_BUILD_MAINT_OPTIONS := hardening=+all

# upstream relies on signed chars
export DEB_CFLAGS_MAINT_APPEND := -fsigned-char
export DEB_LDFLAGS_MAINT_APPEND := -Wl,--as-needed

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  JOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
else
  JOBS = 1
endif

# Assure Agg backend for matplotlib to avoid any possible complication
export MPLBACKEND := Agg


%:
	dh $@ --with python2,python3,sphinxdoc --buildsystem pybuild

%-arch:
	dh $@ --with python2,python3 --buildsystem pybuild

clean:
	dh $@ --with python2,python3 --buildsystem pybuild

# silly distutils does not support parallelism! waste hours of time
.PHONY: build
build-arch: $(addprefix debian/build-stamp-python,$(PY2VERS)) $(addprefix debian/build-stamp-python,$(PY3VERS))
	:
build build-indep: build-arch doc/build/html
	:

debian/build-stamp-python%: debian/build-stamp-pyx
	PYBUILD_INTERPRETERS=python{version} \
	PYBUILD_VERSIONS=$* \
	PYBUILD_TEST_PYTEST=1 \
	dh build-arch --with python$(basename $*) --buildsystem pybuild
	touch $@

override_dh_clean:
	dh_clean
	rm -rf .pytest_cache build scikit_image.egg-info
	rm -f debian/build-stamp-*
	for i in $$(find -name '*.md5'); do \
		rm -f $${i%.pyx.md5}.c $${i%.pyx.md5}.cpp $$i; \
	done
	for i in $$(find -name '*.pyx.in'); do \
		rm -f $${i%.*}; \
	done
ifeq (,$(findstring -a,$(DH_INTERNAL_OPTIONS)))
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	+$(MAKE) -C doc clean
	rm -f doc/source/_static/random.js
endif
endif

debian/build-stamp-pyx: debian/bin/process_pyx.py
	python3 $< $(JOBS)
	touch $@

doc/build/html: debian/build-stamp-python$(PY3VER)
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	# hotfix SPHINXBUILD -- remove in next release
	+\
		PYTHONPATH=$(CURDIR)/.pybuild/cpython3_$(PY3VER)/build:$$(python3 -c 'import sys;print(":".join(sys.path))') \
		PYTHON=python$(PY3VER) \
		SPHINXBUILD="python$(PY3VER) -m sphinx" \
		SPHINXOPTS="-j $(JOBS) -D mathjax_path=MathJax.js" \
		$(MAKE) -C doc html
endif
	touch $@

# Test data and plugin ini not copied to .pybuild -- manually link for them
override_dh_auto_test-arch:
	for ver in $(PYBUILD_VERSIONS); do \
		i=.pybuild/cpython$${ver%.*}_$$ver/build/skimage; \
		cp skimage/io/_plugins/*.ini $$i/io/_plugins/; \
		rm -rf $$i/data || true; \
		ln -sf ../../../../skimage/data $$i; \
		for module in $$(find skimage ! -path '*/data/*' -name tests | cut -sd / -f 2-); do \
			rm -rf $$i/$$module || true; \
			ln -sf $(CURDIR)/skimage/$$module $$i/$${module%/*}/; \
		done; \
	done
	dh_auto_test

override_dh_auto_install:
	dh_auto_install
	# cleanup python3 cache (mixing python3 subversions)
	-find $(PKG_TMP) -name "__pycache__" -exec rm -r {} +

## remove .so libraries from main package, and call dh_numpy*
_dh_python%:
	-find debian/$(PACKAGE$*_NAME)/usr/lib -name "*.so" -delete

	dh_numpy$(*:2=) -p$(PACKAGE$*_NAME)-lib
	dh_python$*

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

override_dh_sphinxdoc-indep:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	dh_sphinxdoc -XMathJax.js
endif

override_dh_installdocs:
	dh_installdocs -A CONTRIBUTORS.txt README.md CONTRIBUTING.txt
