#!/usr/bin/perl

#Directory in English
my $diren = $ARGV[0];
#Directory in Catalan
my $dirca = $ARGV[1];

opendir (DIREN, $diren) || die "Cannot open $diren!";

my @level1en = readdir (DIREN);

closedir (DIREN);

my @level2en;
my @level3en;


foreach my $subdir (@level1en) {

	if (-d "$diren\/$subdir") {

		opendir (DIR2EN, "$diren\/$subdir") || die "Cannot open $diren/$subdir!";
		
		@level2en = readdir (DIR2EN);
		
		closedir (DIR2EN);

		
		foreach my $sub2dir (@level2en) {

			if (-d "$diren\/$subdir\/$sub2dir") {

				opendir (DIR3EN, "$diren\/$subdir\/$sub2dir") || die "Cannot open $diren/$sub2dir!";
		
				@level3en = readdir (DIR3EN);
		
				closedir (DIR3EN);

				foreach my $file (@level3en) {

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

						&makefile ("prop", "$subdir\/$sub2dir\/$file" );

					}

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

						&makefile ("dtd", "$subdir\/$sub2dir\/$file" );

					}

				}

			}

			if ($sub2dir=~/\.properties\s*$/) {

				&makefile ("prop", "$subdir\/$sub2dir" );

			}
			
			if ($sub2dir=~/\.dtd\s*$/) {

				&makefile ("dtd", "$subdir\/$sub2dir" );

			}

			
			
		}
		

	}
	
}

sub makefile {

	
	my $typefile = shift;
	my $location = shift;

	$_ = $location;
	s/\/ca\//\/en-US\//g;
	s/\-ca\//\-en-US\//g;
		
	my $locationen = $_;
	
	my %string;

	my %uni;

	$uni{"u00C0"} = "À";
	$uni{"u00C8"} = "È";
	$uni{"u00C9"} = "É";
	$uni{"u00CD"} = "Í";
	$uni{"u00D2"} = "Ò";
	$uni{"u00D3"} = "Ó";
	$uni{"u00DA"} = "Ú";
	$uni{"u00E0"} = "à";
	$uni{"u00E8"} = "è";
	$uni{"u00E9"} = "é";
	$uni{"u00ED"} = "í";
	$uni{"u00F2"} = "ò";
	$uni{"u00F3"} = "ó";
	$uni{"u00FA"} = "ú";
	$uni{"u00EF"} = "ï";
	$uni{"u00FC"} = "ü";
	$uni{"u00E7"} = "ç";
	$uni{"u00B7"} = "·";
	

	if ($typefile eq "prop") {

		
		print STDERR " ^ $diren\/$locationen\n";
		open (FILEEN, "$diren\/$locationen") || die "Not EN!";

		while (<FILEEN>) {

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


				my ($left, $right) = $_=~ /^(\S+)\s*\=\s*(.*)/;
				

				chomp ($right);

				$string{$left} = [$right];
			}

		}

		close (FILEEN);
		
		print  STDERR " ^ $dirca\/$location\n";
		
		open (FILECA, "$dirca\/$location") || die "Not CA!";
		
		while (<FILECA>) {

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

			
				
				my ($left, $right) = $_=~ /^(\S+)\s*\=\s*(.*)/;
				
				$right=~s/\\(u\S{4})/$uni{"$1"}/g;

				chomp ($right);

				
				push (@{$string{$left}}, $right);

			}
		}

		close (FILECA);
	}

	
	if ($typefile eq "dtd") {

		print  STDERR " ^ $diren\/$locationen\n";
		 
		open (FILEEN, "$diren\/$locationen") || die "Not EN!";

		while (<FILEEN>) {

			if ($_=~/^\<\!ENTITY/) {


				my ($left, $right) = $_=~ /^\<\!ENTITY\s+(\S+)\s+\"(.*)\"\>\s*$/;



				
				chomp ($right);
				
				$string{$left} = [$right];					

			}
		}	

		close (FILEEN);
		 
		print STDERR " ^ $dirca\/$location\n";
		
		open (FILECA, "$dirca\/$location") || die "Not CA!";
		
		while (<FILECA>) {

			if ($_=~/^\<\!ENTITY/) {

				

				my ($left, $right) = $_=~ /^\<\!ENTITY\s+(\S+)\s+\"(.*)\"\>\s*$/;
				
				
				chomp ($right);
				
				push (@{$string{$left}}, $right);

			}			
		}

		close (FILECA);
		
	}
	
	#print "\n\n*** $location ***\n\n";
	
	
	foreach my $entry (keys %string) {

		print $string{$entry}->[1], "\n";
		
	}

}
