#!/bin/sh
echo "Quick-n-dirty download script over git for translators."
echo
echo "Keep an eye over the Transifex installation on Xfce, it is soon to be "
echo "available and will allow to reduce the time needed to get your "
echo "translations uploaded :-)"
echo
echo "For more info join the xfce-i18n mailing-list (http://xfce.org/)"
echo

if test -z "$1"; then
	echo "Error: missing argument..."
	echo "Enter the locale to download (e.g. it, de, fr, pt_BR, ...)"
	exit 1
fi

LOCALE=$1
INDEX=$(wget http://git.xfce.org/ -O - 2> /dev/null)

echo "The script is ready to download $LOCALE.po files from all projects"
echo "to xfce/ apps/ panel-plugins/ and thunar-plugins/."
echo -n "Shall it proceed? [y/N] "
read TEST
test "$TEST" != "y" && exit 0

download() {
	echo
	echo "Download of the $1 collection will start in 2 seconds . . ."
	echo
	sleep 2

	PROJECTS=$(echo $INDEX | egrep -o "$1/[a-z0-9-]+" | sort -u)
	for i in $PROJECTS; do
		# Why on earth does curl exit with 0 on 404
		if test ! -z "$(curl -s -I http://git.xfce.org/$i/plain/po/${i#*/}.pot | grep '404 Not found')"; then
			continue
		fi
		echo "Downloading $i..."
		# Download the files via cgit
		mkdir -p $i
		curl -s http://git.xfce.org/$i/plain/po/$LOCALE.po -o $i/$LOCALE.po
		curl -s http://git.xfce.org/$i/plain/po/${i#*/}.pot -o $i/${i#*/}.pot
	done
}
download xfce
download apps
download panel-plugins
download thunar-plugins
