#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

-include /usr/share/python/python.mk

p	= python-docutils
d	= debian/$(p)
PYTHON	= /usr/bin/python

BUILDHTML_DIRS := .
BUILDHTML_RECURSE_DIRS := docs

build: build-stamp
build-stamp:
	dh_testdir
	$(PYTHON) setup.py build
	set -e && for dir in $(BUILDHTML_DIRS); do \
		PYTHONPATH=.:extras $(PYTHON) tools/buildhtml.py \
			--local --config=tools/docutils.conf $$dir \
			--traceback; \
	done
	set -e && for dir in $(BUILDHTML_RECURSE_DIRS); do \
		PYTHONPATH=.:extras $(PYTHON) tools/buildhtml.py \
			--config=tools/docutils.conf $$dir --traceback; \
	done
ifeq ($(filter nocheck,$(DEB_BUILD_OPTIONS)),)
	set -e && for python in $(shell pyversions -r); do \
		PYTHONPATH=.:extras $$python test/alltests.py; \
	done
endif
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	$(PYTHON) setup.py clean --all
	-find -name '*.py[co]' | xargs rm -f
	rm -f *.html
	rm -rf debian/man.tmp
	find docs -name '*.html' -not -name 'quickref.html' -print0 | \
		xargs -0 --no-run-if-empty rm
	-rm -rf docutils.egg-info extras/docutils.egg-info
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_installdirs
	mkdir -p debian/tmp
	$(PYTHON) setup.py install \
	    --root debian/tmp --no-compile $(py_setup_install_args)
	: # give scripts canonical names in /usr/bin
	cd debian/tmp/usr/bin && \
	mv rst2newlatex.py rst2newlatex && \
	mv rst2html.py rst2html && \
	mv rst2xml.py rst2xml && \
	mv rst2s5.py rst2s5 && \
	mv rst2pseudoxml.py rst2pseudoxml && \
	mv rstpep2html.py rstpep2html && \
	mv rst2odt.py rst2odt && \
	mv rst2odt_prepstyles.py rst2odt_prepstyles && \
	mv rst2man.py rst2man && \
	mv rst2latex.py rst2latex
	install tools/buildhtml.py debian/tmp/usr/bin/rst-buildhtml
	: # Make the man pages (we want to do this *after* renaming all the
	: # tools, because their executable name goes into the --help output)
	install -d debian/man.tmp
	set -e && for exe in rst2html rst2man rst2latex rst2newlatex \
		rst2pseudoxml rst2s5 rst2xml rst-buildhtml rstpep2html \
		rst2odt; do \
		PYTHONPATH=.:extras; \
		export PYTHONPATH; \
		cp debian/$$exe.txt debian/man.tmp/$$exe.txt; \
		$(PYTHON) debian/tmp/usr/bin/$$exe --help \
			>> debian/man.tmp/$$exe.txt;\
		$(PYTHON) tools/rst2man.py debian/man.tmp/$$exe.txt \
			debian/man.tmp/$$exe.1; \
	done
	docbook-to-man debian/rst2odt_prepstyles.sgml > \
		debian/man.tmp/rst2odt_prepstyles.1
	: # Install documentation
	rm -f $(d)/usr/share/doc/$(p)/COPYING[-.]*
	set -e && ( cd docs; find . -name SCCS -prune -o -type d -print ) | \
		while read f ; do install -d $(d)/usr/share/doc/$(p)/docs/$$f ; done
	set -e && ( cd docs; find . -name SCCS -prune -o -type f -print ) | \
		while read f ; do install -o root -m 755 docs/$$f $(d)/usr/share/doc/$(p)/docs/$$f ; done

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installdocs *.txt *.html -XCOPYING
	dh_install -i
	dh_pysupport
	dh_installexamples
	dh_installman
	dh_installchangelogs
	dh_link
	dh_compress -X.py -X.dtd -X.html
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture-dependent files here.
binary-arch: build install
# We have nothing to do by default.

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