#!/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;

$baseuri0 = $dirname = $uri = $ENV{"REQUEST_URI"};

$dirname =~ s/$helm_url_path//;
$dirname = $helm_dir.$dirname;

$baseuri0 =~ s/$helm_url_path//;

opendir(DIR, $dirname);
@filenames = readdir(DIR);
closedir(DIR);

$output = "";
foreach $i (@filenames) {
   if ($i eq "..") {
      $output .= <<EOT;
<img border="0" src="/icons/back.gif" alt="[BACK]"> <a href="$uri$i">Parent Directory</a>
EOT
   } elsif ($i !~ /^\./) {
      # hidden files excluded
      (undef,undef,$mode) = stat("$dirname$i");
      if ($mode &= 16384) {
         # directory
         $output .= <<EOT;
<img border="0" src="/icons/folder.gif" alt="[DIR]"> <a href="$uri$i">$i</a>
EOT
      } else {
	 # file
	 if ($i =~ /\.(con|var|ind)\.xml$/) {
            my $i_without_xml = $i;
            $i_without_xml =~ s/(.*)\.xml/$1/;
	    # cic file
            my $baseuri = "cic:".$baseuri0;
            $output .= <<EOT;
<img border="0" src="/icons/generic.red.gif" alt="[DIR]"> <a
href="$uri$i">$i</a> <a
href="$getheader_url?baseurl=$style_url&stylesheet1=rootcontent.xsl&stylesheet2=annotatedpres.xsl&xmluri=$baseuri$i_without_xml">MathML</a> <a href="$getheader_url?baseurl=$style_url&stylesheet1=rootcontent.xsl&stylesheet2=content_to_html.xsl&xmluri=$baseuri$i_without_xml">HTML</a> <a href="$webeq_url?mmlurl=$getheader_url?baseurl=$style_url&stylesheet1=rootcontent.xsl&stylesheet2=annotatedpres.xsl&xmluri=$baseuri$i_without_xml">WEBEQ</a> <a href="$webeqp_url?mmlurl=$getheader_url?baseurl=$style_url&stylesheet1=rootcontent.xsl&stylesheet2=annotatedpres.xsl&xmluri=$baseuri$i_without_xml">WEBEQ PRESENTATION ONLY</a>
EOT
	 } elsif ($i =~ /\.(con|var|ind)\.ann\.xml$/) {
            my $i_without_xml = $i;
            my $i_without_ann_and_xml = $i;
            $i_without_xml =~ s/(.*)\.xml/$1/;
            $i_without_ann_and_xml =~ s/(.*)\.ann\.xml/$1/;
	    # cic file
            my $baseuri = "cic:".$baseuri0;
            $output .= <<EOT;
<img border="0" src="/icons/text.gif" alt="[DIR]"> <a href="$uri$i">$i</a> <a href="$getheader_url?baseurl=$style_url&stylesheet1=rootcontent.xsl&stylesheet2=annotatedpres.xsl&xmluri=$baseuri$i_without_ann_and_xml&annuri=$baseuri$i_without_xml">MathML</a> <a href="$getheader_url?baseurl=$style_url&stylesheet1=rootcontent.xsl&stylesheet2=content_to_html.xsl&xmluri=$baseuri$i_without_ann_and_xml&annuri=$baseuri$i_without_xml">HTML</a> <a href="$webeq_url?mmlurl=$getheader_url?baseurl=$style_url&stylesheet1=rootcontent.xsl&stylesheet2=annotatedpres.xsl&xmluri=$baseuri$i_without_ann_and_xml&annuri=$baseuri$i_without_xml">WEBEQ</a> <a href="$webeqp_url?mmlurl=$getheader_url?baseurl=$style_url&stylesheet1=rootcontent.xsl&stylesheet2=annotatedpres.xsl&xmluri=$baseuri$i_without_ann_and_xml&annuri=$baseuri$i_without_xml">WEBEQ PRESENTATION ONLY</a>
EOT
	 } elsif ($i =~ /\.theory\.xml$/) {
            my $i_without_xml = $i;
            $i_without_xml =~ s/(.*)\.xml/$1/;
	    # theory file
            my $baseuri = "theory:".$baseuri0;
            $output .= <<EOT;
<img border="0" src="/icons/layout.gif" alt="[DIR]"> <a
href="$uri$i">$i</a> <a href="$getheader_url?baseurl=$style_url&stylesheet1=theory_content.xsl&stylesheet2=theory_pres.xsl&xmluri=$baseuri$i_without_xml">MathML</a> <a href="$getheader_url?baseurl=$style_url&stylesheet1=roottheory.xsl&stylesheet2=content_to_html.xsl&xmluri=$baseuri$i_without_xml">HTML</a>
EOT
         } else {
	    # other file
            $output .= <<EOT;
<img border="0" src="/icons/unknown.gif" alt="[DIR]"> <a href="$uri$i">$i</a>
EOT
	 }
      }
   }
}

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>Index of $uri</title>
</head>
<body bgcolor="#ffffff" text="#000000">
<table><tr><td bgcolor="#ffffff" class="title">
<font size="+3" face="Helvetica,Arial,sans-serif"><b>Index of $uri</b></font>
</td></tr></table>
<hr noshade align="left" width="80%">
<pre>
$output
</pre>
<hr noshade align="left" width="80%">
</body>
</html>
EOT
