#!/bin/bash
#   Updates 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 tidy-up your html pages"
	echo
	echo -n "Usage:	$(basename "$0") <options>"
	echo "Options are:"
	echo "  -h  display this screen and exit"
  echo "  -v  display version info and exit"
	echo
}

function	findBranch(){
if `isInStable "$PWD"`;then
echo -n "stable "
else
	echo -n "trunk "
fi
}

function	findFolder(){
local check=`svn info | grep URL |grep templates`
if test -z "$check" ;then
	echo messages
else
	echo templates
fi
}

#
# 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
#
while getopts ":vhn" Option
do
	case $Option in
	h) # show help
		help
		exit 0;;
	v) # show version
		showVersion 'S. Georgaras <sngeorgaras@otenet.gr>'
		echo
		exit 0
	esac
done
shift $(($OPTIND - 1))


echo -n "Runing tidy on your HTML pages...   "
# HTML TIDY
for n in "$cvsDIR"/*.html;do
  cat "$n" | tr -d '\r' > "$n".tmp && mv "$n".tmp "$n"
  tidy -utf8 -language el -i -w 2000 -m "$n" 2>/dev/null
  for control in '<!-- Σημείο τέλους εγγραφής στατιστικών -->' '<!-- Σημείο αρχής εγγραφής στατιστικών -->' '<!--Πίνακας τελευταίας ενημέρωσης-->' '<!--Νέα αλλαγή-->' '<!-- Σημείο εισαγωγής λίστας -->' '<!-- Τέλος εισαγωγής λίστας -->';do
      kena="$(grep "$control" "$n" |sed 's|\(  *\)[^ ].*|\1|')"
      sed "s|$control|\n$kena$control|g" "$n" 2>/dev/null > "$n".tmp && mv "$n".tmp "$n" || rm "$n".tmp
  done
done
echo 'done'
