#!/usr/bin/perl

# First of all, let's load HELM configuration
use Env;
my $HELM_LIB_DIR = $ENV{"HELM_LIB_DIR"};
# this should be the only fixed constant
my $DEFAULT_HELM_LIB_DIR = "/projects/helm/on-line/local/lib/helm";
if (defined ($HELM_LIB_DIR)) {
   $HELM_LIB_PATH = $HELM_LIB_DIR."/configuration.pl";
} else {
   $HELM_LIB_PATH = $DEFAULT_HELM_LIB_DIR."/configuration.pl";
}

# next require defines: $helm_dir, $html_link, $dtd_dir, $uris_dbm
require $HELM_LIB_PATH;

use URI::Escape;
use LWP::UserAgent;
use CGI;

my $cgi = new CGI($ENV{"REQUEST_URL"});
$uri = $cgi->param('cicuri');
$uri =~ s/(.*)\/$/$1/; # Remove a final slash if present
$uri .= "/"; # Put a final slash
$myurl = $cgi->url();

$cont = ""; # modified by side-effect by &callback
my $ua = LWP::UserAgent->new;
my $request = HTTP::Request->new(GET => $getter_url."ls?format=txt&baseuri=".$uri);
my $response = $ua->request($request, \&callback);
 
@filenames = split(/\n/, $cont);

my $uridotdot = $uri;
$uridotdot =~ s/(.*)\/.+/$1/;
if ($uri ne "cic:/") {
 # Let's print the link to the parent directory
 $output = <<EOT;
<tr>
<td><img border="0" src="/icons/back.gif" alt="[BACK]"></td>
<td><a href="$myurl" onClick="top.frames[0].document.forms[0].hiddencicuri.value='$uridotdot'; refreshReload(); refreshCICHeader(); this.search='?cicuri=$uridotdot';" onMouseOver="window.status='$uridotdot'; return true">Parent Directory</a></td>
</tr>
EOT
}

foreach $i (@filenames) {
   my ($type,$name,$flags) = split(/, /, $i);
   my ($flagscic, $flagstypes) = split(/,/, $flags);
   $flagscic =~ s/^<(.*)/$1/;
   $flagstypes =~ s/(.*)>$/$1/;
   if ($type eq "dir") {
      $output .= <<EOT;
<tr>
<td><img border="0" src="/icons/folder.gif" alt="[DIR]"></td>
<td><a href="$myurl" onClick="top.frames[0].document.forms[0].hiddencicuri.value='$uri$name'; refreshReload(); refreshCICHeader(); this.search='?cicuri=$uri$name';" onMouseOver="window.status='$uri$name'; return true">$name</a></td>
</tr>
</tr>
EOT
   } elsif ($type eq "object") {
	 if ($name =~ /\.(con|var|ind)$/) {
	    # cic file
            $output .= <<EOT;
<tr>
<td><img border="0" src="/icons/generic.red.gif" alt="[DIR]"></td>
<td><a href="" target="cic" onClick="this.href=makeURL('cic','$uri$name','$processor_url','$getter_url','$flagscic','$flagstypes')" onMouseOver="window.status='$uri$name'; return true">$name</a>
</tr>
EOT
         } else {
            $output .= <<EOT;
<tr>
<td><img border="0" src="/icons/unknown.gif" alt="[DIR]"></td>
<td>$name</td>
</tr>
EOT
	 }
   } else {
      $output .= <<EOT;
<tr>
<td><img border="0" src="/icons/burst.gif" alt="[DIR]"></td>
<td>$name</td>
</tr>
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>
<script language="JavaScript" src="http://phd.cs.unibo.it/helm/javascript/control.js">
</script>
</head>
<body bgcolor="#ffffff" text="#000000">
<table>
$output
</table>
<hr noshade align="left" width="80%">
</body>
</html>
EOT

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

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