#!/bin/sh
# Rename po files, or move them from a module to another, for every language.
# Author: David Faure <faure@kde.org>

oldlocation="$1"
newlocation="$2"

if test -z "$oldlocation" -o -z "$newlocation"; then
  echo "Usage: $0 <oldlocation> <newlocation>"
  echo "For instance: $0 kdenonbeta/kfoo.po kdereview/kfoo.po"
  echo "          (will move it both in messages/ and docmessages/ if applicable)"
  echo "Don't forget to svn update first!"
  exit 1
fi

template1="templates/messages/${newlocation}t"
template2="templates/docmessages/${newlocation}t"
if test ! -f "$template1" -a ! -f "$template2"; then
  echo "There is no $template1 nor $template2, are you sure?"
  exit 1
fi

## I'm using a tempfile - sh is really confusing. Setting a var inside the loop doesn't work.
dirsToCommit="/tmp/kde-i18n-temp-dirsToCommit-$$"
echo > $dirsToCommit
duplicates="/tmp/kde-i18n-temp-duplicates-$$"
echo > $duplicates

paths=`ls -1 */messages/$oldlocation 2>/dev/null`
paths="$paths `ls -1 */docmessages/$oldlocation 2>/dev/null`"
for oldpath in $paths; do
  newpath=`echo $oldpath | sed -e "s,$oldlocation$,$newlocation,"`
  if test "$oldpath" = "$newpath"; then
    echo "Unexpected error: oldpath=newpath=$oldpath, aborting"
    exit 2
  fi
  echo "$oldpath -> $newpath"
  olddir=`dirname $oldpath`
  newdir=`dirname $newpath`

  if test -f "$newpath"; then
    # Both oldpath and newpath exist already. Figure out what to do.
    if test -n "`msgfmt -o /dev/null --statistics $oldpath 2>&1 | egrep '^0 translated messages'`"; then
      # get rid of the old file
      echo "$newpath already exists, but $oldpath isn't translated, so it will be deleted"
      svn rm $oldpath
      echo $olddir >> $dirsToCommit
      continue
    elif test -n "`msgfmt -o /dev/null --statistics $newpath 2>&1 | grep -v fuzzy | grep -v untranslated`"; then
      # same as above, except the echo
      echo "$newpath already exists and is fully translated, so $oldpath will be deleted"
      svn rm $oldpath
      echo $olddir >> $dirsToCommit
      continue
    elif test -n "`msgfmt -o /dev/null --statistics $newpath 2>&1 | egrep '^0 translated messages'`"; then
      echo "$newpath already exists, but isn't translated -> will be overwritten"
    else
      echo "$oldpath $newpath" >> $duplicates
      echo "ERROR: $newpath already exists!"
      # Add them nonetheless. If you fix the problem by hand, it's convenient to have the full list
      # for committing afterwards.
      echo $olddir >> $dirsToCommit
      echo $newdir >> $dirsToCommit
      continue
    fi
  fi

  echo $olddir >> $dirsToCommit
  echo $newdir >> $dirsToCommit

  if test ! -d $newdir; then
    echo "  creating $newdir"
    mkdir $newdir || exit 3
    svn add $newdir
  fi
  if test -f "$newpath" -a -f "$oldpath"; then
    # svn can't do "svn rm a ; svn mv b a" without an intermediate commit.
    cat $oldpath > $newpath
    svn rm $oldpath || exit 4
  else
    svn mv $oldpath $newpath || exit 5
  fi
done || exit $?

echo "Done, please check and commit."

echo "List of directories:"
cat $dirsToCommit | xargs

duplicatesList=`cat $duplicates`
if test -n "$duplicatesList"; then
  echo "WARNING: the following duplicates were found:"
  echo $duplicatesList
  for i in $duplicatesList; do
    echo "$i: `msgfmt -o /dev/null --statistics $i 2>&1`"
  done
fi

rm -f $dirsToCommit $duplicates
