#!/usr/bin/perl

my $HELM_CONFIGURATION_PREFIX = $ENV{"HELM_CONFIGURATION_PREFIX"};
my $HELM_CONFIGURATION_PATH =
 $HELM_CONFIGURATION_PREFIX."/local/lib/helm/configuration.pl";
 # next require defines: $helm_dir, $html_link, $dtd_dir, $uris_dbm
 require $HELM_CONFIGURATION_PATH;

require HTTP::Request;
require LWP::UserAgent;
use CGI;

$url = $ENV{"REQUEST_URI"};
$url =~ s/.*mmlurl=(.*)/$1/;

$content = "";

$ua = LWP::UserAgent->new;
$request = HTTP::Request->new(GET => "$url");
$response = $ua->request($request, \&callback);

$content =~ s/\r//sg;
$content =~ s/\n/ /sg;
$content =~ s/<!DOCTYPE[^>]*(.*)>/$1/sg;
$content =~ s/<m:/</g;
$content =~ s/<\/m:/<\//g;
$content =~ s/<math[^>]*>/<math>/sg;
$content =~ s/"/'/g;
$content =~ s/&/&amp;/g;

print <<EOT;
Content-type: text/html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title>Generato in automatico a partire da $url</title>
</head>
<body bgcolor="#ffffff" text="#000000">
<applet codebase="http://www.webeq.com/WebEQ/2.2/classes" code="webeq.Main"
+width=600 height=2000 align=middle>
<param name=eq value="$content">
<param name=color value="#cccccc">
<param name=parser value="mathml">
<param name=size value="14">
</applet>
</body>
</html>
EOT

#=========================

sub callback
{ 
 my ($data) = @_;
 $content .= $data;
}
