#!/bin/bash
# function checkLineNum(){
# local i
# # set -x
# if [ "$1" = "" ];then
# 	line=$(( $line + 1 ))
# 	return 1
# fi
# local c=`echo "$1" | sed 's|[^0-9]*\([0-9]*\).*|\1|'`
# if [ "$1" != "$c" ];then
# 	line=$(( $line + 1 ))
# 	return 1
# fi
# set +x
# if [ "$count" = "-1" ];then
# 	return 0
# fi
# for i in `seq 0 "$count"`;do
# 	if [ "${noError["$i"]}" = "$1" ]; then
# 		return 1
# 	fi
# done
# return 0
# }
function help(){
	echo "A tool to check po files"
	echo
	echo "Usage: $(basename $0) [options] <po file>"
	echo
	echo "Valid options are:"
	echo "  -n  execute no sanity checks"
	echo "  -h  display this screen and exit"
	echo "  -s  check only KDE SVN stable branch"
	echo "  -t  check only KDE SVN trunk branch"
	echo "  -a  check accelerators"
	echo "  -f  check format"
	echo "  -H  check header"
	echo
}

function chpo(){
	if [ "$1" = "$PO_STABLE" ];then
		echo "Working in KDE SVN stable branch"
	elif [ "$1" = "$PO_TRUNK" ];then
		echo "Working in KDE SVN trunk branch"
	else
		echo "Working in \"$1\""
	fi
	cd "$1"
	for k in *;do
		if [ -d "$k" ];then
			cd "$k"
				for s in *.po
				do
					echo "  --> Checking $k/$s"
						if [ $optChk -eq 0 ];then
							msgfmt "$s" -o /dev/null 2>&1
						else
					msgfmt "$chkType" "$s" -o /dev/null 2>&1
						fi
				done
			cd ..
		fi
	done
}
#
# 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
#
chkType=''
doStable=1
doTrunk=1
doPO=0
optChk=0

while getopts ":vhstalfHn" 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) # Work in stable
		doStable=1
		doTrunk=0
		doPo=0;;
	t) # work in trunk
		doStable=0
		doTrunk=1
		doPo=0;;
	a) # check accelerators
		chkType="--check-accelerators=&"
		optChk=$(( $optChk + 1 ));;
	l)# show statistics
		chkType="--statistics"
		optChk=$(( $optChk + 1 ));;	
	f) # check format
		chkType="--check-format"
		optChk=$(( $optChk + 1 ));;
	H) # check format
		chkType="--check-header"
		optChk=$(( $optChk + 1 ));;
	*)
		doStable=0
		doTrunk=0
		doPo=1
	esac
done
shift $(($OPTIND - 1))










if [ $# -eq 1 ];then
	chk=$(echo "$1" | grep '.po$')
	if [ "$chk" = "" ];then
		echo "Error: File \"$1\" is not a po file"
		exit 1
	fi
	if [ -w "$1" ];then
		doStable=0
		doTrunk=0
		doPo=1
	else
		echo "Error: File \"$1\" does not exist or"
		echo "       or you don't have write permission to it"
		exit 1
	fi
fi

if [ $doPo -eq 1 ];then
	if [ $optChk -eq 0 ];then
	msgfmt "$1" -o /dev/null 2>&1
	else
	msgfmt "$chkType" "$1" -o /dev/null 2>&1
	fi
	exit
fi


if [ $doStable -eq 1 ] && [ "$usingStable" = "true" ];then
	chpo "$PO_STABLE"
fi
if [ $doTrunk -eq 1 ] && [ "$usingTrunk" = "true" ];then
	chpo "$PO_TRUNK"
fi
exit


# cd /home/spiros/kde-i18n/head/messages/
# for k in *;do
# 	if [ -d "$k" ];then
# 		clear
# 		cd "$k"
# 		echo "-->> $k"
# 		for n in *.po;do
# 			count='-1'
# 			line=1
# 			echo "$n"
# 			msgfmt --check-accelerators='&' "$n" -o/dev/null 2> "$n".chk
# 			file=`cat "$n".chk | sed '/fatal/d'`
# 			while [ "$file" != "" ];do
# 				fname=`echo "$file" | sed -n "${line}p"| sed 's|\([^:]*\):[0-9]*:.*|\1|'`
# 				fnum=`echo "$file" | sed -n "${line}p"| sed 's|.*:\([0-9]*\):.*|\1|'`
# 				echo "  Error found in file \"$n\", line \"$fnum\""
# 				checkLineNum "$fnum" 
# 				if [ $? -eq 0 ];then
# 					cp "$n" "$n".bck
# 					kwrite --line "$fnum" "$n"
# 					
# 					DIFF=`diff "$n" "$n".bck`
# 					if [ "$DIFF" = "" ];then
# 						ans='n'
# 					else
# 						ans=''
# 					fi
# 					echo -n "  Was it a real error? ([y]/n) "
# 					read ans
# 					if [ "$ans" = "n" ];then
# 						count=$(( $count + 1 ))
# 						line=$(( $line + 1 ))
# 						noError["$count"]="$fnum"
# 					fi
# 				else
# 					echo "  Not an error!!!"
# 
# 				fi
# 				msgfmt --check-accelerators='&' "$n" -o/dev/null 2> "$n".chk
# 				file=`cat "$n".chk | sed "1,${line}d"`
# 				set +x
# 			done
# 			rm "$n".chk 2>/dev/null
# 			rm "$n".bck 2>/dev/null
# 		done
# 		cd ..
# 		read
# 	fi
# done