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

include /usr/share/python/python.mk

export PYTHONWARNINGS=d

here = $(dir $(firstword $(MAKEFILE_LIST)))/..
debian_version = $(word 2,$(shell cd $(here) && dpkg-parsechangelog | grep ^Version:))
upstream_dfsg_version = $(firstword $(subst -, ,$(debian_version)))

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

javascript_path = /usr/share/javascript/sphinxdoc/1.0/

build-arch:

build-indep build: build-stamp

build-stamp:
	dh_testdir
	ln -s /usr/share/javascript/jquery/jquery.js sphinx/themes/basic/static/
	ln -s /usr/share/javascript/underscore/underscore.js sphinx/themes/basic/static/
	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
	xvfb-run ./debian/jstest/run-tests
endif
	touch build-stamp

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

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/
	set -e; \
	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
	# 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
	# Move JavaScript code to libjs-sphinxdoc:
	set -e; \
	for js in $$(find $(PACKAGE_DIR)/usr/share/sphinx/themes/ -name '*.js'); do \
		mv $$js debian/libjs-sphinxdoc/$(javascript_path); \
		ln -sf "$(javascript_path)/$${js##*/}" $$js; \
	done
	cd debian/dh-sphinxdoc/ && pod2man -c Debhelper -r '' dh_sphinxdoc dh_sphinxdoc.1

binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs CHANGES
	dh_installdocs
	dh_install
	./debian/dh-sphinxdoc/dh_sphinxdoc /usr/share/doc/python-sphinx/html/
	dh_installexamples
	dh_installman
	dh_pysupport
	dh_compress -X.py -X.js -X.rst -X.json -X.txt -Xobjects.inv
	dh_link
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-arch: build install

binary: binary-indep binary-arch

get-orig-source:
	sh $(here)/debian/get-orig-source.sh $(upstream_dfsg_version)

.PHONY: build-arch build-indep build clean binary-indep binary-arch binary install get-orig-source
