#!/usr/bin/perl

#Migrate to a clean format from MT... Directory based

my $diren = $ARGV[0]; 
my $dirca = $ARGV[1]; 

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

my %entity;
my %props;
my @files;
my @dtdfiles; #list of DTD files
my @propfiles; #list of properties files

#Open DIREN;

opendir (DIREN, $diren) || die "cannot open English directory";

@files =  readdir(DIREN);
closedir(DIREN);

print @files;
#for props 
foreach my $file (@files) {


		if ($file=~/\.properties/) {

			push (@propfiles, $file);
		}

		if ($file=~/\.dtd/) {

			push (@dtdfiles, $file);

		}
	}


print @propfiles, "\n";

open (FILEERR, ">$dirca/err");

foreach my $dtd (@dtdfiles) {
	
	open (FILEOUT, ">$dirca/$dtd.new");

	#Extreu info del català
	open (FILECAT, "$dirca/$dtd") || die "Cannot open CAT!";

	while (<FILECAT>) {
		

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

			$entity{$1} = $_;

		}

	}

	close (FILECAT);
	

	open (FILEEN, "$diren/$dtd") || die "Cannot open EN";

	while (<FILEEN>) {



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

			if (defined $entity{$1}) {

				print FILEOUT $entity{$1};
			}

			else {print FILEOUT $_; print FILEERR "$dtd -> $_";}
		}
		
		else {
			
			if ($_=~/END LICENSE BLOCK/) {

				print FILEOUT $_, "\n<!-- Catalan Translation: Projecte Mozilla en català - Softcatalà - http://www.softcatala.org/projectes/mozilla/ -->\n\n";
			}
		
			else {	print FILEOUT $_;}

		}


	}

	close (FILEEN);
	close (FILEOUT);
}

#Process Properties files

foreach my $prop (@propfiles) {

	open (FILEOUT, ">$dirca/$prop.new");
	
	open (FILECAT, "$dirca/$prop") || die "Cannot open CAT!";

	while (<FILECAT>) {

		#Clean
		s/\\:/:/g;
		s/\\=/=/g;
		s/\\#/#/g;
		s/\\u00C0/À/g;
		s/\\u00C8/È/g;
		s/\\u00C9/É/g;
		s/\\u00CD/Í/g;
		s/\\u00D2/Ò/g;
		s/\\u00D3/Ó/g;
		s/\\u00DA/Ú/g;
		s/\\u00E0/à/g;
		s/\\u00E8/è/g;
		s/\\u00E9/é/g;
		s/\\u00ED/í/g;
		s/\\u00F2/ò/g;
		s/\\u00F3/ó/g;
		s/\\u00FA/ú/g;
		s/\\u00EF/ï/g;
		s/\\u00FC/ü/g;
		s/\\u00E7/ç/g;
		s/\\u00B7/·/g;

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

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

			}

		}

	}


	close (FILECAT);


	open (FILEEN, "$diren/$prop") || die "Cannot open EN!";

	while (<FILEEN>) {

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

	
				if (defined $props{$1}) { print FILEOUT $props{$1};}

				else {print FILEOUT $line ; print FILEERR "$prop -> $line";}
			}

			else {

				print FILEOUT $line;
			}
		}
		
		else {

			if ($_=~/END LICENSE BLOCK/) {

				print FILEOUT $_, "#\n# Catalan Translation: Projecte Mozilla en català - Softcatalà - http://www.softcatala.org/projectes/mozilla\n\n";
			}
		
			else {	print FILEOUT $line;}
		}
	}

	close (FILEEN);
	close (FILEOUT);
	
}

close (FILEERR);

#clean
opendir (DIR, $dirca) || die "Cannot open $dir!";

my @newfiles;

@newfiles = grep {$_=~/\.new$/} readdir DIR;
closedir (DIR);

print @newfiles;

foreach $new (@newfiles) {

	my ($pre) = $new =~/^(\S+)\.new$/;
	system ("mv $dirca/$new $dirca/$pre");
}

#system ("rm $dir/err");

