#!/bin/bash
# msgdiff old.po new.po additions.po fixes.po [fixes_review.po]
# Compare old.po and new.po and generate fixes.po that contains only
# fixes and additions.po that contains only additions.
# If fixes_review.po is specified, then generate also human readable po
# differences review file.

RC=0
OLD_HEADER=false
if test "$1" = "--old-header" ; then
    OLD_HEADER=true
    shift
fi
PO_OLD=$1
PO_NEW=$2
PO_ADD=$3
PO_FIX=$4
if test -n "$5" ; then
    PO_REV=$5
    PO_REV_PRESENT=true
else
    PO_REV_PRESENT=false
fi

TMP=$HOME/.msgdiff$$

trap "rm -rf $TMP" 0
mkdir -p $TMP

# Join both translations, string changes become fuzzy.
msgcat --force-po "$PO_OLD" "$PO_NEW" -o $TMP/allfz.po
# Join both translations, use new in case of change.
msgcat --use-first --force-po "$PO_NEW" "$PO_OLD" -o $TMP/all.po

# Compose the best po file header.
if $OLD_HEADER ; then
    ${0%msgdiff}msgheadermerge "$PO_NEW" "$PO_OLD" $TMP/header.po --newdate
else
    ${0%msgdiff}msgheadermerge "$PO_OLD" "$PO_NEW" $TMP/header.po --newdate
fi

# step 4: Find string fixes (existed before, now different).
msgcat --unique --force-po $TMP/all.po $TMP/allfz.po -o $TMP/fix.po
sed -i '/#~/d' $TMP/fix.po
msgcat --use-first $TMP/header.po $TMP/fix.po -o "$PO_FIX"

# step 5: Find newly translated strings (translation removal is not supported).
msgcat --unique --force-po "$PO_OLD" $TMP/all.po -o $TMP/add.po
sed -i '/#~/d' $RPMPODIR/$TMP/add.po
msgcat --use-first $TMP/header.po $RPMPODIR/$TMP/add.po -o "$PO_ADD"

if $PO_REV_PRESENT ; then
    msgmerge --force-po $TMP/allfz.po $TMP/fix.po -o $TMP/review.po
    sed -i '/#~/d' $TMP/review.po
    msgcat $TMP/review.po -o "$PO_REV"
fi