#!/bin/bash
#   Commits changes to both l10n branches
#   Copyright (C) 2009 Greek KDE l10n Team.
#   Written by Spiros Georgaras <sngeorgaras@otenet.gr>, 2009.
#
#   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 "A tool to change the active branch name for the Greek (el) l10n team site"
  echo
  echo "Usage: $(basename $0) [options]"
  echo
  echo "Valid options are:"
  echo "  -s  change active branch to stable"
  echo "  -t  change active branch to trunk"
  echo "  -h  display this screen and exit"
  echo "  -v  display version info and exit"
  echo
  echo
}

#
# Script 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
#
msgCommandLine="# $(basename $0)"
while getopts ":vhst" Option
do
  case $Option in
  h) # show help
    help
    exit 0;;
  v) # show version
    showVersion 'S. Georgaras <sngeorgaras@otenet.gr>'
    echo
    exit 0;;
  s)
    activeBranch=stable;;
  t)
    activeBranch=trunk;;
   *)
     echo "  Error: parameter -$OPTARG not supported!!!"
     exit 1
  esac
done
shift $(($OPTIND - 1))
# if [ $# -eq 0 ];then
#   help
#   exit 1
# fi


if [ -z "$activeBranch" ];then
  help
  exit
fi

if [ ! -d "$cvsDIR"/tmp ];then
  mkdir "$cvsDIR"/tmp 2>/dev/null
  if [ ! -d "$cvsDIR"/tmp ];then
    echo "  Error: Cannont create \"$cvsDIR\"/tmp"
    exit 1
  fi
fi
cd "$cvsDIR"
# set -x
for aFile in *.html
do
  # bring control fiends in one line

sed 's|</[tT][iI][tT][lL][eE]>|</title>\n|
/<[Bb][Oo][Dd][yY]/,/<.[Bb][Oo][Dd][yY]>/{
:a
N
s/\n/ /
ta
}' "$aFile" > "$aFile".tmp && mv "$aFile".tmp "$aFile"

  sed "s|<!-- active branch name -->.*<!-- end of active branch name -->|<!-- active branch name -->"$activeBranch"<!-- end of active branch name -->|" "$aFile" > tmp/"$aFile"
fixThisFile tmp/"$aFile"
done

mv tmp/*.html "$cvsDIR"

# HTML TIDY
for n in "$cvsDIR"/*.html;do tidy -utf8 -language el -i -w 2000 -m "$n" 2>/dev/null;done

echo "  Active branch changed to \"$activeBranch\""
