#!/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 deleted
from KDE SVN (remotely), but still exist in the local copy"
	echo
	echo "Deleted po files will be removed from the local copy
of the repository and the remote repository itself
(if user is aythorized to commit), and will be copied
in \"$delDir\" for reference
"
	echo -n "Usage: "
	echo -n $(basename "$0")
	echo " [options]"
	echo
	echo "Valid options are:"
	echo "  -r  report only (take no action)"
	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
#
delDir="$tmpFolder"/deleted_from_svn
mkdir "$delDir" 2>/dev/null
mkdir "$delDir"/stable 2>/dev/null
mkdir "$delDir"/head 2>/dev/null

reportOnly=0
while getopts ":vhnr" Option
do
	case $Option in
		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 "$PO_STABLE" "$PO_TRUNK"
do
	cd "$n"
	echo "Checking \"$n\""
	for k in `find . -name "*.po"`
	do
		chPO=`echo "$k" | sed 's|^\./||'`
		tmp=`dirname "$chPO"`
		if [ "$tmp" != "" ] && [ "$tmp" != "." ];then
			tmp=`echo "$n" | grep stable`
			if [ "$tmp" = "" ];then
				chDir="$POT_TRUNK"
			else
				chDir="$POT_STABLE"
			fi
			chPOT="$chDir"/"$chPO"t
			if [ ! -e "$chPOT" ];then
				echo "  File \"$chPO\" has been deleted from KDE SVN..."
				if [ $reportOnly -eq 0 ];then
					if [ "$tmp" = "" ];then
						dPO="$delDir"/head/"$chPO"
					else
						dPO="$delDir"/stable/"$chPO"
					fi
						dDir=`dirname "$dPO"`
						mkdir -p "$dDir" && echo "File deleted on $(date): $PWD/$chPO
It was backed up as: $dPO" >> "$tmpFolder"/auto-actions.log
						if [ ! -d "$dDir" ];then
							echo "    Error creating folder: \"$dDir\""
							echo "    File \"$chPO\" will not be backed up"
						else
							mv "$n"/"$chPO" "$dDir"
							canRemove=`svn info |grep URL | sed 's|^URL: ||' | grep "$svnURL"`
							if [ "$canRemove" != "" ];then
								echo -n "    "
								svn remove "$chPO" 
							fi
						fi
				fi
			fi
		fi
	done
done