#!/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 "Usage: "
	echo -n $(basename "$0")
	echo " [options] <module> <po file>   or"
	echo -n "Usage: "
	echo -n $(basename "$0")
	echo " [options] <module>/<po file>"
	echo
	echo "Valid options are:"
	echo "  -s  merge from stable branch (stable -> trunk)"
	echo "      normally $(basename "$0") merges from trunk branch"
  echo "      (trunk -> stable)"
	echo "  -n  execute no sanity checks"
	echo "  -h  display this screen and exit"
	echo
	echo "The new file will be saved as \"<po filename>-new.po\"."
	echo "Then the user should check weather the file is accepted"
	echo "(and rename it to \"<po filename>.po\") or not."
}

#### 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
#
workFolder="$PO_STABLE"
poFolder="$PO_TRUNK"
potFolder="$POT_STABLE"

while getopts ":svhn" Option
do
	case $Option in
		s )
			workFolder="$PO_TRUNK"
			poFolder="$PO_STABLE"
			potFolder="$POT_TRUNK";;
		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))
# set -x
if [ $# -eq 0 ];then
	help
	exit 1
elif [ $# -eq 1 ];then
	MODULE=`echo "$1" | sed 's|\([^/]*\)/.*|\1|'`
	PO=`echo "$1" | sed 's|[^/]*/||'`
	if [ "$MODULE" = "$PO" ];then
		MODULE=$(basename "$PWD")
		PO="$1"
	fi
elif [ $# -eq 2 ];then
  MODULE="$(echo "$1" | sed 's|/||g')"
  PO="$2"
fi
# exit



TMP=$(echo "$PO" | grep -e 'po$')
if [ "$TMP" = "" ];then
	echo "File \"$PO\" is not a valid po file"
	echo
	exit 1
fi
POT="$PO"t
if [ ! -d "$PO_TRUNK"/"$MODULE" ];then
	echo "Module \"$MODULE\" does not exist in trunk branch"
	echo
	exit 1
elif [ ! -d "$PO_STABLE"/"$MODULE" ];then
	echo "Module \"$MODULE\" does not exist in stable branch"
	echo
	exit 1
elif [ ! -w "$PO_TRUNK"/"$MODULE" ];then
	echo "No write permission for module \"$MODULE\" in trunk branch"
	echo
	exit 1
elif [ ! -w "$PO_STABLE"/"$MODULE" ];then
	echo "No write permission for module \"$MODULE\" in stable branch"
	echo
	exit 1
fi

if [ ! -e "$poFolder"/"$MODULE"/"$PO" ];then
	echo "File \"$PO\" does not exist"
	echo
	exit 1
elif [ ! -r "$poFolder"/"$MODULE"/"$PO" ];then
	echo "No read permission for file \"$PO\""
	echo
	exit 1
fi

noPO=''
if [ ! -e "$workFolder"/"$MODULE"/"$PO" ];then
	noPO=true
	if [ ! -e "$potFolder"/"$MODULE"/"$POT" ];then
		echo "File \"$PO\" does not exist in brunch"
		exit 1
	fi
elif [ ! -w "$poFolder"/"$MODULE"/"$PO" ];then
	echo "No write permission for file \"$PO\""
	echo
	exit 1
fi

tmpPO=$(echo "$PO" | sed 's/.po$/-tmp.po/')
newPO=$(echo "$PO" | sed 's/.po$/-new.po/')

cd "$workFolder"/"$MODULE"
if [ "$noPo" = "true" ];then
	cp "$potFolder"/"$MODULE"/"$POT" "$PO"
fi
echo -n "Merging $PO with POT "
msgmerge "$PO" -C "$poFolder"/"$MODULE"/"$PO" "$potFolder"/"$MODULE"/"$POT" -o "$newPO"
echo

DIFF=`diff "$PO" "$newPO"`
if [ "$DIFF" = "" ];then
	rm "$newPO"
	echo "No file has been saved in \"$PWD\""
else
# # 	echo "New file: \"$PWD/$newPO\""
#   statPO="$(msgfmt --statistics -o /dev/null "$PO" 2>&1)"
#   translated="$(echo "$statPO" | sed '/translated/s|\([0-9][0-9]*\) translated.*|\1|')"
#   if [ -n "$(echo "$translated" | grep -e '\.$')" ];then translated=0;fi
#   fuzzy="$(echo "$statPO" | sed '/fuzzy/s|.*, \([0-9][0-9]*\) fuzzy.*|\1|')"
#   if [ -n "$(echo "$fuzzy" | grep -e '\.$')" ];then fuzzy=0;fi
#   untranslated="$(echo "$statPO" | sed '/untranslated/s|.*, \([0-9][0-9]*\) untranslated.*|\1|')"
#   if [ -n "$(echo "$untranslated" | grep -e '\.$')" ];then untranslated=0;fi
#   allMsg=$(echo "$translated + $fuzzy + $untranslated" | bc)
#   printf "%4s translated, %4s fuzzy, %4s untranslated, %4s total - %s\n" $translated $fuzzy $untranslated $allMsg $PO
#   statNewPO="$(msgfmt --statistics -o /dev/null "$newPO" 2>&1)"
#   translatedNew="$(echo "$statNewPO" | sed '/translated/s|\([0-9][0-9]*\) translated.*|\1|')"
#   if [ -n "$(echo "$translatedNew" | grep -e '\.$')" ];then translatedNew=0;fi
#   fuzzyNew="$(echo "$statNewPO" | sed '/fuzzy/s|.*, \([0-9][0-9]*\) fuzzy.*|\1|')"
#   if [ -n "$(echo "$fuzzyNew" | grep -e '\.$')" ];then fuzzyNew=0;fi
#   untranslatedNew="$(echo "$statNewPO" | sed '/untranslated/s|.*, \([0-9][0-9]*\) untranslated.*|\1|')"
#   if [ -n "$(echo "$untranslatedNew" | grep -e '\.$')" ];then untranslatedNew=0;fi
#   allMsgNew=$(echo "$translatedNew + $fuzzyNew + $untranslatedNew" | bc)
#   printf "%4s translated, %4s fuzzy, %4s untranslated, %4s total - %s\n" $translatedNew $fuzzyNew $untranslatedNew $allMsgNew $newPO
  mv "$newPO" "$PO"
fi

