#!/usr/bin/perl

my $pofile = $ARGV[0];
my $propfile = $ARGV[1];

#First PO file

open (FILE, $pofile) || die "Cannot open $pofile!";

my $switch = 0;
my $msgmult = 0;
my %pofile;
my @stringi;
my $stringf;

while (<FILE>) {

	#Identificator
	if ($_=~/^\#\:\s+(\S+)/) {

		$switch = 0;
		$msgmult = 0;		
		#repeated strings
		push (@stringi, $1);	
		$switch++;
	}

	#Main lines

	if (($switch > 0) and ($_=~/^msgstr\s+\"(.*)\"\s*$/)){
	
		$stringf = $1;

		foreach my $stringc (@stringi) {
			
			$pofile{$stringc} = $stringf;
		}

		$msgmult++;
	}



	#Extra lines;

	if (($switch > 0) and ($_=~/^\"(.*)\"/) and ($msgmult>0)) {

		$stringf = $1;
		
		
		foreach my $stringc (@stringi) {
			
			$pofile{$stringc} .= $stringf;
		}
	
	}

	if (($switch > 0) and ($_=~/^\s.*$/)) {

		@stringi = ();
	}
}

close (FILE);

foreach my $cas (keys %pofile) {

	print STDERR $cas, "->", $pofile{$cas}, "\n";
}

#Then properties file

open (FILE, $propfile) || die "Cannot open $propfile!";

while (<FILE>) {

	if ($_=~/^\S/) {

		unless ($_=~/^\#/) {

			(my $header) = $_=~/^(\S+)\s*\=\s*/;

			#Check params
			if ($header=~/\.params$/) {print $_;}
				
			else {print "$header = $pofile{$header}\n";}
			
		}
		
		else {print;}
	}	

	else {print;}
}

close (FILE);


