#!/bin/sh

# This script is used for translations using .po files.
# It creates the initial .po files for a language where there
# already is a translation.

# This script is meant to be used only once for the transition
# from translating the .xml files to using .po files.

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

language=${1:-pl}

[ -d ./$language/ ] || exit 1

SCRIPTDIR="./scripts"
WORKDIR="./integrated"
SOURCEDIR="$WORKDIR/$language"
PODIR="./po"

if [ ! -d "$PODIR" ] ; then
    echo "Error: directory $PODIR does not exist."
    exit 1
fi

mkdir -p $PODIR/$language
if [ -n "$PODIR/$language/*.po" ] ; then
    echo "Deleting old PO files for '$language'..."
    rm $PODIR/$language/*.po
fi

XMLLIST=$(find $SOURCEDIR -name "*.xml")

echo "Creating PO files for language '$language'..."

export NO_CREDITS=1      # Repress KDE specific credits
                         # Note: not yet supported by split2po

for SOURCEXML in $XMLLIST ; do
    SUBDIR=$(dirname $SOURCEXML | sed "s:$SOURCEDIR::" | sed "s:^/::")
    XML=$(basename $SOURCEXML)
    ORIGXML=$WORKDIR/en/$SUBDIR/$XML
    PO=$PODIR/$language/$(basename $SOURCEXML .xml).po

    echo "Converting translated $SUBDIR/$XML to PO file"
    split2po $ORIGXML $SOURCEXML >$PO
    if [ $? -ne 0 ] ; then
        echo "** Error during conversion."
        continue
    fi
done

# Check the results
echo ""
echo "Checking whether translation matches corresponding POT file..."
for PO in `find $PODIR/$language -name "*.po"` ; do
    POT="$PODIR/pot/$(basename $PO .po).pot"
    if [ -s $PO ] ; then
        if [ -f $POT ] ; then
            count_POT=$(egrep "^msgid " $POT | wc -l)
            count_PO=$(egrep "^msgstr " $PO | wc -l)
            if [ $count_PO != $count_POT ] ; then
                echo "** Warning: translation for $PO has $count_PO strings, while original has $count_POT strings."
            fi
            # Missing strings: If a line with 'msgstr ""' is followed by an empty line.
            count_missing_PO=$(egrep -A 1 "^msgstr \"\"$" $PO | egrep "^$" | wc -l)
            if [ $count_missing_PO -ne 0 ] ; then
                echo "** Warning: translation for $PO has $count_missing_PO missing strings."
            fi
        else
            echo "** Error: corresponding POT file not found for $PO."
        fi
    else
        echo "** Error: $PO is empty (conversion error)."
    fi
done
echo "Done."

# Checking for untranslated strings
echo ""
echo "Checking for untranslated strings in the PO files..."
for PO in `find $PODIR/$language -name "*.po"` ; do
    echo "Checking $PO..."
    awk -f $SCRIPTDIR/mark_untranslated.awk $PO
done

echo ""
echo "The conversion has finished successfully."
echo "The PO files for $language have been saved in '$PODIR/$language'."
echo ""
echo "Please check all messages above very carefully."
echo "If any translations are shown to have a different amount of strings than the original,"
echo "you should probably correct the cause of this and run the conversion again."
echo ""
echo "Strings that are shown as 'looking untranslated', this could just be a string that"
echo "does not need translation, but could also indicate parts of the original that really"
echo "are untranslated but are not marked as such."
echo "In that case, you can use the set_untranslated script to mark these strings as"
echo "untranslated (enter 'set_untranslated --help' for usage)."

rm -f /tmp/tmp.po.$$ /tmp/$$.xml
exit 0
