#!/bin/bash
#   merges new po files with existing ones
#   Copyright (C) 2005 Greek KDE l10n Team.
#   Written by Spiros Georgaras <sngeorgaras@otenet.gr>, 2005.
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2, or (at your option)
#   any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software Foundation,
#   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
function help(){
  echo -n \"$(basename "$0")\"
  echo " scans the local copy of KDE SVN 
repository and finds the po files that have been created
at KDE SVN (remotely), but don't exist in the local copy"
  echo
  echo "New po files will be added to the local copy
of the repository and the remote repository itself
(if user is aythorized to commit)
"
  echo -n "Usage: "
  echo -n $(basename "$0")
  echo " [options]"
  echo
  echo "Valid options are:"
  echo "  -r         report only (take no action)"
  echo "  -c [file]  use compendium file [file]"
  echo "  -n         execute no sanity checks"
  echo "  -h         display this screen and exit"
  echo

}

#### sctipt starts here ####
#
# Copy this to all scripts
if [ -e "$HOME"/.l10n.conf ];then
  . "$HOME"/.l10n.conf
  if [ -z "$installFolder" ];then
    echo `basename "$0"`"> Error: parameter installFolder not set..."
    echo `basename "$0"`"> Please edit ~/.l10n.conf"
    echo
    exit 1
  fi
  if [ ! -d "$installFolder" ];then
    echo `basename "$0"`"> Error: parameter \"installFolder\""
    echo `basename "$0"`"> Value: \"$installFolder\""
    echo  `basename "$0"`" Does not exist or is not a folder"  
    echo `basename "$0"`"> Please edit ~/.l10n.conf"
    echo
    exit 1
  fi
  if [ ! -r "$installFolder" ];then
    echo `basename "$0"`"> Error: parameter \"installFolder\""
    echo `basename "$0"`"> Value: \"$installFolder\""
    echo `basename "$0"`"  No read permission to this folder"
    echo `basename "$0"`"> Please edit ~/.l10n.conf"
    echo
    exit 1
  fi
  . "$installFolder"/l10n-common
  parceConfig
  errorDetected=$?
else
  echo `basename "$0"`"> Error: ~/.l10n.conf - Not found"
  echo "A sample l10n.conf file can be found at
ftp://ftp.i18n.kde.org/teams/el/scripts"
  echo
  exit 1
fi
# End copy
#

reportOnly=0
while getopts ":vhnrc:" Option
do
  case $Option in
    c) useCompendium=true
       compendium="$OPTARG"
       if [ ! -r "$compendium" ];then
         echo "  Error: Compendium file does not exit (or is not readable)"
         exit 1
       fi
       ;;
    r) reportOnly=1;;
    h) # show help
      help
      exit 0;;
    n) # run no checks
      runCheck=false;;
    v) # show version
      showVersion 'S. Georgaras <sngeorgaras@otenet.gr>'
      echo
      exit 0
  esac
done

shift $(($OPTIND - 1))
for n in "$POT_STABLE" "$POT_TRUNK"
do
  cd "$n"
  echo "Checking \"$n\""
  for k in `find . -name "*.pot"`
  do
    chPOT=`echo "$k" | sed 's|^\./||'`
    tmp=`dirname "$chPOT"`
    if [ "$tmp" != "" ] && [ "$tmp" != "." ];then
      tmp=`echo "$n" | grep stable`
      if [ "$tmp" = "" ];then
        chDir="$PO_TRUNK"
      else
        chDir="$PO_STABLE"
      fi
      chPO=`echo "$chDir"/"$chPOT" | sed 's|t$||'`
       if [ ! -e "$chPO" ];then
         echo "  File \"$(basename $(dirname $chPO))/$(basename $chPO)\" has been added to KDE SVN..."
        if [ $reportOnly -eq 0 ];then
          poDir=$(dirname "$chPO")
          addDir=0
          if [ ! -d "$poDir" ];then
            mkdir -p "$poDir"
            if [ ! -d "$poDir" ];then
              echo "    Cannot create folder \"$poDir\""
              echo "    The file will NOT be added in local copy"
              continue
            else
              addDir=1
            fi
          fi
          cp "$n"/"$chPOT" "$chPO"
          if test "useCompendium";then
            echo -n "    Merging translations...   "
            msgmerge -C "$compendium" "$chPO" "$n"/"$chPOT" -o "$chPO".tmp
            mv "$chPO".tmp "$chPO"
          fi
          comeBack="$PWD"
          cd $(dirname "$chPO");cd ..
          canRemove=`svn info |grep URL | sed 's|^URL: ||' | grep "$svnURL"`
          if [ $addDir -eq 0 ];then
            if [ "$canRemove" != "" ];then
              echo -n "    "
              svn add "$(basename $(dirname $chPO))/$(basename $chPO)" && echo "File added on $(date): $chPO" >> "$tmpFolder"/auto-actions.log
            fi
          else
            if [ "$canRemove" != "" ];then
              echo -n "    "
              svn add "$(basename $(dirname $chPO))" && echo "Folder added on $(date): $(dirname $chPO), containing file $(basename $chPO)" >> "$tmpFolder"/auto-actions.log
            fi
          fi
          cd "$comeBack"
        fi
      fi
     fi
  done
done

# for n in `svn st | sed 's|^A  *||'`;do svn --force remove "$n";done
