#!/bin/perl
#
$filedir="Resources/languages/";
$langfile="edustrings.resx";

sub parse_file
{
	my $filename = "@_";
	my $front = $filename;
	my %items;
	my $program = $filename;

	$program =~ s/\..*//g;
	$front =~ s/\..*//g;
	$front =~ s/[a-z]//g;
	$front = "${front}_";
	#print "$filename\n";
	#print "$front\n";
	my $count=0;
	if ( -f $filename )
	{
		open(INFILE, $filename);
		while ( <INFILE> )
		{
			if(/([^.]*)\.Text = (.*)$/)
			{
				$var=$1;
				$text=$2;

				$var=~ s/ *//g;
				if($var eq "this") { $var="Form"};
				$item=$var;
				$var="$front$var";

				$text=~ s/[";]//g;
				$text=~ s/\r//g;
				$text=~ s/\n//g;
				#print "Var: $var Text: $text\n";
				#now we process them.  If it does not exist yet, make an entry for it.
				if(!exists($existing{$var}))
				{
					#print("var $key   item $items{$key}\n");
					print EDUFILE ('  <data name="' . $var . '" xml:space="preserve">' . "\r\n");
					printf EDUFILE ('    <value>%s</value>%s',$text,"\n");
					printf EDUFILE ('    <comment>%s</comment>%s',"$program $item = $text","\n");
					print EDUFILE ("  </data>\n");

					if($count == 0)
					{
						print CFILE "$program\n";
						print CFILE "private void LanguagifyComponents()
			{
			    ResourceManager RM = NB.GetResource();
			    CultureInfo CI = NB.GetCulture();\n"
					}
					if($item == "Form")
					{
						print CFILE ("Text = RM.GetString(\"$var\", CI);\n");
					} 
					else
					{
						print CFILE ("$item.Text = RM.GetString(\"$var\", CI);\n");
					}
					$count++;
				}
			}
		}
		close INFILE;
		if($count > 0)
		{
			print CFILE ("}\n\n");
		}
	}
	else
	{
		print "$filename does not exist\n";
	}
}

#Read the language file to see what already exists.
$tfile="$filedir/$langfile";
open (IFILE, $tfile) or die "$tfile does not exist.";
while ( <IFILE> ) 
{
	if(/data name="([^"]*)"/)
	{
		$tag=$1;
		#print("TAG: $tag\n");
		$existing{$tag}="here";
	}
}
close(IFILE);

open(EDUFILE, ">add2edustrings.txt");
open(CFILE, ">add2cfiles.txt");

#parse the various Designer files and process them
my @files = glob("*.Designer.cs");
foreach my $file (@files) 
{
        print "$file\n";
	parse_file( $file );
}


close(CFILE);
close(EDUFILE);
