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

PACKAGE_NAME = python-skimage
PACKAGE_ROOT_DIR = $(CURDIR)/debian/${PACKAGE_NAME}

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

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

# Mega rule
%:
	dh $@ --buildsystem python_distutils

override_dh_clean:
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

# Build docs during install
override_dh_auto_install: ${PYVERS:%=python-install%}

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

	: # Build Documentation
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	export PYTHONPATH=$(PACKAGE_ROOT_DIR)/usr/lib/python$*/dist-packages; \
	 cd doc; test -d build/html || $(MAKE) html
	python$* setup.py install --install-layout=deb --root=$(PACKAGE_ROOT_DIR)
	: # Use jquery and underscore from Debian package, so prune shipped one
	-rm -f doc/build/html/_static/jquery.js
	# Actually due to the problems with underscore pkg across releases just ship one for now
	#-rm -f doc/build/html/_static/underscore.js
	-rm -rf doc/build/html/_sources
	-rm -f doc/build/html/.buildinfo
endif

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	: # Run unittests
	export PYTHONPATH=$(PACKAGE_ROOT_DIR)/usr/lib/python$*/dist-packages; \
	 python$* /usr/bin/nosetests -s \
	  $(PACKAGE_ROOT_DIR)/usr/lib/python$*/*-packages/skimage
else
	: # Skip unittests due to nocheck
endif

	: # Move platform-specific libraries into -lib
	set -e; for lib in $$(find $(PACKAGE_ROOT_DIR)/usr -name '*.so'); do \
	   sdir=$$(dirname $$lib) ; \
	   tdir=$(PACKAGE_ROOT_DIR)-lib/$${sdir#*$(PACKAGE_NAME)/} ; \
	   mkdir -p $$tdir ; \
	   echo "Moving '$$lib' into '$$tdir'." ; \
	   mv $$lib $$tdir ; \
	done

	if [ -x /usr/bin/dh_numpy ]; then dh_numpy; fi

## 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.rst DEVELOPMENT.txt TASKS.txt

