#!/bin/bash
#   Common functions for l10n scripts (v 1.2)
#   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.
#
myVersion='1.7.0'
function parceConfig(){
if [ "$runCgeck" = "false" ];then return;fi
isInPATH "$installFolder" && {
	echo "Warning: The installation folder of the l10n scripts"
	echo "         parameter \"installFolder\"=\"$installFolder\""
	echo "         is NOT in your PATH!!!"
	echo
}

if [ "$usingStable" != "true" ] && [ "$usingTrunk" != "true" ];then
	echo "parceConfig> Error: parameters \"usingStable\" and \"usingTrunk\""
	echo "parceConfig> You have declared you are using neither \"stable\""
	echo "             nor \"trunk\" in your local copy of KDE SVN."
  echo "parceConfig> Please edit ~/.l10n.conf"
  	echo
	exit 1
fi
if [ "$usingStable" = "true" ];then
	checkFolder "$STABLE" STABLE && exit 1
	checkFolder "$PO_STABLE" PO_STABLE && exit 1
	checkFolder "$POT_STABLE" POT_STABLE && exit 1
fi
if [ "$usingTrunk" = "true" ];then
	checkFolder "$TRUNK" TRUNK && exit 1
	checkFolder "$PO_TRUNK" PO_TRUNK && exit 1
	checkFolder "$POT_TRUNK" POT_TRUNK && exit 1
fi
checkFolder "$tmpFolder" tmpFolder && exit 1
if [ "$usingCVS" = "true" ];then
	checkFolder "$cvsDIR"  cvsDIR && exit 1
fi
if [ "$usingAnonSVN" = "true" ]; then
	checkParam "$svnUserName" svnUserName && exit 1
	checkParam "$svnURL" svnURL && exit 1
	checkParam "$svnLang" svnLang && exit 1
elif [ "$usingAnonSVN" = "false" ]; then
	checkParam "$svnAnonURL" svnURL && exit 1
	checkParam "$svnLang" svnLang && exit 1
fi
return 0
}

function checkFolder(){
# $1 -> parameter
# $2 -> parameter name
if [ "$runCgeck" = "false" ];then return;fi
local ret=1
if [ -z "$1" ];then
	echo "parceConfig> Error: parameter \"$2\" not set..."
	echo "parceConfig> Please edit ~/.l10n.conf"
	echo
	ret=0
elif [ ! -d "$1" ];then
	echo "parceConfig> Error: parameter \"$2\""
	echo "parceConfig> Value: \"$1\""
  echo "parceConfig> Does not exist or is not a folder"	
	echo "parceConfig> Please edit ~/.l10n.conf"
	echo
	ret=0
elif [ ! -w "$1" ];then
	echo "parceConfig> Error: parameter \"$2\""
	echo "parceConfig> Value: \"$1\""
	echo "parceConfig> No write permission to this folder"
	echo "parceConfig> Please edit ~/.l10n.conf"
	echo
	ret=0
fi
return $ret
}

function checkParam(){
# $1 -> parameter
# $2 -> parameter name
# $3 -> optional message
if [ "$runCgeck" = "false" ];then return;fi
local ret=1
if [ -z "$1" ];then
	echo "parceFTPConfig> Error: parameter \"$2\" not set..."
	if [ ! -z "$3" ];then echo "$3";fi	
# echo "parceFTPConfig> FTP transfer disabled..."
	echo "parceFTPConfig> Please edit ~/.l10n.conf"
	echo
	noFTP=1
	ret=0
fi
return $ret
}

function parceFTPConfig(){
if [ "$runCgeck" = "false" ];then return;fi
local ret=1
local msg="parceFTPConfig> FTP transfer disabled..."
# Check if curl exists
local curlExec=`type -p curl`
if [ -z "$curlExec" ];then
	echo "parceFTPConfig> Warning: curl executable not found..."
	echo "$msg"
	echo "parceFTPConfig> Please install the curl package"
	echo
	noFTP=1
	return 0
fi

checkParam "$ftpServer" ftpServer "$msg" && noFTP=1 && return 0
checkParam "$ftpUserName" ftpUserName "$msg" && noFTP=1 && return 0
checkParam "$ftpPassword" ftpPassword "$msg" && noFTP=1 && return 0
return 1
}

function isInStable(){
local length=`expr length "$STABLE"`
local test=`expr substr "$1" 1 "$length"`
if [ "$STABLE" = "$test" ];then
	return 0
else
	return 1
fi
}

function showVersion(){
echo "`basename "$0"`  (v $myVersion)"
echo "(c) The Greek l10n Team (2005,2006) - http://el.l10n.kde.org/scripts.html"
echo "Written by $1"
if [ "$2" != "" ];then echo "$2";fi
echo
echo "Info: http://el.l10n.kde.org/scripts.html"
}

function isInPATH(){
	local check=`echo "$PATH" | grep "$1"`
	if [ "$check" = "" ];then
		return 0
	else
		return 1
	fi
}

function fixThisFile(){
# join all lines
cat "$1" | tr -d '\r'|sed '/<[tT][iI][tT][lL][eE]/,/<.[tT][iI][tT][lL][eE]?/{
:a
N
s/\n/ /
ta
}' | sed 's|</[tT][iI][tT][lL][eE]>|</title>\n|
/<[Bb][Oo][Dd][yY]/,/<.[Bb][Oo][Dd][yY]>/{
:a
N
s/\n/ /
ta
}' > "$1".tmp && mv "$1".tmp "$1"
# fromat the file
sed 's|[ \t][ \t]*| |g
s|  $||g
s|<[tT][aA][bB][lL][eE]|\n<table|g
s|</[tT][aA][bB][lL][eE]>|\n</table>\n|g
s|<[tT][dD]|\n<td|g
s|<[tT][rR]|\n<tr|g
s|</[tT][dD]>|\n</td>\n|g
s|</[tT][rR]>|\n</tr>\n|g
s|<[hH]|\n<h|g
s|<!--|\n<!--|g
s|-->|-->\n|g
s|<[dD][iI][vV]|\n<div|g
s|<[lL][iI]|\n<li|g
s|<[oO][lL]|\n<ol|g
s|<[uU][lL]|\n<ul|g
s|</[dD][iI][vV]>|\n</div>\n|g
s|</[lL][iI]>|</li>\n|g
s|</[oO][lL]>|\n</ol>\n|g
s|</[uU][lL]>|\n</ul>\n|g
s|<[pP]|\n<p|g
s|</[pP]>|</p>\n|g
s|</[Tt][bB][oO][dD][yY]>|\n</tbody>\n|g
s|<[Tt][bB][oO][dD][yY]|\n<tbody|g
s|<[iI][mM][gG] |\n<img |g
s|<[fF][oO][rR][mM]|\n<form|g
s|</[fF][oO][rR][mM]>|</form>\n|g
' "$1" | sed '/^[ \t]*$/d
s|^[ \t][ \t]*||g
s|[ \t][ \t]*$||' > "$1".tmp && mv "$1".tmp "$1"
}
