#!/bin/bash
#   Replaces strings in po files
#   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 replace strings in po files"
echo -n "Usage:	"
echo -n $(basename "$0")
echo " <options> [search string] [replacement string]"
echo "Options are:"
echo "  -s   replace in subfolders"
echo "  -n   execute no sanity checks"
echo "  -h   display this screen and exit"
echo
echo "  Both [search string] and [replacement string]"
echo "  are case sensetive and UTF-8 encoded."
}

function replaceInPO(){
# Create the sed file
		ls -1 *.po 1>&2>/dev/null
		if [ $? -ne 0 ];then
			echo "Error: This folder contains no PO files"
			echo
			exit 1
		fi
		rep=`echo "$2" | sed 's|&|\\&|'`
		echo "s|$1|$rep|g">l10n-replace.sed
		
		for n in *.po
		do
			po=$(echo "$n"|sed 's/.po$/-new.po/')
# 			sed -f l10n-replace.sed "$n" > "$po" # 1.4
			sed "s|$1|$rep|g" "$n" > "$po" # 1.4.1
			DIFF=$(diff "$n" "$po")
			if [ "$DIFF" = "" ];then
				rm "$po"
			else
				if [ -s "$po" ];then
					echo "  New file: "$k"/"$n""
					mv "$po" "$n"
				else
					echo "  Zero sized file: "$k"/"$po""
				fi
			fi
		done
		
		# Cleaning up
# 		rm l10n-replace.sed # 1.4
}
#### 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
#
. "$installFolder"/l10n-common
while getopts ":svn" Option
do
	case $Option in
			s )
			DIR_ONLY=no;;
			n) # run no checks
				runCheck=false;;
			v) # show version
			showVersion 'S. Georgaras <sngeorgaras@otenet.gr>'
			echo
			exit 0
	esac
done
shift $(($OPTIND - 1))

if [ $# -ne 2 ];then
	help
	exit 0
fi
if [ "$1" = "" ];then
	help
	echo
	echo "Error:	The [search string] is empty..."
	exit 1
fi
if [ "$2" = "" ];then
	help
	echo
	echo "Error:	The [replacement string] is empty..."
	exit 1
fi
# set -x
if [ "$DIR_ONLY" != "no" ];then
	replaceInPO "$1" "$2"
	exit 0
fi
for k in *
do
	if [ -d "$k" ];then
		cd "$k"
		echo "Working in $k"
		replaceInPO "$1" "$2"
		cd ..
#		fi
	fi

done