#!/usr/bin/perl

my %nrval, %tain, %have, $max, $tainted, $ignored, $preserved, $added, $ignerr, $uname, $name, $pref;

sub ReadTrans($)
{
    my $nr, $val, $found, $name;
    
    $found = 0;
    $name = shift;
    $name .= ".i18n";

    open (TMPL, "<:bytes", $name) || die "Could not open file $name.";
    while (<TMPL>)
    {
        if (m=^# \$Id: C.i18n ([0-9.]* 20..[-/]..[-/].. ..:..:..).*=)
        {
            $revision = $1;
        }
        elsif (m/^([0-9]{1,4})[.:](.*)/)
        {
            $nr = sprintf ("%04d", $1);
            $val = $2;
            $max = $nr;
            $nrval{$nr}=$val;
            $found++;
        }
        elsif (m/^#/)
        {
        }
        else
        {
            print "Can't understand: $_\n";
        }
    }
    print "Updating translation to $found strings.\n";
    die "Oooops" unless $found;
}

sub ReadTainted($)
{
    my $name = shift;
    
    %tain = {};
    $name = ".$name.tainted";
    open IN, "<$name" || return;
    while (<IN>)
    {
        if (m/^([0-9]{4})[.:](.*)$/)
        {
            $tain{$1} = $2;
        }
    }
}

sub ProcessLine($)
{
    my $line = shift;
    
    if ($line =~ m/^#  EN:/)
    {
        print OUT "#  EN: __.i18n $revision <-- NEVER TOUCH THIS - it's updated automatically\n";
    }
    elsif ($line =~ m/^#[0-9][0-9]*:/)
    {
    }
    elsif ($line =~ m/^#[ _]#([0-9][0-9]*):/)
    {
       next unless defined $nrval{$1} and length($nrval{$1});
       print OUT "#_#$1:$nrval{$1}\n" unless $have{$1} & 4;
       $added++ unless $have{$1} & 4;
       $have{$1} |= 4;
    }
    elsif ($line =~ m/^#[-!+]#([0-9][0-9]*):/)
    {
        my $i = $1;
        $neu = sprintf ("%04d", $i);
        for ($last++ ; $last < $neu - 1; $last++)
        {
            $last = sprintf ("%04d", $last);
            next unless defined $nrval{$last} and length($nrval{$last});
            print OUT "#_#$last:$nrval{$last}\n" unless $have{$last};
            $added++ unless $have{$last};
        }
        $last--;
       if (defined $nrval{$i})
       {
           my $l = $i - 1;
           $have{$i} |= 2;
           if ($have{$l} == 2)
           {
               print OUT "#_#$l:$nrval{$l}\n";
               $have{$l} = 6;
           }
           print OUT "$line\n";
           $ignored++;
       }
    }
    elsif ($line =~ m/^#/)
    {
       print OUT "$line\n";
       $ignored++;
    }
    elsif ($line =~ m/^([0-9][0-9]*)([:.])(.*)/)
    {
        $neu = sprintf ("%04d", $1);
        for ($last++ ; $last < $neu; $last++)
        {
            $last = sprintf ("%04d", $last);
            next unless defined $nrval{$last} and length($nrval{$last});
            print OUT "#_#$last:$nrval{$last}\n" unless $have{$last} & 4;
            $added++ unless $have{$last} & 4;
        }
        print "Error: $last $neu" if $last != $neu;
        $last = $mod = sprintf ("%04d", $last);
        $mod -= 2000 if $mod > 3000;
        if (length ($nrval{$mod}))
        {
            if (length ($tain{$mod}))
            {
                print OUT "#-#$line\n";
                print OUT "#+#$last:$tain{$mod}\n";
                print OUT "#_#$last:$nrval{$mod}\n";
                $tainted++;
            }
            else
            {
                if ($neu != 9999)
                {
                    print OUT "$1:$3\n";
                    $preserved++;
                }
            }
        }
        elsif ($neu != 9999)
        {
            if ($2 != ".")
            {
                print "Dropping $neu: $line.\n";
            }
            print OUT "$1.$3\n";
        }
        $last = $neu;
    }
    else
    {
        print OUT "#$line\n";
        $ignerr++;
    }
}

ReadTrans ("C");

print "File		OVers Comments Junk	Old	Changed	New\n";
while ($uname = shift)
{
    next unless $uname cmp "C.i18n";

    $rev = `grep -a "# *EN:" $uname | sed "s/.*EN://;s/.*,v //;s/.*\.i18n //;s/ .*//"`;
    chomp $rev;
    if (!length ($rev))
    {
        $rev = `grep ^1003 $uname`;
        $rev =~ s/^1003://;
        chomp $rev;
        $rev = 0 unless length ($rev);
        $rev = "i18n-$rev";
    }
#    print "Old revision tag: $rev.\n";
    
    stat ".C.$rev" || system "svn cat -r $rev http://svn.climm.org/climm/lang/C.i18n > .C.$rev";
    system "svn cat http://svn.climm.org/climm/lang/C.i18n > .C.head";
    system "diff -u .C.$rev .C.head > .C.diff";
    system "grep \"^-[0-9]\" < .C.diff | sed \"s,^-,,\" | sort -n -t : -k 1,1 -u -o .$uname.tainted";
    ReadTainted ("$uname");

    $name = $pref . $uname;
    stat "$name~" && next;
    rename ($name, "$name~");
    open (IN, "<:bytes", "$name~") || die;
    open (OUT, ">:bytes", $name) || die;
    
    $ignored = 0;
    $preserved = 0;
    $added = 0;
    $ignerr = 0;
    $tainted = 0;
    $last = -1;
    %have = ();
    while (<IN>)
    {
        chomp;
        ProcessLine ($_);
    }
    ProcessLine ("9999:");
    print "$name 	$rev  $ignored	 $ignerr	$preserved	$tainted	$added\n";
}
print "Finished.\n";
