#! /bin/bash

# This script can be used to update the revision comment in translations
# after an update in an English original. Result is that translators won't
# need to check changes.
# This script is intended only for use by people working on the original
# English text and then only if they know exactly what they are doing!

# The script takes a revision number as argument, looks for English files
# modified in that commit and finds the previous revision of the file.
# It then changes the revision comment for any translation that was
# up-to-date with the previous revision.

[ -d en/ ] || exit 1

if [ "$1" ]; then
	REV="$1"
else
	echo "Usage: unfuzzy-xml <revision>"
	exit 1
fi

cd en/
MODIFIED="$(find . -name "*.xml" | xargs grep "^<.*\$Id:.*$REV" | cut -d: -f1)"
cd ..

for XML in $MODIFIED; do
	PREVREV="$(svn cat -r $(($REV - 1)) en/$XML | grep "^<.*\$Id:" | \
		   sed "s/^.*\.xml \([0-9]*\) .*/\1/")"
	echo "$XML: $PREVREV -> $REV"
	if [ "$PREVREV" ] && [ "$PREVREV" -lt "$REV" ]; then
		sed -i "s/^\(.*original version: \)$PREVREV\(.*\)$/\1$REV\2/" */$XML
	else
		echo "*** Invalid previous revision: '$PREVREV'"
	fi
done
