#!/bin/sh
# Removed -x to avoid 45MiB emails (20110328, faw)

. `dirname $0`/cfg/common
. `dirname $0`/cfg/`basename $0`

##echo "`basename $0`: Updating debconf translations..."

TMPDIR=/srv/pootle.debian.net/tmp
export TMPDIR
TEMPDIR=`mktemp -d -p "$TMPDIR" sync.XXXXXX`

cd "$POOTLEDEBCONFDIR"
svn -q up

[ -f "$TARBALL" ] || exit 1

# First we extract the tarball in the temporary directory
##echo "  Extracting files from $TARBALL..."
cd "$TEMPDIR"
umask 002
tar xfz "$TARBALL"
mv po/unstable/* .
rm -rf po

# Then we extract the PO Files
##echo "  Uncompress/update/check extracted files..."
for pkg in `find . -mindepth 3 -maxdepth 3 -type d` ; do
	# Files are originally in debian/po. Move them in the root
	# of the directory
	if [ ! -d "$pkg/debian/po/" ]; then
		continue
	fi
	mv "$pkg"/debian/po/*gz "$pkg" 2>/dev/null
	# And clean out the remaining stuff
	rm -rf "$pkg/debian" 2>/dev/null
	# Uncompress files
	if [ `\ls -1 "$pkg"/*gz 2>/dev/null | wc -l` != "0" ] ; then
	  gzip -d "$pkg"/*gz
	fi
	# Now we should rename those files to $LANGUAGE.po
	# We need to strip out the package name and version
	# The name of the templates.pot file will give it to us
	for pot in `\ls -1 "$pkg"/*templates.pot 2>/dev/null`; do
		root=`basename "$pot" templates.pot`
		for pots in `\ls -1 "$pkg"/*.pot 2>/dev/null`; do
		        # Rename the file to "templates.pot"
			mv "$pots" $pkg/`basename $pots | sed "s/$root//g"`
		done
		for po in `\ls -1 "$pkg"/*.po 2>/dev/null`; do
		        # Convert the file to UTF-8 and rename it by
		        # stripping out the package name and version
		        # If something fails, report
			if msgconv --to-code=utf-8 "$po" >$pkg/`basename "$po" | sed "s/$root//g"` 2>/dev/null; then
				rm "$po"
			else
				rm $pkg/`basename "$po" | sed "s/$root//g"` >/dev/null 2>&1 || true
				rm "$po"
				echo "      Error while working on $po"
			fi
		done
	done
	# Now we have a directory with only $LANGUAGE.po and templates.pot
	TEMPFILE=`tempfile --directory "$TMPDIR"`
	# Now we update the file in Pootle
	# We need to merge the file from the tarball with
	# the file in Pootle (that one could have been changed by a translator
	# 
	# Of course, if the directory does not exist in Pootle (new
	# package with debconf), we will just add it
	# and copy files from the tarball
	if [ -d "$POOTLEDEBCONFDIR/$pkg" ] ; then
	        # So, there's something in Pootle
	        # Cycle over all PO files
		for pofile in `\ls -1 "$pkg"/*.po 2>/dev/null` ; do
			filename=`basename "$pofile"`
			if [ -f "$POOTLEDEBCONFDIR/$pkg/$filename" ] ; then
			        # Give priority to the file in Pootle
				# Ignore errors (e.g. wrong CHARSET because the file is empty)
				msgcat --use-first "$POOTLEDEBCONFDIR/$pkg/$filename" "$pofile" >"$TEMPFILE" 2>/dev/null || true
				# And merge with the POT file from the tarball
				msgmerge -U "$TEMPFILE" "$POOTLEDEBCONFDIR/$pkg/templates.pot" >/dev/null 2>&1
				# And overwrite the Pootle file
				cp "$TEMPFILE" "$POOTLEDEBCONFDIR/$pkg/$filename"
			else
			        # Maybe *that* file didn't exist in Pootle
			        # In such case, just copy it
				cp "$pofile" "$POOTLEDEBCONFDIR/$pkg"
				msgmerge -U "$POOTLEDEBCONFDIR/$pkg/$filename" "$POOTLEDEBCONFDIR/$pkg/templates.pot"  >/dev/null 2>&1
			fi
		done
	else
	        # There was no directory in Pootle yet, just copy
		mkdir -p "$POOTLEDEBCONFDIR/$pkg"
		cp -r "$pkg"/* "$POOTLEDEBCONFDIR/$pkg"
	fi
	rm "$TEMPFILE" 2>/dev/null || true
done

# Now we have the Pootle directory updated with files from the tarball
# Let's do some cleaning'

# When there are known broken files, let's clean them
##echo "  Cleaning out broken files..."
for file in $BROKEN; do
##	echo "    $file"
	rm "$POOTLEDEBCONFDIR/$file"
done

# Some packages should be ignored for various reasons
##echo "  Cleaning out ignored packages..."
for pkg in $IGNORE; do
##	echo "    $pkg"
	rm -rf `find "$POOTLEDEBCONFDIR" -mindepth 3 -maxdepth 3 -name "$pkg"` #2>/dev/null
done

# And some packages provide useless garbage 
##echo "  Cleaning out garbage..."
for dir in $GARBAGE; do
##	echo "    $dir"
	rm -rf "$POOTLEDEBCONFDIR/$dir"
done

# What languages are present?
LANGUAGES=`find "$POOTLEDEBCONFDIR" -name \*.po | cut -f9 -d\/ | sort | uniq | cut -f1 -d\.`
# This will allow automatically adding a language as soon as *one* package
# provides a file for it

# Now we fill in all packages directories with one file per language
# That's slightly suboptimal but this is the only way to have correct
# statistics in Pootle
##echo "  Updating Pootle files..."
for pkg in `find "$POOTLEDEBCONFDIR" -maxdepth 3 -mindepth 3 -type d | grep -v \\.svn` ; do
	for lang in $LANGUAGES; do
		if [ ! -f "$pkg/$lang.po" ] ; then
			cp "$pkg/templates.pot" "$pkg/$lang.po"
		else
			msgmerge -U "$pkg/$lang.po" "$pkg/templates.pot" >/dev/null 2>&1
		fi
	done
done


# Now commit the whole stuff to SVN
##echo "  Committing to SVN..."
cd "$POOTLEDEBCONFDIR"
svn -q up
# echo "   Committing updated files"
svn -q commit -m"[SILENT_COMMIT] Update existing files for `date +%Y%m%d%H%M`"
# find . -type d | grep -v \\.svn | xargs svn add -N 2>/dev/null
# echo "   Adding new directories"
# find . -type d | grep -v \\.svn | xargs svn add -N 2>/dev/null
# echo "   Adding PO files"
# find . -type f -name \*.po | grep -v \\.svn | xargs svn add 2>/dev/null
# echo "   Adding POT files"
# find . -type f -name templates.pot | grep -v \\.svn | xargs svn add 2>/dev/null

# Now we need to find out what directories are in $POOTLEDEBCONFDIR but
# aren't anymore in the tarball. Most of the time, this will be
# packages that dropped po-debconf support
##echo "  Finding added/removed files/directories..."
cd "$POOTLEDEBCONFDIR"
find . -mindepth 3 -maxdepth 3 -type d | grep -v \\.svn | sort > "$TMPDIR/list-pootle"
cd "$TEMPDIR"
find . -mindepth 3 -maxdepth 3 -type d | sort > "$TMPDIR/list-tarball"
# We display the diff. It will be up to a human to delete/add
# the relevant dirs in SVN (too dangerous)
diff -d -I "lintian" -I "powertweak" "$TMPDIR/list-tarball" "$TMPDIR/list-pootle" > "$TMPDIR/diff" || true
if [ -n "`cat $TMPDIR/diff`" ] ; then
	cd "$POOTLEDEBCONFDIR"
	echo "  SVN commands to issue:"
	for i in `grep -E "^>" "$TMPDIR/diff" | cut -f2 -d" "` ; do
		svn rm --force $i
		svn commit -m"Removed $i after syncing with unstable packages" $i
	done
fi

# Fix permissions: g+w on everything so that (for instance)
# members of the pootle group may commit, including the pootle user
cd "$POOTLEDEBCONFDIR"
find . -print0 | xargs --null chmod g+w 2>/dev/null

# Cleaning out
#Temporarily disabled to solve out a few issues
rm "$TMPDIR/list-tarball" "$TMPDIR/list-pootle" >/dev/null 2>&1 || true
rm -rf "$TEMPDIR" >/dev/null 2>&1 || true

