#!/bin/sh
set -e

#  Copyright (C) 2006-2010  Neil Williams <codehelp@debian.org>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

wrap_langs () {
	if [ -z "$LANGS" ]; then
		echo "$1 $2" >> $CONFIG
	else
		echo "$1 $2 $3" >> $CONFIG
	fi
}

while [ -n "$1" ]; do
case "$1" in
	--pot-only)
		POTONLY=1
		shift
	;;
	*)
		com=$1
		echo "Unrecognised command: \$com"
	exit;
	;;
esac
done

CONFIG="doc/po4a.config"
# calculate the langs, automatically.
LANGS=`ls doc/po/*.po 2>/dev/null || true`
if [ ! -z "$LANGS" ]; then
	LANGS=`ls doc/po/*.po | sed -e 's/.*\/\(.*\)\.po/\1 /' || true`
fi
if [ ! -z "$LANGS" ]; then
	# LANGS should not include the specification, just the root.
	LANGS=`echo $LANGS | sed -e 's/_.*//'`
	LANGS=`echo $LANGS | sed -e 's/@.*//'`
	LANGS=`echo $LANGS | sed -e 's/\+.*//'`
	echo "[po4a_langs] $LANGS" > $CONFIG
	else
	echo > $CONFIG
fi
wrap_langs "[po4a_paths]" "doc/po/emdebian-grip.pot" "\$lang:doc/po/\$lang.po"

echo "Processing Docbook XML . . . "
for file in `ls doc/xml/*.xml`; do
	xmllint --format "$file" --output "$file"
	LOCAL=`basename $file`
	wrap_langs "[type:docbook]" "$file" "\$lang:doc/xml/\$lang/$LOCAL"
done

# grip
for file in apt-grip emgrip ; do
	wrap_langs "[type: pod]" "$file" "\$lang:doc/grip/\$lang/$file"
done

echo "Refreshing POT file for translators"
if [ "$POTONLY" = "1" ]; then
	po4a --no-translations $CONFIG
	rm $CONFIG
	exit 0
else
# use -k to create all XML even if untranslated or the XSL breaks the build.
	po4a -k 40 $CONFIG
fi

# grip
mkdir -p doc/grip/man/man1/
for file in apt-grip emgrip ; do
	pod2man --utf8 $file > doc/grip/man/man1/$file.1
	for l in $LANGS; do
		mkdir -p doc/grip/man/$l/man1/
		if [ -f doc/grip/$l/$file ]; then
			pod2man --utf8 doc/grip/$l/$file > doc/grip/man/$l/man1/$file.1
		fi
	done
done

# just make sure the XML catalog is available for XSL.
# never go to the internet for build config, use -nonet.
XML_CATALOG_FILES="/etc/xml/catalog" \
 xsltproc -o doc/man/ -''-nonet \
 http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl doc/xml/emgrip-build.xml
mv doc/man/emgrip-build.1 doc/grip/man/man1/
for l in $LANGS; do
	if [ -f doc/xml/$l/emgrip-build.xml ]; then
		XML_CATALOG_FILES="/etc/xml/catalog" \
		 xsltproc -o doc/man/ -''-nonet \
		 http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl doc/xml/$l/emgrip-build.xml
		if [ -f doc/man/emgrip-build.1 ]; then
			mv doc/man/emgrip-build.1 doc/grip/man/$l/man1/
		fi
	fi
done

for l in $LANGS; do
	rm -rf doc/server/$l
	rm -rf doc/grip/$l
	rm -rf doc/tdeb/$l
	rm -rf doc/xml/$l/
	rm -rf doc/pod/$l/
	rm -rf doc/man/$l
done
