#!/usr/bin/make -f

#export DH_VERBOSE=1

PYVERS=$(shell pyversions -vs)
PY3VERS=$(shell py3versions -vs)
pkgdir = $(CURDIR)/debian/python$(if $(patsubst 3.%,,$(1)),,3)-mako
-include /usr/share/python/python.mk

clean:
	dh_testdir
	dh_testroot
	rm -f build-*
	rm -rf build
	find . -name '*\.py[co]' -delete
	rm -rf .pytest_cache
	rm -rf Mako.egg-info
	dh_clean

build:
build-indep: build-docs
build-arch:

test: $(PY3VERS:%=test-python%)
test-python%:
	-
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	python$* -m pytest -Wignore
endif

build-docs:
ifeq (,$(findstring nodoc,$(DEB_BUILD_OPTIONS)))
	dh_testdir
	dh_installdirs -i
	cd doc/build && sphinx-build -N -q -E -b html . \
	 $(call pkgdir,2.X)-doc/usr/share/doc/python-mako-doc/html/
	rm -rf $(call pkgdir,2.X)-doc/usr/share/doc/python-mako-doc/html/.doctrees
	touch $@
endif

install: $(PYVERS:%=install-python%) $(PY3VERS:%=install-python%) build-docs test
	dh_install
ifeq (,$(findstring nodoc,$(DEB_BUILD_OPTIONS)))
	mv $(CURDIR)/debian/python-mako-doc/usr/share/vim/addons/indent/mako_indent.vim \
	   $(CURDIR)/debian/python-mako-doc/usr/share/vim/addons/indent/mako.vim
	#rm -rf debian/python-mako-doc/usr/share/doc/python-mako/doc/build
endif

install-python%:
	$(if $(filter 2.7,$*),, \
	  python$* setup.py build_scripts	--executable=/usr/bin/python3 \
	)
	python$* setup.py install $(py_setup_install_args) \
		--root $(call pkgdir,$*) --prefix=/usr
	([ -d build/lib ] && mv build/lib $(shell python$* -c 'from distutils.command.build import build; from distutils.core import Distribution; b = build(Distribution()); b.finalize_options(); print(b.build_platlib)')) || true

binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs -i doc/build/changelog.rst
	dh_installdocs -i
	dh_installexamples -i
	dh_link -i
	dh_installman -i
	rm -rf debian/python-mako/usr/bin
	dh_python2 -i
	dh_python3 -i
	dh_compress -i -X.py -X.js -Xmakotemplates.txt
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch:

binary: binary-indep binary-arch

.PHONY: clean build-arch build-indep binary binary-arch binary-indep install
