#!/usr/bin/env perl

use strict;

# there is prolly a much much better way to do this, but the docs
# were not findable, and or that helpful & parsing .po is easy
#
# This tool expects a tree of .po files, 1 per ISO lang:
# {prefix}/
#          de/
#          fr/
#          ...
#
# inside each dir this filename is used:
my $pofilename = 'totranslate.po';
my $path;
my $xml = 0;
for my $arg (@ARGV) {
    if ($arg eq '--xml') {
	$xml = 1;
    } elsif ($arg eq '--help' || $arg eq '-h') {
	print "potores [--xml] /path/to/pofiles\n";
	exit 0;
    } else {
	$path = $arg;
    }
}

sub insert($$$$)
{
    my ($lang_hash, $lang, $msgid, $msgstr) = @_;

    if (!defined $lang_hash->{$msgid}) {
	my %tmphash;
	$lang_hash->{$msgid} = \%tmphash;
    }
    my $thismsg = $lang_hash->{$msgid};
    $thismsg->{$lang} = $msgstr;
#    print "Insert '$msgid' [$lang] '" . $thismsg->{$lang} . "'\n";
}

sub slurp_pofile($$$)
{
    my ($lang_hash, $lang, $po_file) = @_;
    my $fileh;
    my $msgid = '<error>';

    open ($fileh, "$po_file") || die "Can't open $po_file: $!";
    while (<$fileh>) {
	my $line = $_;
	$line =~ /^\#/ && next;
	$line =~ /^\s*$/ && next;
	if ($line =~ m/^"(.*)"\s*$/) {
	    my $attr = $1;
	    if ($attr =~ m/Content-Type: .*charset=(.*)/) {
		my $charset = $1;
		$charset =~ m/utf-8/i || die "Invalid charset $charset";
	    }
	    next; # skip headers
	}
	if ($line =~ /^msg(id|str)\s*"([^\"]*)"/) {
	    $msgid = $2 if ($1 eq 'id');
	    insert ($lang_hash, $lang, $msgid, $2) if ($1 eq 'str');
	} else {
	    print "Unpleasant .po file line '$line'\n";
	}
    }
    close ($fileh);
}

print STDERR "Reading translations at $path\n";

my $dirh;
my %langs;
opendir ($dirh, $path) || die "Can't open $path: $!";
while (my $lang = readdir ($dirh)) {
    $lang =~ /^\./ && next;
    printf STDERR "$lang ";
    slurp_pofile (\%langs, $lang, "$path/$lang/$pofilename");
}
printf STDERR "\n";
closedir ($dirh);

sub printstr($$)
{
    my ($lang, $str) = @_;
    $lang =~ s/_/-/g;
    if ($xml) {
# FIXME: escaping ?
	print "+\t\t<value xml:lang=\"$lang\">$str</value>\n";
    } else {
	print "+\t\tText[ $lang ] = \"$str\";\n";
    }
}

for my $msgid ( sort keys %langs ) {
    print "SID_\n";
#    print "msgid $msgid\n";
    my $thismsg = $langs{$msgid};
    printstr("en-US", $msgid);
    for my $lang ( sort keys %{$thismsg} ) {
	printstr($lang, $thismsg->{$lang});
    }
}
