#!/bin/bash
#   Searches for 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 search for strings in po files"
	echo "Usage: "`basename "$0"`" <options> [search string]"
	echo "Options are:"
	echo "  -d[folder]  cd to folder"
	echo "  -s          scan subfolders"
	echo "  -e          the [search string] is a RegExp"
	echo "  -l          show file lines"
	echo "  -n          execute no sanity checks"
	echo "  -h          display this screen and exit"
}

function doIt(){
	if [ "$3" != "" ];then
		cd "$dir"
	fi
	for n in $(find . -maxdepth 1 -name "*.po");do
		if [ $2 -eq 0 ];then
			if [ $4 -eq 0 ];then
				grep -H "$1" "$n" | grep -v ':#:' | grep -v ':#~'
			else
				grep -Hn "$1" "$n" | grep -v ':#:' | grep -v ':#~'
			fi
		else
			if [ $4 -eq 0 ];then
				grep -H -e "$1" "$n" | grep -v ':#:' | grep -v ':#~'
			else
				grep -Hn -e "$1" "$n" | grep -v ':#:' | grep -v ':#~'
			fi
		fi
	done
}
#### 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
#
. "$installFolder"/l10n-common
regExp=0
folderOnly=1
showLines=0
while getopts ":esvhnld:" Option
do
	case $Option in
			e ) # use regexp
				regExp=1;;
			s) # folder only
				folderOnly=0;;
			l) # with line numbers
				showLines=1;;
			n) # run no checks
				runCheck=false;;
			d) # cd to folder
				if [ ! -d "$OPTARG" ];then
					echo "Error: Parameter -d \"$OPTARG\": No such folder"
					echo
					exit 1
				fi
				if [ ! -r "$OPTARG" ];then
					echo "Error: Parameter -d \"$OPTARG\": No read permission"
					echo
					exit 1
				fi
				dir="$OPTARG"
				folderOnly=1
				;;
			h) # print help
				help
				exit 0;;
			v) # show version
			showVersion 'S. Georgaras <sngeorgaras@otenet.gr>'
			echo
			exit 0
	esac
done
shift $(($OPTIND - 1))


if [ $# -eq 1 ];then
	if [ $folderOnly -eq 1 ];then
		doIt "$1" $regExp "$dir" "$showLines"
	else
		for k in *
		do
			if [ -d "$k" ];then
				cd "$k"
				echo "--> Current working dir: $k"
				doIt "$1" $regExp "$dir" "$showLines"
				cd ..
			fi
		done
	fi
else
	help
	exit 1
fi
