File gnome-patch-translation-update of Package gnome-patch-translation

#! /bin/bash

if test "$1" = "--help" ; then
    echo "Patch translation update tool works in top directory of unpacked source code."
    echo "$0 is an utility that is called after applying of all patches."
    echo "Usage: $0 [translation_directory] [translation_domain] [command to create pot file]"
fi

set -o errexit
shopt -s nullglob

TOP_DIR=$PWD
DIR=${1:-po}
DOMAIN=$2
if test -d "$1" ; then
	EXT=."$1"
else
	EXT=
fi
cd "$DIR"

# This action takes some time. But run it every time to confirm, that update is still needed.
if test -z "$3" ; then
    if test -z "$DOMAIN" ; then
	intltool-update --pot
    else
	intltool-update --gettext-package=$DOMAIN --pot
    fi
else
    eval $3
fi

POT_NOT_UNIQUE=false
MISSING=true
for POT in *.pot ; do
    if $POT_NOT_UNIQUE ; then
	echo "ERROR: Directory $DIR contains more than one .pot file."
	exit 1
    fi
    POT_NOT_UNIQUE=true
    MISSING=false
    POT=${POT%.pot}
done
if $MISSING ; then
    echo "ERROR: Directory $DIR does not contains any .pot file."
    exit 1
fi

mkdir $TOP_DIR/gnome-patch-translation$EXT/new
cp -a $POT.pot $TOP_DIR/gnome-patch-translation$EXT/new

cd $TOP_DIR/gnome-patch-translation$EXT

msgcomm --unique -o $POT-unique.pot old/$POT.pot new/$POT.pot
if test $? -eq 0 -a ! -f $POT-unique.pot ; then
    echo ""
    echo "No unique patch specific strings in project!"
    echo "You can remove gnome-patch-translation support from this directory."
    echo ""
    exit 1
fi
msgcomm --more-than=1 -o $POT-patch.pot $POT-unique.pot new/$POT.pot

rm -rf new old $POT-unique.pot

cd "$TOP_DIR/$DIR"

# Main action. Only this is really needed for package rebuild.
for PO in *.po ; do
    LNG=${PO%.po}
    if test -f /usr/share/gnome-patch-translation/$LNG.po ; then
	echo "Updating $PO using gnome-patch-translation compendium."
	if test "$COMPENDIUM_UPDATE_MODE" = 1 ; then
	    msgmerge --compendium=/usr/share/gnome-patch-translation/$LNG.po -o $PO.new $PO $POT.pot
	else
	    # For compilation use --no-fuzzy-matching to run faster.
	    msgmerge --no-fuzzy-matching --compendium=/usr/share/gnome-patch-translation/$LNG.po -o $PO.new $PO $POT.pot
	fi
	mv $PO.new $PO
    fi
done
# This action takes long time. Run it only if we really need it.
if test "$COMPENDIUM_UPDATE_MODE" = 1 ; then
    sed -i '/^#:/s@ \.\./@ '$POT'/@' $TOP_DIR/gnome-patch-translation$EXT/$POT-patch.pot
    for PO in *.po ; do
	LNG=${PO%.po}
	echo "Creating fuzzy $PO for gnome-patch-translation."
	msgmerge -o $TOP_DIR/gnome-patch-translation$EXT/$POT-$LNG.po $PO $TOP_DIR/gnome-patch-translation$EXT/$POT-patch.pot
	sed '/^#:/s@ \.\./@ '$POT'/@' <$TOP_DIR/gnome-patch-translation$EXT/$POT-$LNG.po >$TOP_DIR/gnome-patch-translation$EXT/$POT-$LNG.po.new
	msgattrib --no-obsolete --force-po $TOP_DIR/gnome-patch-translation$EXT/$POT-$LNG.po.new -o $TOP_DIR/gnome-patch-translation$EXT/$POT-$LNG.po
	rm $TOP_DIR/gnome-patch-translation$EXT/$POT-$LNG.po.new
    done
    echo "================================================================="
    echo " Translatable strings from patches were written to"
    echo " gnome-patch-translation$EXT/."
    echo " If you did any translatable string change in any patch,"
    echo " please follow HOWTO in gnome-patch-translation package source."
    echo "================================================================="
else
    echo " Skipping compendium update. Use COMPENDIUM_UPDATE_MODE=1 to force it."
fi