#!/bin/bash

# XXX override this:
PACKAGE=$(basename $PWD)

TRAN=translations
RES=res/values
POT=$TRAN/$PACKAGE.pot
PODIR=translations/$PACKAGE
PO=$PODIR/$PACKAGE-
CREDITS=res/values/translators.xml

download() {
	FN=launchpad-$(date +%F).tar.gz
	pushd "$TRAN"
	wget "$1" -O "$FN"
	tar xvzf "$FN"
	mv translations/*.po $PACKAGE/
	rename -f 's/@[^.]*//' $PACKAGE/*.po
	popd
}

translate_xml2pot() {
	if [ -f $POT ] ; then
		xml2po -a -u $POT $RES/strings.xml
	else
		xml2po -a -o $POT $RES/strings.xml
	fi
}

translate_po2xml() {
	if [ ! -d "translations/" ] ; then
		echo "You need to download the translations package from launchpad first."
		echo "Get it from https://translations.launchpad.net/$PACKAGE and store"
		echo "the .po files as $PO##.po"
		exit 1
	fi
	for po in $PO*.po; do
		lang=${po##$PO}
		lang=${lang%%.po}	# strip .po
		lang=${lang/_/-r}	# fix up lang_Locale -> lang-rLocale
		dir=$RES-$lang
		# fix Java f'ups <http://developer.android.com/reference/java/util/Locale.html>
		[ "$lang" == "he" ] && dir=$RES-iw
		[ "$lang" == "id" ] && dir=$RES-in
		[ "$lang" == "yi" ] && dir=$RES-ji
		echo $lang: $dir
		mkdir -p $dir
		xml2po -a -l $lang -p $po $RES/strings.xml | sed "s/'/\\\\'/g" > $dir/strings.xml
	done
	sed -i 's/\\\\'\''/\\'\''/g' res/values-*/strings.xml
	[ -f $CREDITS ] && {
		cat <<EOF
<?xml version="1.0" encoding="utf-8"?>
<!-- AUTOGENERATED BY xml2po.sh! DO NOT CHANGE MANUALLY! -->
<!-- APRSdroid translators. Autogenerated by xml2po.sh -->
<resources>
<string name="translation_credits">\\n
$(cat translations/$PACKAGE/$PACKAGE-*.po | awk -F ': | <' '/Last-Translator:/ { if ($2 != "FULL NAME") { print $2 "\\n"; } }')
</string>
</resources>
EOF
	} > $CREDITS
}


if [ "$1" = "xml2pot" ]; then
	translate_xml2pot
elif [ -n "$1" ] ; then
	download "$1"
	translate_po2xml
else
	translate_po2xml
fi
