#!/bin/sh

# This script is used for translations using .po files.
# It updates .pot files after changes in the original English
# .xml files.
# The script 'merge_xml' should be run before this script!

if [ "$1" = "--help" ] ; then
    echo "Usage: $0"
    exit 0
fi

if [ -z "`which xml2pot 2>/dev/null`"  ] ; then
   echo "ERR: xml2pot not found, please install the poxml package"
   exit 1
fi

BUILDDIR="./build"
if [ -z "$PO_USEBUILD" ] ; then
    WORKDIR="./integrated"
    PODIR="./po"
else
    WORKDIR="$BUILDDIR/build.po"
    PODIR="$BUILDDIR/build.po"
fi
SOURCEDIR="$WORKDIR/en"
RET=0

[ -d $SOURCE ] || exit 1
mkdir -p $PODIR/pot

# This check is broken!
if [ -n "$(find $PODIR/pot/ -name *.pot 2>/dev/null)" ] ; then
    echo "Deleting old POT files..."
    rm $PODIR/pot/*.pot
fi

for XML in `find $SOURCEDIR -name "*.xml"` ; do
    echo "Creating new POT file for $XML"
    POT=$(basename $XML .xml).pot

    xml2pot $XML >$PODIR/pot/$POT
    RC=$?
    if [ $RC -ne 0 ] ; then
        RET=$RC
        echo "Error: error $RC while executing xml2pot"
    fi
done

exit $RET
