#!/usr/bin/perl

# Name this index.html and put it in a directory you want to be browsable.
# Make sure this script has the execute bits set on it.
#
# Cameron Kaiser

$j = $ENV{'SCRIPT_FILENAME'}; $j =~ s#/[^/]*$##; chdir($j) || die;
$j = $ENV{'SCRIPT_NAME'}; $j =~ s#/[^/]*$##; opendir(D, ".") || die;

print <<"EOF";
HTTP/1.0 200 OK
Content-type: text/html

<html>
<body>
<h1>Index of $j</h1>
<pre>
EOF

if (-r "README" && -s "README") {
	open(Q, "README"); while(<Q>) { print stdout $_; } close(Q);
	print stdout "\n\n";
}

format STDOUT =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<   @>>>>>>>>> bytes   @<<<<<<<<<<<<<<
"$j</a>", -s $q, ((-d $q) ? "Directory" : ($q =~ /\.s?htm?l?$/i) ? "HTML Document" : ($q =~ /\.(gif|jpg|jpeg)$/i) ? "Image" : ($q =~ /\.(lha|lzh|gz|zip)$/i) ? "Archive" : "")
.

while(defined($q = readdir(D))) {
	next if ($q =~ /^\./); $j = $q;
	print "<a href = \"$q\">"; $j =~ s/(^.{39}).+$/\1.../; write;
}

closedir(D);

print <<"EOF";
</pre>
<hr>
<address>generated by
<a href = "http://httpi.ptloma.edu/">$ENV{'SERVER_SOFTWARE'}</a></address>
</body>
</html>
EOF

 1;
