#!c:/www/perl/bin/perl.exe
@files = glob("htdocs/*.html");
push @files, glob("htdocs/docs/*.html");
push @files, glob("htdocs/source/*.html");
push @files, glob("share/templates/en_US/*.tmpl");

foreach $filename (@files) {
	print "Processing ".$filename."...";
	$content = readfile($filename);
	$content = ssi2jscript($content);
	writefile($filename, $content);
	print " done\n";	
}

showEndMessage();

sub ssi2jscript(my $content)
{
	$content =~ s/<!--#include virtual="([^"]*)"\s*-->/<!--#include file="$1" -->/smg;
	$content =~ s/<!--#set var="([^"]*)" value="([^"]*)"\s*-->/<% var $1 = "$2" %>\n/smg;
	$content =~ s/<!--#echo var="([^"]*)" -->/<%=$1%>/smg;
	$content =~ s/<!--#if expr="\$([^ ]*) = ([^"]*)" -->/<% if ($1 == $2) { %>/smg;
	$content =~ s/<!--#endif -->/<% } %>/smg;
	$content =~ s/action="check"/action="check.pl"/smg;
	
	return $content;
}

sub readfile(my $filename)
{
	open(FILE, $filename) || die("can't open datafile: $!\n");
	$content = "";
	while(<FILE>)
	{
		$content .= $_;
	}
	close(FILE);
	
	return $content;
}
sub writefile(my $filename, my $content)
{
	open(FILE, ">", $filename) || die("can't open datafile for writing: $!\n");
	print FILE $content;
	close(FILE);
}

sub showEndMessage()
{
	print << ".EOF.";
Remember to add
  \$path_info =~ s/(.*)\\/check.pl(.*)\$/\$2/
	if (\$ENV{SERVER_SOFTWARE} and \$ENV{SERVER_SOFTWARE} =~ /Microsoft-IIS/);
to your check.pl in prepCGI().
.EOF.

}
