#!/usr/bin/perl

# wxdiff: highlight word differences in gettext po file translations
# use when the original and translation are close but not identical
# usage: wxdiff en_GB.po > tmp.po; less -R tmp.po

#   Copyright (C) 2005 by ott
#   Part of the Battle for Wesnoth Project http://wesnoth.org/
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License.
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY.
#
#   See the COPYING file for more details.

use Text::WordDiff;

sub procstrings {
    my $msgid = shift;
    my $msgstr = shift;
    $msgid =~ s/^[^\^]*\^//;
    @a = split /(?<!\p{IsWord})(?=\p{IsWord})/, $msgid;
    @b = split /(?<!\p{IsWord})(?=\p{IsWord})/, $msgstr;
    $diff = word_diff \@a, \@b;
    print "msgid \"$diff\"\nmsgstr \"\"\n";
    #print "msgid \"$msgid\"\nmsgstr \"$msgstr\"\n";
    #print "#diff \"$diff\"\n" if $diff;
}

my $state = 0;
my $msgstr = '';
LOOP:
while (<>) {
    if (/^#/ or /^\s*$/) {
	if ($state == 4) {
	    procstrings( $msgid, $msgstr );
	    $msgstr = '';
	    $msgid = '';
	    $state = 1;
	}
	print $_;
	next LOOP;
    }

    if ($state == 0) { # skip header
	if (/^msgstr /) { $state = 1 }
    } elsif ($state == 1) { # waiting for msgid
	if (/^msgid /) { $state = 2; redo LOOP } else { $msgid = '' }
    } elsif ($state == 2) { # snarf msgid
	if (/^msgstr /) { $state = 4; redo LOOP }
	/^[^\"]*\"([^\"]*)\"/ and $msgid .= $1;
    } elsif ($state == 4) { # snarf msgstr
	if (/^[^\"]*\"([^\"]*)\"/) {
	    $msgstr .= $1;
	} else {
	    die "unexpected characters, stopping at $_";
	}
    } else { die "internal error: unexpected state $state, stopping" }
}

procstrings( $msgid, $msgstr ) if ($state == 4);

