#!/bin/sh

# note: in order to run this script you need the programs 'pngtopnm' and 'pnmtops'
# both of which are available (in debian) as part of the 'netpbm' package (as of 2004-05-27)
# Also required is the 'ps2pdf13' program, part of the (debian) package
# 'gs-common' (as of 2004-07-30).

VERSION=$1

set -e

templ=doc/core/howto/template.tpl

LORE="./bin/lore/lore --config baseurl=http://twistedmatrix.com/documents/current/api/%s.html"

echo "Processing manual pages"
for man in doc/*/man; do
	echo $'\t'"$man"
	$LORE -p --input man --output lore --config ext=-man.xhtml $man/*.1
done

for doc in doc/core doc/core/{man,examples,howto,howto/tutorial,howto/policy,specifications,upgrades,upgrades/2.0} doc/{conch,flow,lore,mail,names,pair,web,words}/{examples,howto} doc/mail/tutorial/smtpclient doc/development doc/development/{policy} ; do
	if [ -e "$doc" ]; then
		echo "Processing $doc"
		echo $'\t'"Lint"
		$LORE -n --output lint $doc/*.xhtml |sed 's/^/WARNING: /'
		echo $'\t'"HTML"
		$LORE \
			-p --config template=$templ --config ext=.html \
			--config version=$VERSION \
			-l $(echo $doc | sed -e 's/^doc\///' | sed -e 's/\(^\|\/\)[^.\/]\+/\1../g')/../core/documentation/howto/ \
			$doc/*.xhtml
		echo $'\t'"LaTeX"
		$LORE -p --output latex --config section $doc/*.xhtml
	else
		echo "Skipping non-existent $doc"
	fi
done

cd doc/core/howto

for i in 1 2; do latex book.tex < /dev/null; done

latex book.tex </dev/null | sed '/^LaTeX Warning:/s/^/WARNING: /'

dvips -o book.ps book.dvi
ps2pdf13 book.ps book.pdf

