#!/bin/sh
set -e

#  Copyright (C) 2006-2009  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

echo "Processing POD . . ."
wrap_langs "[type: pod]" "Emdebian/Grip.pm" "\$lang:doc/pod/\$lang/Emdebian/Grip.pm"
wrap_langs "[type: pod]" "Debian/Packages/Compare.pm" "\$lang:doc/pod/\$lang/Debian/Packages/Compare.pm"

# tdeb em_installtdeb
#doc/man/man1/em_installtdeb.1 ./usr/share/man/man1/
#doc/man/man1/dpkg-gentdeb.1 ./usr/share/man/man1/
#doc/man/man1/dh_gentdeb.1 ./usr/share/man/man1/
#doc/man/man1/splitout_tdeb.1 ./usr/share/man/man1/

for file in em_installtdeb splitout_tdeb dpkg-gentdeb dh_gentdeb; do
	wrap_langs "[type: pod]" "tdeb/$file" "\$lang:doc/tdeb/\$lang/$file"
done

#doc/man/man1/emgrip.1 ./usr/share/man/man1/
#doc/man/man1/apt-grip* ./usr/share/man/man1/
#doc/man/man1/emgrip-build.1 ./usr/share/man/man1/
# grip
for file in apt-grip emgrip ; do
	wrap_langs "[type: pod]" "$file" "\$lang:doc/grip/\$lang/$file"
done

# grip-server
for file in emgrip-edos grip-overridearch.pl \
	em_autogrip grip-overridereplace.pl emgrip-dupes \
	emgrip-remove emgrip-dumpsingle.pl ; do
	wrap_langs "[type: pod]" "$file" "\$lang:doc/server/\$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

# Emdebian::Grip
#pod2html -outfile doc/html/EmdebianGrip.html -title Emdebian::Grip < Emdebian/Grip.pm

# Debian::Packages::Compare
#pod2html -outfile doc/html/DebianPackagesCompare.html -title Debian::Packages::Compare < Debian/Packages/Compare.pm
mkdir -p doc/pod/man/man3/
pod2man Debian/Packages/Compare.pm > doc/pod/man/man3/Debian::Packages::Compare.3
for l in $LANGS; do
	mkdir -p doc/pod/man/$l/man3/
	pod2man doc/pod/$l/Debian/Packages/Compare.pm > doc/pod/man/$l/man3/Debian::Packages::Compare.3
done

# tdeb em_installtdeb
#doc/man/man1/em_installtdeb.1 ./usr/share/man/man1/
#doc/man/man1/dpkg-gentdeb.1 ./usr/share/man/man1/
#doc/man/man1/dh_gentdeb.1 ./usr/share/man/man1/
#doc/man/man1/splitout_tdeb.1 ./usr/share/man/man1/
for file in em_installtdeb splitout_tdeb dpkg-gentdeb dh_gentdeb; do
	mkdir -p doc/tdeb/man/man1/
#	pod2html -outfile doc/html/$file.html -title $file < tdeb/$file
	pod2man tdeb/$file > doc/tdeb/man/man1/$file.1
	for l in $LANGS; do
		mkdir -p doc/tdeb/man/$l/man1/
		if [ -f doc/tdeb/$l/$file ]; then
#			pod2html -outfile doc/html/$file.html -title $file < doc/tdeb/$l/$file
			pod2man doc/tdeb/$l/$file > doc/tdeb/man/$l/man1/$file.1
		fi
		rm -rf doc/tdeb/$l/
	done
done

#doc/man/man1/emgrip.1 ./usr/share/man/man1/
#doc/man/man1/apt-grip* ./usr/share/man/man1/
#doc/man/man1/emgrip-build.1 ./usr/share/man/man1/
# grip
mkdir -p doc/grip/man/man1/
for file in apt-grip emgrip ; do
#	pod2html -outfile doc/html/$file.html -title $file < $file
	pod2man $file > doc/grip/man/man1/$file.1
	for l in $LANGS; do
#		mkdir -p doc/grip/man/$l/man3/
		mkdir -p doc/grip/man/$l/man1/
		if [ -f doc/grip/$l/$file ]; then
#			pod2html -outfile doc/html/$file.html -title $file < doc/grip/$l/$file
			pod2man doc/grip/$l/$file > doc/grip/man/$l/man1/$file.1
		fi
		rm -rf doc/grip/$l/
	done
done

# grip-server
mkdir -p doc/server/man/man1/
for file in emgrip-edos grip-overridearch.pl \
	em_autogrip grip-overridereplace.pl emgrip-dupes \
	emgrip-remove emgrip-dumpsingle.pl ; do
#		pod2html -outfile doc/html/$file.html -title $file < $file
		pod2man $file > doc/server/man/man1/$file.1
	for l in $LANGS; do
#		mkdir -p doc/server/man/$l/man3/
		mkdir -p doc/server/man/$l/man1/
		if [ -f doc/server/$l/$file ]; then
#			pod2html -outfile doc/html/$l/$file.html -title $file < doc/server/$l/$file
			pod2man doc/server/$l/$file > doc/server/man/$l/man1/$file.1
		fi
		rm -rf doc/server/$l/
	done
done

mkdir -p doc/server/man/man3/
pod2man Emdebian/Grip.pm > doc/server/man/man3/Emdebian::Grip.3
for l in $LANGS; do
	mkdir -p doc/server/man/$l/man3/
	if [ -f doc/pod/$l/Emdebian/Grip.pm ]; then
		pod2man doc/pod/$l/Emdebian/Grip.pm > doc/server/man/$l/man3/Emdebian::Grip.3
	fi
done
#tdeb
#for l in $LANGS; do#
#	for file in em_installtdeb; do
#		pod2html -outfile doc/html/$l/$file.html -title $file < doc/pod/$l/$file
#		pod2man doc/pod/$l/$file > doc/man/$l/man1/$file.1
#	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/
XML_CATALOG_FILES="/etc/xml/catalog" \
 xsltproc -o doc/man/ -''-nonet \
 http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl doc/xml/grip-cron.sh.xml
mv doc/man/grip-cron.sh.1 doc/server/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
		mv doc/man/emgrip-build.1 doc/grip/man/$l/man1/
	fi
	if [ -f doc/xml/$l/grip-cron.sh.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/grip-cron.sh.xml
		mv doc/man/grip-cron.sh.1 doc/server/man/$l/man1/
	fi
	rm -rf doc/xml/$l/
done
rm -rf doc/man/

for l in $LANGS; do
	rm -rf doc/pod/$l/
done
