#!/bin/sh

set -e

echo "Updating DDTP translations:"

PROJECTDIR=/srv/pootle.debian.net/pootle/ddtp
DDTPPODIR=/org/ddtp.debian.net/pos

echo "  Sync files from the DDTP PO directories"
cd "$DDTPPODIR"
LANGS=`find . -maxdepth 1 -mindepth 1 -type d | sed 's/\.\///g'`

TEMPFILE=`tempfile --directory /srv/pootle.debian.net/tmp/`
for lang in $LANGS; do
	echo "    $lang"
	cd "$DDTPPODIR/$lang/sid"
	for srcpkg in `find . -mindepth 2 -maxdepth 2 -type d` ; do
		echo "      $srcpkg"
		for pofile in `ls -1 "$srcpkg"/*.po`; do
			pkg=`basename "$pofile" .po`
			echo "        $pkg"
			if [ -d "$PROJECTDIR/$srcpkg/$pkg" ] ; then
				if [ -f "$PROJECTDIR/$srcpkg/$pkg/$lang.po" ] ; then
					msgcat --use-first "$PROJECTDIR/$srcpkg/$pkg/$lang.po" "$pofile" >"$TEMPFILE" 2>/dev/null
						cp "$TEMPFILE" "$PROJECTDIR/$srcpkg/$pkg/$lang.po"
				else
					cp "$pofile" "$PROJECTDIR/$srcpkg/$pkg/$lang.po"
				fi
			else
				mkdir -p "$PROJECTDIR/$srcpkg/$pkg"
				cp -r "$pofile" "$PROJECTDIR/$srcpkg/$pkg/$lang.po"
			fi
		 done
	done
done

echo "  Cleaning out broken files"
rm -rf "$PROJECTDIR/d/doc-linux-html-pt"
rm -rf "$PROJECTDIR/d/doc-linux-text-pt"
rm -rf "$PROJECTDIR/i/itcl3"

rm "$TEMPFILE" 2>/dev/null || true

