#!/bin/bash
#   Commits changes to both l10n branches
#   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 "A tool to commit changes to both trunk and stable"
	echo "branch of the local copy of the KDE SVN. It will"
	echo "also add new files to the branches"
	echo
	echo "Usage: $(basename $0) <log message>    or"
	echo "       $(basename $0) [options]"
	echo
	echo "Valid options are:"
	echo "  -n  execute no sanity checks"
	echo "  -h  display this screen and exit"
	echo
	echo "The log message may contain the following strings,"
	echo "that will be replaced with the corresponding values:"
	echo "  <branch>  The branch name (trunk or stable)"
	echo "  <date>    The current date (format: dd/mm/yyyy)"
	echo "  <time>    The current time (format: hh:mm:ss)"
	echo
	echo "E.g the log message"
	echo "\"Updating <branch> on <date>, <time>\""
	echo "will be replaced with"
	echo "\"Updating trunk on 29/11/2005, 12:23:24\""
	echo
}

function commitFiles(){
local msg
local canCommit='false'
local svnSt=`svn st`
local toCommit=`echo "$svnSt" | grep -e '^[MAD]' | sed 's|.[ 	]*||'`
if [ "$toCommit" != "" ];then canCommit='true';fi
local toAdd=`echo "$svnSt" | grep -e '^?' | sed 's|.[ 	]*||' | grep -e '\.po$'`
if [ "$toAdd" != "" ];then
	echo >> "$logFile"
	for n in `echo "$toAdd"`
	do
		if [ "$1" = "trunk" ];then
			potFolder="$POT_TRUNK"	
		else
			potFolder="$POT_STABLE"
		fi
		# check if pot file exists
		if [ -e "$potFolder/$n"t ];then
	 		svn add "$n" 1>/dev/null
			canCommit='true'
		else
			echo "A new po file named \"$n\""
			echo "was found in branch $1, but the"
			echo "corresponding pot file does not exit."
			echo "The file was NOT added in KDE SVN..."
			echo "A new po file named \"$n\""  >> "$logFile"
			echo "was found in branch $1, but the"  >> "$logFile"
			echo "corresponding pot file does not exit."  >> "$logFile"
			echo "The file was NOT added in KDE SVN..."  >> "$logFile"
		fi
		echo >> "$logFile"
	done
fi


if [ "$canCommit" = "true" ];then
	msg=`echo "$svnMessage" | sed "s|<branch>|$1|" | sed "s|<date>|$mDate|" | sed 's|<time>|$mTime|'`
	echo "Commiting changes for branch $1..."
	echo "Commiting changes for branch $1..." >> "$logFile"
	echo "# svn ci -m \"$msg\""
	echo "# svn ci -m \"$msg\"" 1>>"$logFile"
	svn ci -m "$msg" 1>"$logFile".tmp
	cat "$logFile".tmp
	if [ -z "$(grep 'Committed revision' "$logFile".tmp)" ];then
		retVal=1
		rm "$logFile".tmp
		echo "" >>"$logFile"
		echo "Error Commiting file(s)..."
		echo "Error Commiting file(s)..." 1>>"$logFile"
		return
	fi
	cat "$logFile".tmp>>"$logFile"
	rm "$logFile".tmp
	echo
else
	echo "No files to commit for branch $1..."
	echo
	echo "No files to commit for branch $1..."  >> "$logFile"
	echo  >> "$logFile"
	retVal=0
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
#
msgCommandLine="# $(basename $0)"
while getopts ":vhsn" Option
do
	case $Option in
	h) # show help
		help
		exit 0;;
	v) # show version
		showVersion 'S. Georgaras <sngeorgaras@otenet.gr>'
		echo
		exit 0;;
	n) # run no checks
		runCheck=false;;
	s) # Display log file
		displayLog='true'
		msgCommandLine="$msgCommandLine -s"
	esac
done
shift $(($OPTIND - 1))
# if [ $# -eq 0 ];then
# 	help
# 	exit 1
# fi

mDate=`date '+%d/%m/%Y'`
mTime=`date '+%H:%M:%S'`
logFile="$tmpFolder/l10n-ci-$(date '+%Y%m%d-%H%M%S').log"
svnMessage="$1"
if [ -z "$svnMessage" ];then
	svnMessage='updating "<branch>" on <date>'
	echo "$msgCommandLine \"$svnMessage\"" > "$logFile"
else
	echo "$msgCommandLine" > "$logFile"
fi
# commit changes
filesHaveBeenCommited=1
echo "$msgCommandLine \"$svnMessage\"" > "$logFile"
if [ "$usingTrunk" = "true" ];then
	cd "$PO_TRUNK"
	commitFiles trunk
	filesHaveBeenCommited=$?
	if [ $filesHaveBeenCommited -eq 1 ];then
		exit
	fi
echo >> "$logFile"
fi
if [ "$usingStable" = "true" ];then
	cd "$PO_STABLE"
	commitFiles stable
fi

if [ $filesHaveBeenCommited -eq 0 ];then

if [ "$displayLog" = "true" ];then
	clear
	cat "$logFile"
else
	echo;echo "Commit log file: \"$logFile\""
fi
day=`date '+%H' | sed 's/^0//'`
if [ "$day" = "" ];then day=0;fi
case $day in
4 | 5 | 6 | 7 | 8 | 9 |10 | 11 ) DAY="Καλημέρα";;
12 | 13 | 14 | 15 | 16 ) DAY="Καλό μεσημέρι";;
17 | 18 | 19 | 20  ) DAY="Καλό απόγευμα";;
* ) DAY="Καλό βράδυ"
esac
echo "$DAY σε όλους

Μόλις έκανα το ακόλουθο commit
Παρακαλώ κάντε svn up
" >"$logFile".tmp
cat "$logFile" >> "$logFile".tmp
kmail --msg "$logFile".tmp -s "Ενημέρωση stable και trunk" 'kde-i18n-el@kde.org ("kde-i18n-el")'


fi
rm "$logFile".tmp 2>/dev/null