#!/usr/bin/make -f

include /usr/share/python/python.mk

PYVERS :=  $(shell pyversions -vr)
PY3VERS := $(shell py3versions -vr)

%:
	dh $@ --with python2,python3

override_dh_auto_build:
	# Rebuild from Cython source
	rm -f src/*.c
	set -e -x; \
	for py in $(PYVERS) $(PY3VERS); do \
		python$$py setup.py build; \
		python$$py-dbg setup.py build; \
	done
	mkdir -p debian/html/api
	epydoc --html -o debian/html/api --name="PyStemmer" \
	       --url=http://snowball.tartarus.org/ \
	       build/lib.*-$(shell pyversions -vd)/Stemmer.so

override_dh_auto_clean:
	rm -rf build debian/html
	rm -f src/*.c

override_dh_auto_test:
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	set -e -x; \
	for py in $(PYVERS) $(PY3VERS); do \
		PYTHONPATH=$(call py_builddir_sh,$$py) \
			python$$py runtests.py; \
		PYTHONPATH=$(call py_builddir_sh,$$py-dbg) \
			python$$py-dbg runtests.py; \
	done
endif

override_dh_auto_install:
	set -e -x; \
	for py in $(PYVERS); do \
		python$$py setup.py install --skip-build --root debian/python-stemmer \
		                            --install-layout deb; \
		python$$py-dbg setup.py install --skip-build --root debian/python-stemmer-dbg \
		                                --install-layout deb; \
	done
	set -e -x; \
	for py in $(PY3VERS); do \
		python$$py setup.py install --skip-build --root debian/python3-stemmer \
		                            --install-layout deb; \
		python$$py-dbg setup.py install --skip-build --root debian/python3-stemmer-dbg \
		                                --install-layout deb; \
	done

override_dh_compress:
	dh_compress -X.js

override_dh_strip:
	dh_strip -p python-stemmer -p python-stemmer-dbg --dbg-package=python-stemmer-dbg
	dh_strip -p python3-stemmer -p python3-stemmer-dbg --dbg-package=python3-stemmer-dbg

override_dh_installdocs:
	dh_installdocs -p python-stemmer -p python-stemmer-dbg --link-doc=python-stemmer
	dh_installdocs -p python3-stemmer -p python3-stemmer-dbg --link-doc=python3-stemmer
	dh_installdocs -p python-stemmer-doc

get-orig-source:
	set -e; \
	VER=$(shell dpkg-parsechangelog | sed -rne 's,^Version: ([^+]+).*,\1,p'); \
	uscan --noconf --force-download --rename --download-version=$$VER --destdir=.; \
	tar -x --exclude=libstemmer_c -f pystemmer_$$VER.orig.tar.gz; \
	rm -f pystemmer_$$VER.orig.tar.gz; \
	GZIP=--best tar -cz --owner root --group root --mode a+rX \
	    -f pystemmer_$$VER+dfsg.orig.tar.gz PyStemmer-$$VER; \
	rm -rf PyStemmer-$$VER
