#!/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 "Don't forget to cvs update first!"
  exit 1
fi

template="templates/${newlocation}t"
if test ! -f "$template"; then
  echo "There is no $template, 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

find . | grep $oldlocation'$' | while read oldpath; 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 | grep '0 translated messages'`"; then
      # get rid of the old file
      echo "$newpath already exists, but $oldpath isn't translated, so it will be deleted"
      cvs rm -f $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"
      cvs rm -f $oldpath
      echo $olddir >> $dirsToCommit
      continue
    elif test -n "`msgfmt -o /dev/null --statistics $newpath 2>&1 | grep '0 translated messages'`"; then
      # do nothing here. $oldpath will overwrite this useless $newpath
      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
    cvs add $newdir
  fi
  mv $oldpath $newpath || exit 4
  cvs rm $oldpath
  cvs add $newpath
done

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
