#! /usr/bin/perl

# http://l10n.openoffice.org/extract_merge.html

our @projects = qw(
		   accessibility 
		   avmedia 
		   basctl 
		   basic 
		   chart2 
		   connectivity 
		   crashrep 
		   dbaccess 
		   desktop 
		   extensions 
		   filter 
		   forms 
		   fpicker 
		   framework 
		   goodies 
		   instsetoo_native 
		   helpcontent2
		   javainstaller2 
		   officecfg 
		   padmin 
		   readlicense_oo 
		   reportdesign 
		   sc 
		   scaddins 
		   sccomp 
		   scp2 
		   sd 
		   sdext 
		   sfx2 
		   shell 
		   starmath 
		   svtools 
		   svx 
		   sw 
		   swext 
		   sysui 
		   toolkit 
		   ucbhelper 
		   uui 
		   vcl 
		   wizards 
		   xmlsecurity 
		   );

sub reading {
    my $hashref = shift;
    my $langref = shift;
    my ($key, $value);
    while (<ARGV>) {
	next if m/\A\#/;
	next if m/\A\s*\Z/;
	#Project SourceFile Dummy ResourceType GroupId LocalId HelpId Platform Width LanguageId ...
	($Project,
	 $SourceFile,
	 $Dummy,
	 $ResourceType,
	 $GroupId,
	 $LocalId,
	 $HelpId,
	 $Platform,
	 $Width, 
	 $LanguageId)
	    = split(m/\t/, $_);
	unless ($LanguageId) {
	    printf STDERR "Warning: No LanguageId is specified: %s %n: %s", $ARGV, $., $_;
	    next;
	}
	if ($LanguageId eq "en-US") {
	    printf STDERR "Warning: en-US is included: %s %n: %s", $ARGV, $., $_;
	    next;
	}
	$key = join("\t", $Project, $SourceFile, $Dummy, $ResourceType, $GroupId, $LocalId);
	$value = $LanguageId;
	$$hashref{$key}{$value} ++;
	$$langref{$value} ++;
    } continue {
	# reset line numbering on each input file
	close ARGV  if eof;     # Not eof()!
    }
}

sub mycmp {
    my @a = split(m/\t/, $a);
    my @b = split(m/\t/, $b);
    my $ret;
    foreach my $x (@a) {
	$y = shift @b;
	if ($x =~ m/\A[0-9]/) {
	    $ret = $x <=> $y;
	}
	else {
	    $ret = $x cmp $y;
	}
	return $ret unless $ret == 0;
    }
    return 0;
}

sub main {
    my %data;
    my %lang;
    reading(\%data, \%lang);

    # Valid projects
    my %projects;
    map { $projects{$_} ++ } @projects;

    # header
    my @lang = sort keys %lang;
    print join("\t", qw( Project SourceFile Dummy ResourceType GroupId LocalId ));
    foreach my $lang (@lang) {
	print "\t$lang";
    }
    print "\n";

    # body
    my $value;
    my ($Project, $SourceFile);
    my $flag;
    foreach my $key (sort { mycmp } keys %data) {
	($Project, $SourceFile) = split(m/\t/, $key);
	if (defined $projects{$Project}) {

	    $flag = 1;
	    if ($Project eq "helpcontent2") {
		# Help
		$flag = 0 if (!defined($data{$key}{"de"})); # German: http://pootle.sunvirtuallab.com/de/helpcontent2/
		$flag = 0 if ($SourceFile =~ m{source\\text\\shared\\01\\} and !defined($data{$key}{"it"})); # Italy: http://pootle.sunvirtuallab.com/it/helpcontent2/shared/?
		$flag = 0 if ($SourceFile =~ m{source\\text\\shared\\01\\} and !defined($data{$key}{"pl"})); # Polish: http://pootle.sunvirtuallab.com/pl/helpcontent2/
	    }
	    else {
		# UI
		$flag = 0 if (!defined($data{$key}{"tr"})); # Turkish: http://pootle.sunvirtuallab.com/tr/openoffice_org/

		if ($Project ne "basic") {
		    $flag = 0 if (!defined($data{$key}{"de"})); # German: http://pootle.sunvirtuallab.com/de/openoffice_org/
		    $flag = 0 if (!defined($data{$key}{"it"})); # Italy: http://pootle.sunvirtuallab.com/it/openoffice_org/
		    $flag = 0 if (!defined($data{$key}{"pl"})); # Polish: 
		}
	    }

	    if ($flag) {
		print "$key";
		foreach my $lang (@lang) {
		    $value = $data{$key}{$lang} + 0;
		    print "\t$value";
		}
		print "\n";
	    }
	    else {
		print STDERR "Excluded: $key\n";
	    }

	}
	else {
	    print STDERR "Excluded: $key\n";
	}
    }
}

main();

__END__

    
