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

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

PYVERS = $(shell pyversions -vr)
PYVER = $(shell pyversions  -vd)
PY3VERS = $(shell py3versions -vr)

export MPLCONFIGDIR=$(CURDIR)/build
export HOME=$(CURDIR)/build

# Mega rule
%:
	dh $@ --with python2,python3,sphinxdoc --buildsystem python_distutils

override_dh_clean:
	rm -f install-stamp
ifeq (,$(filter noclean,$(DEB_BUILD_OPTIONS)))
	rm -rf build doc/build doc/auto_examples *-stamp skimage.egg-info
	dh_clean
endif

# Assure Agg backend for matplotlib to avoid any possible complication
override_dh_auto_configure:
	mkdir -p $(MPLCONFIGDIR)
	echo "backend : Agg" >| $(MPLCONFIGDIR)/matplotlibrc
	dh_auto_configure

override_dh_auto_install-arch: ${PYVERS:%=python-install%} ${PY3VERS:%=python-install%}
	touch install-stamp

# Build docs during install, requires arch build for imports
override_dh_auto_install-indep: override_dh_auto_install-arch
	: # Build Documentation
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	export PYTHONPATH=$(PKG_TMP)/usr/lib/python$(PYVER)/dist-packages; \
	 cd doc; test -d build/html || $(MAKE) html
endif

# no docs so no reason for sphinxdoc to fail
override_dh_sphinxdoc-arch:

# Per Python version logic -- install, test, move .so into -lib
python-install%:
	python$* setup.py install --install-layout=deb --root=$(CURDIR)/debian/tmp

python-test%: install-stamp
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	pkgbuild=yes debian/tests/python$*
else
	true # Skip unittests due to nocheck
endif

## remove .so libraries from main package, and call dh_numpy*
_dh_python%: python-test%
	# does not exist in arch build
	-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_installdocs:
	dh_installdocs -A CONTRIBUTORS.txt README.md DEVELOPMENT.txt TASKS.txt

