#!/bin/sh

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

if [ "$1" = "--help" ] ; then
    echo "Usage: $0 <language>"
    exit 0
fi
if [ -z "`which msgmerge 2>/dev/null`" ] ; then
    echo "ERR: Msgmerge not found, please install the gettext package"
    exit 1
fi

language=${1:-pl}

SPODIR="./po"
BUILDDIR="./build"
if [ -z "$PO_USEBUILD" ] ; then
    PODIR=$SPODIR
else
    PODIR="$BUILDDIR/build.po"
    mkdir -p $PODIR/$language
fi
RET=0

[ -d "$PODIR" ] || exit 1

echo "Updating PO files for language '$language':"
for POT in `find $PODIR/pot -name "*.pot"` ; do
    BASENAME="$(basename $POT .pot)"
    PO=$PODIR/$language/$BASENAME.po
    SPO=$SPODIR/$language/$BASENAME.po

    if [ -f $SPO ] ; then
        echo "- updating $BASENAME.po"
        if [ -z "$PO_USEBUILD" ] ; then
            # Update existing PO file
            msgmerge -q -U --backup=simple $PO $POT
            RC=$?
        else
            # Generate temporary PO file in build directory
            msgmerge -q $SPO $POT -o $PO
            RC=$?
        fi
        if [ $RC -ne 0 ] ; then
            RET=$RC
            echo "Error: error $RC while executing msgmerge"
        fi
    else
        echo "Warning: no PO file found for '$BASENAME'."
    fi
done

exit $RET
