#!/usr/bin/make -f
# -*- makefile -*-

include /usr/share/python/python.mk

PACKAGE_NAME=python-sphinx
PACKAGE_DIR=$(CURDIR)/debian/$(PACKAGE_NAME)
SITE_PACKAGES_DIR=$(PACKAGE_DIR)$(call py_libdir,$(shell pyversions -d))

build: build-stamp

build-stamp:
	dh_testdir
	mkdir -p _build/html
	PYTHONPATH=. python debian/sphinx-build-local.py doc _build/html
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	set -e -x; \
	for python in $(shell pyversions -r); do \
		$$python tests/run.py; \
	done
endif
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	python setup.py clean
	find . -name '*.py[co]' -delete
	rm -rf _build build *.egg-info
	dh_clean sphinx/pycode/*.pickle

install: build
	dh_testdir
	dh_testroot
	dh_installdirs
	
	python setup.py install --no-compile --install-layout=deb --root $(PACKAGE_DIR)
	# move static files outside {site,dist}-packages
	mv $(SITE_PACKAGES_DIR)/sphinx/texinputs $(PACKAGE_DIR)/usr/share/sphinx/
	mv $(SITE_PACKAGES_DIR)/sphinx/themes $(PACKAGE_DIR)/usr/share/sphinx/
	mv $(SITE_PACKAGES_DIR)/sphinx/pycode/Grammar.txt $(PACKAGE_DIR)/usr/share/sphinx/pycode/
	cp sphinx/pycode/Grammar.pickle $(PACKAGE_DIR)/usr/share/sphinx/pycode/
	mv $(SITE_PACKAGES_DIR)/sphinx/ext/autosummary/templates $(PACKAGE_DIR)/usr/share/sphinx/ext/autosummary/
	for lang in `find $(SITE_PACKAGES_DIR)/sphinx/locale\
		-maxdepth 1 -mindepth 1 -type d -printf "%f "`;\
	do\
		mkdir $(PACKAGE_DIR)/usr/share/sphinx/locale/$$lang;\
		mkdir -p $(PACKAGE_DIR)/usr/share/locale/$$lang/LC_MESSAGES;\
		mv $(SITE_PACKAGES_DIR)/sphinx/locale/$$lang/LC_MESSAGES/sphinx.js\
		   $(PACKAGE_DIR)/usr/share/sphinx/locale/$$lang/;\
		mv $(SITE_PACKAGES_DIR)/sphinx/locale/$$lang/LC_MESSAGES/sphinx.mo\
		   $(PACKAGE_DIR)/usr/share/locale/$$lang/LC_MESSAGES;\
		rm -rf $(SITE_PACKAGES_DIR)/sphinx/locale/$$lang;\
	done
	rm -f $(SITE_PACKAGES_DIR)/sphinx/locale/sphinx.pot
	# use packaged jQuery
	dh_link /usr/share/javascript/jquery/jquery.js \
		/usr/share/sphinx/themes/basic/static/jquery.js
	# install scripts not touched by easy install
	install -m 755 $(CURDIR)/sphinx-build.py $(PACKAGE_DIR)/usr/bin/sphinx-build
	install -m 755 $(CURDIR)/sphinx-quickstart.py $(PACKAGE_DIR)/usr/bin/sphinx-quickstart
	install -m 755 $(CURDIR)/sphinx-autogen.py $(PACKAGE_DIR)/usr/bin/sphinx-autogen

binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs CHANGES
	dh_installdocs
	rm -rf $(PACKAGE_DIR)/usr/share/doc/python-sphinx/html/.doctrees
	dh_link /usr/share/doc/python-sphinx/html/_sources/ /usr/share/doc/python-sphinx/rst
	dh_installexamples
	dh_installman
	dh_pysupport
	dh_compress -X.py -X.js -X.rst -X.json -X.doctree -X.txt -Xobjects.inv
	dh_link /usr/share/javascript/jquery/jquery.js \
		/usr/share/doc/python-sphinx/html/_static/jquery.js
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-arch: build install

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