#!/usr/bin/perl

#Calendar Translator - Toni Hermoso - toniher@softcatala.org
#Under the GNU/GPL License -> http://www.gnu.org/copyleft/gpl.html

my $typefile = $ARGV[0]; #Filetype 0->DTD, 1->properties
my $fileprev = $ARGV[1]; #Previous en-US file
my $filepost = $ARGV[2]; #Current en-US file
my $filecat = $ARGV[3]; #Previous translation

#Standard Output is Current translation to be finished
#Nontranslated or changed strings are marked with '*'

my %entity;
my %entitycat;
my %props;
my %propscat;

my @preentity;

#Process DTD Files

if ($typefile == '0') {
	
open (FILEPREV, $fileprev) || die "Cannot open $fileprev!";

while (<FILEPREV>) {



	if ($_=~/^\<\!ENTITY\s+(\S+)\s+\"(.*)\"\s*\>/) {

		$entity{$1} = $2;

	}
	
	if ($_=~/^\<\!ENTITY\s+(\S+)\s+\"(.*)\w\s*$/) {

		@preentity = ();
		push(@preentity, $1);
		push(@preentity, $2);
	}

	if ($_=~/^\s+(.*)\"\s*\>/) {

		
		my $prov = $1." ".$preentity[1];
		$entity{$preentity[0]} = $prov;
		
	}
	

}

close (FILEPREV);


open (FILECAT, $filecat) || die "Cannot open $filecat!";

while (<FILECAT>) {


	if ($_=~/^\<\!ENTITY\s+(\S+)\s+\"(.*)\"\s*\>/) {

		$entitycat{$1} = $_;

	}

	if ($_=~/^\<\!ENTITY\s+(\S+)\s+\"(.*)\w\s*$/) {

		@preentity = ();
		
		push(@preentity, $1);
		push(@preentity, $2);
	}

	if ($_=~/^\s+(.*)\"\s*\>/) {

		
		my $prov = $1." ".$preentity[1];
		$entitycat{$preentity[0]} = $prov;
		
	}


}




close (FILECAT);



open (FILEPOST, $filepost) || die "Cannot open $filepost!";

while (<FILEPOST>) {

	my $line = $_;
	
	if ($_=~/^\<\!ENTITY\s+(\S+)\s+\"(.*)\"\s*\>/) {

		unless ((-e $entity{$1}) or ($entity{$1} eq $2)) {
		
			print "*", $line; 
		}
		
		if ($entity{$1} eq $2) { print $entitycat{$1};}
	}

	if ($_=~/^\<\!ENTITY\s+(\S+)\s+\"(.*)\w\s*$/) {

		@preentity = ();
		
		push(@preentity, $1);
		push(@preentity, $2);
	}

	if ($_=~/^\s+(.*)\"\s*\>/) {

		
		my $prov = $preentity[1]."\n                                       ".$1;
	
		unless ((-e $entity{$1}) or ($entity{$1} eq $prov)) {
		
			print "*<!ENTITY $preentity[0]  \"$prov\">\n";
		}
		
		if ($entity{$1} eq $prov) { print $entitycat{$1};}
	}


	unless (($_=~/^\<\!ENTITY/) or ($_=~/\"\>\s*$/)) {

		print $line;
	}

}
close (FILEPOST);

}


#Process Properties files
if ($typefile == '1') {

open (FILEPREV, $fileprev) || die "Cannot open $fileprev!";

while (<FILEPREV>) {


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

		
		if ($_=~/^(\S+)\s*\=\s*(\S.*)\s*$/) {

			$props{$1} = $2;

		}
	}


}

close (FILEPREV);



open (FILECAT, $filecat) || die "Cannot open $filecat!";

while (<FILECAT>) {

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

		if ($_=~/^(\S+)\s*\=\s*(\S.*)\s*$/) {
	
			$propscat{$1} = $_;


		}

	}

}




close (FILECAT);


open (FILEPOST, $filepost) || die "Cannot open $filepost!";

while (<FILEPOST>) {

	my $line = $_;
	
	unless ($_=~/^\#/) {
	
		if ($_=~/^(\S+)\s*\=\s*(\S.*)\s*$/) {

			unless ((-e $props{$1}) or ($props{$1} eq $2)) {
		
				print "*", $line;
			}
		
			if ($props{$1} eq $2) { print $propscat{$1};}
		}

		else {

			print $line;
		}
	}
}

close (FILEPOST);

	
}
