#! /bin/sh
# A script to merge two translations, as an aid to learning a language.

# Arg 1 is foreign, other args are english.
eliminate()
{
    FOREIGN=$1
    shift
    for arg; do
	# Mentioned in foreign?  Assume proper noun.
	if echo "$FOREIGN" | grep -qwe "$arg"; then :
	else
	    case "`echo $arg | tr 'A-Z' 'a-z'`" in
		the|an|a|one|of|be|it|i|he|she|we|they|is) :;;
		*) echo $arg;;
	    esac
	fi
    done
}

# Prints every second english word in ().
interlace()
{
    echo \"$2\"
    echo '"('`eliminate "$2" $1`')"'
}
	    

IN=unknown
while read -r LINE; do
    case "$LINE" in
	msgid\ \"\")
        MSGID=""
	IN=msgid
	echo "$LINE"
	;;
	msgstr\ \"\")
        MSGSTR=""
	if [ -n "$MSGID" ]; then
	    IN=msgstr
	else
	    echo "$LINE"
	    IN=unknown
	fi
	;;
	\"*)
        case $IN in
	    msgid) 
		MSGID=`echo $MSGID$(echo $LINE | sed 's/^"\(.*\)"/\1/')`" "
		echo "$LINE"
		;;
	    msgstr)
		MSGSTR=`echo $MSGSTR$(echo $LINE | sed 's/^"\(.*\)"/\1/')`" "
		;;
	    unknown)
		echo "$LINE"
		;;
	esac
	;;
	"")
	    if [ $IN = msgstr ]; then
		echo 'msgstr ""'
		if [ -n "$MSGSTR" ] && [ -n "$MSGID" ]; then
		    interlace "$MSGID" "$MSGSTR"
		fi
	    fi
	    IN=unknown
	    MSGID=""
	    MSGSTR=""
	    echo
	    ;;
	*)
	    echo "$LINE"
	    IN=unknown
	    ;;
    esac
done
	    

