#!/bin/bash
# Copyright (C) 2000-2012 Marcin Owsiany <porridge@debian.org>

#
# Set up temporary directory
#
POTMP=`mktemp -d -t poedit.XXXXXX` || exit 1

function usage()
{
	echo "Usage: poedit [ -a ] [ -p ] [ -n ] <file.po>" >&2
	exit 1
}

#
# Check what msgs to include
#
INCLUDE_ALL_MSGS=no
IGNORE_ENCODING=no
PRESERVE_WRAPPING=no

TEMP=`getopt -o anp -n 'poedit' -- "$@"`
if [ $? != 0 ] ; then usage ; fi
# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"

while true ; do
	case "$1" in
	-a)
		INCLUDE_ALL_MSGS=yes
		shift
		;;
	-n)
		IGNORE_ENCODING=yes
		shift
		;;
	-p)
		PRESERVE_WRAPPING=yes
		shift
		;;
	--)
		shift
		break
		;;
	*)
		echo "Internal error!" >&2
		exit 1
		;;
	esac
done

POFILE="$1"
TMPFILE="$POTMP/`basename -- "$POFILE"`"
TMPFILE2="$TMPFILE.tmp"

#
# Some sanity checks
#
[ -z "$POFILE" ] && usage
if ! [ -w "$POFILE" ]; then
	echo "$POFILE: not writable" >&2
	exit 1
fi
if [ -n "$EDITOR" ]; then
	EDITCMD="$EDITOR"
elif which sensible-editor >/dev/null 2>&1; then
	EDITCMD="sensible-editor"
else
	echo '$EDITOR is not set, and sensible-editor is not available' >&2
	exit 1
fi

if [ $PRESERVE_WRAPPING = yes ]; then
	potool="potool -p"
else
	potool="potool"
fi

#
# Get file encoding
#
if [ $IGNORE_ENCODING = no ] ; then
	input_encoding=$($potool -ft -fnth "$POFILE" | egrep -m 1 -i '^"Content-Type:' | perl -p -e 's,^.*charset=(.*?)( *;.*)?\\n"$,$1,')
	if [ -z "$input_encoding" ] ; then
		echo "Failed to retrieve encoding from the content-type header." 1>&2
		echo "Make sure the file has a proper header before running $0" 1>&2
		exit 1
	fi
	locale_encoding=$(locale charmap)
	if [ -z "$locale_encoding" ] ; then
		echo "Failed to retrieve the locale charmap. Something is very wrong." 1>&2
		exit 1
	fi
fi

#
# Filter the file
#

if [ "$INCLUDE_ALL_MSGS" = "yes" ]
then
	$potool "$POFILE" -ft -fno > "$TMPFILE" || { echo "Running '$potool \"$POFILE\" -ft -fno > \"$TMPFILE\"' failed with code $?" >&2; exit 1; }
	echo >> "$TMPFILE"
	$potool "$POFILE" -fnt >> "$TMPFILE" || { echo "Running '$potool \"$POFILE\" -fnt >> \"$TMPFILE\"' failed with code $?" >&2; exit 1; }
	echo >> "$TMPFILE"
	$potool "$POFILE" -ft -fo >> "$TMPFILE" || { echo "Running '$potool \"$POFILE\" -ft -fo >> \"$TMPFILE\"' failed with code $?" >&2; exit 1; }
else
	$potool "$POFILE" -fnth > "$TMPFILE" || { echo "Running '$potool \"$POFILE\" -fnth > \"$TMPFILE\"' failed with code $?" >&2; exit 1; }
fi

if [ $IGNORE_ENCODING = no ] ; then
	#
	# Recode the file so it is in the locale's encoding
	#
	iconv -f "$input_encoding" -t "$locale_encoding" < "$TMPFILE" > "$TMPFILE2"
	if [ $? -ne 0 ]; then
		echo "Recoding from [$input_encoding] to [$locale_encoding] failed." 1>&2
		echo "Temp. file: $TMPFILE" 1>&2
		exit 1
	fi
	
	#
	# Fix the charset information
	#
	change-po-charset "$locale_encoding" "$TMPFILE2" > "$TMPFILE"
	if [ $? -ne 0 ]; then
		echo "Failed to substitute the encoding attribute in the header." 1>&2
		echo "Make sure the file has a proper header before running $0" 1>&2
		echo "Temp. file: $TMPFILE2" 1>&2
		exit 1
	fi
fi

#
# Run editor and update the file on success
#
$EDITCMD "$TMPFILE"
editor_ret="$?"

if [ $editor_ret -ne 0 ]; then
	echo "$EDITCMD exited abnormally (code $editor_ret), not updating the po file" 1>&2
	exit 1
fi

if [ $IGNORE_ENCODING = no ] ; then
	#
	# Change the charset information back
	#
	change-po-charset "$input_encoding" "$TMPFILE" > "$TMPFILE2"
	if [ $? -ne 0 ]; then
		echo "Failed to substitute the encoding attribute in the header." 1>&2
		echo "Temp. file: " $TMPFILE 1>&2
		exit 1
	fi
	
	#
	# Recode the file back to the original encoding
	#
	iconv -f "$locale_encoding" -t "$input_encoding" < "$TMPFILE2" > "$TMPFILE"
	if [ $? -ne 0 ]; then
		echo "Recoding back from [$locale_encoding] to [$input_encoding] failed." 1>&2
		echo "Temp. file: " $TMPFILE2 1>&2
		exit 1
	fi
fi

mv "$POFILE" "$POFILE~" || { echo "Failed to rename \"$POFILE\" to \"$POFILE~\""; exit 1; }
$potool "$POFILE~" "$TMPFILE" > "$POFILE"
if [ $? -eq 0 ]; then
	printf "Before: %s/%s\n" `$potool -ft -s "$POFILE~"` `$potool -s "$POFILE~"`
	printf "After:  %s/%s\n" `$potool -ft -s "$POFILE"` `$potool -s "$POFILE"`
	rm -f "$POFILE~" "$TMPFILE"
else
	mv -f "$POFILE~" "$POFILE"
	echo "Merging $POFILE with $TMPFILE failed" >&2
	exit 1
fi

rm -Rf "$POTMP"
