#!/usr/bin/make -f

#export DH_VERBOSE=1

DEB_UPSTREAM_VERSION=$(shell dpkg-parsechangelog | sed -rne 's,^Version: ([^-]+).*,\1,p')
PYVERS=$(shell pyversions -vr)
PY3VERS=$(shell py3versions -vr)
PKGDIR=$(CURDIR)/debian/python-paste
PKG3DIR=$(CURDIR)/debian/python3-paste

include /usr/share/python/python.mk
clean:
	dh_testdir
	dh_testroot
	rm -rf dist build
	find . -name *\.py[co] -delete
	dh_clean build-docs* $(PYVERS:%=install-python%) $(PY3VERS:%=install3-python%)

build: build-indep
build-indep:2to3-stamp build-docs
build-arch:

2to3-stamp:
	rm -rf debian/tmp
	mkdir -p debian/tmp
	cp -a $(filter-out debian .svn, $(wildcard * .[^.]*)) debian/tmp
	2to3 --write --nobackups debian/tmp
	touch $@

build-docs: 2to3-stamp
	dh_testdir
	dh_installdirs
	PYTHONPATH=. sphinx-build -N -q -E -b html docs/ $(PKGDIR)/usr/share/doc/python-paste/docs/
	cd debian/tmp && PYTHONPATH=. /usr/share/sphinx/scripts/python3/sphinx-build -N -q -E -b html docs/ $(PKG3DIR)/usr/share/doc/python3-paste/docs/
	touch $@

install: $(PYVERS:%=install-python%) $(PY3VERS:%=install3-python%)
install-python%:
	python$* setup.py install --root $(PKGDIR) \
		--install-layout=deb $(py_setup_install_args)
	# pth file is not needed since all paste* packages are installed in same directory
	find $(PKGDIR) \( -name '*.pth' -or -name 'namespace_packages.txt' \) -delete
	touch $(PKGDIR)$(call py_libdir,$*)/paste/__init__.py
	dh_link -ppython-paste /usr/share/javascript/mochikit/MochiKit.js \
		$(call py_libdir,$*)/paste/evalexception/media/MochiKit.packed.js
	touch $@

install3-python%: 2to3-stamp
	cd debian/tmp && python$* setup.py install --root $(PKG3DIR) \
		--install-layout=deb $(py_setup_install_args)
	# pth file is not needed since all paste* packages are installed in same directory
	find $(PKG3DIR) \( -name '*.pth' -or -name 'namespace_packages.txt' \) -delete
	touch $(PKG3DIR)$(call py_libdir,$*)/paste/__init__.py
	touch $@

binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_link -ppython3-paste /usr/share/javascript/mochikit/MochiKit.js \
		/usr/lib/python3/dist-packages/paste/evalexception/media/MochiKit.packed.js
	dh_installchangelogs -i docs/news.txt
	dh_installdocs -i
	dh_sphinxdoc -i
	dh_python2 -i
	dh_python3 -i
	# it's not an extra license file
	echo "python-paste: extra-license-file usr/share/doc/python-paste/docs/_sources/license.txt" \
		>> $(PKGDIR)/usr/share/lintian/overrides/python-paste
	echo "python3-paste: extra-license-file usr/share/doc/python3-paste/docs/_sources/license.txt" \
		>> $(PKG3DIR)/usr/share/lintian/overrides/python3-paste
	dh_compress -i -X.py -X.js -Xobjects.inv
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i -- -Z bzip2

binary-arch:

binary: binary-indep binary-arch

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