#!/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
}

if [ -f po4a.config ]; then
	cd ../
fi
if [ -f ../po4a.config ]; then
	cd ../../
fi
CONFIG="doc/po4a.config"
# calculate the langs, automatically.
LANGS=`ls po/*.po 2>/dev/null || true`
if [ ! -z "$LANGS" ]; then
	LANGS=`ls 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]" "po/emdebian-grip.pot" "\$lang:po/\$lang.po"

for l in $LANGS; do
	mkdir -p doc/html/$l/
	mkdir -p doc/man/$l/man1/
done
mkdir -p doc/html/
mkdir -p doc/man/man3/
mkdir -p doc/man/man1/

echo "Processing POD . . ."

# Emdebian::Grip
pod2html -outfile doc/html/EmdebianGrip.html -title Emdebian::Grip < Emdebian/Grip.pm
pod2man Emdebian/Grip.pm > doc/man/man3/Emdebian::Grip.3
wrap_langs "[type: pod]" "Emdebian/Grip.pm" "\$lang:doc/\$lang/Emdebian/Grip.pm"

# Debian::Packages::Compare
pod2html -outfile doc/html/DebianPackagesCompare.html -title Debian::Packages::CompareE < Debian/Packages/Compare.pm
pod2man Debian/Packages/Compare.pm > doc/man/man3/Debian::Packages::Compare.3
wrap_langs "[type: pod]" "Debian/Packages/Compare.pm" "\$lang:doc/\$lang/Debian/Packages/Compare.pm"

# tdeb em_installtdeb
for file in em_installtdeb splitout_tdeb dpkg-gentdeb dh_gentdeb; do
	pod2html -outfile doc/html/$file.html -title $file < tdeb/$file
	pod2man tdeb/$file > doc/man/man1/$file.1
	wrap_langs "[type: pod]" "tdeb/$file" "\$lang:doc/pod/\$lang/$file"
done

# grip
for file in apt-grip emgrip emgrip-edos grip-overridearch.pl em_autogrip grip-overridereplace.pl; do
	pod2html -outfile doc/html/$file.html -title $file < $file
	pod2man $file > doc/man/man1/$file.1
	wrap_langs "[type: pod]" "$file" "\$lang:doc/pod/\$lang/$file"
done

echo "Refreshing POT file for translators"
# use -k to create all XML even if untranslated or the XSL breaks the build.
po4a -k 0 $CONFIG
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

rm -f doc/html/tmp
rm -f pod2htmd.tmp
rm -f pod2htmi.tmp
rm -rf doc/pod/
