#!/usr/local/bin/perl

$gunzip="/usr/local/bin/gunzip" ;

##############################################
#                                            #
# no configuration is needed below this line #
#                                            #
##############################################

print "Content-type: text/html\n\n";

undef $line;
if ($ENV{'QUERY_STRING'} =~ /\d+/) {
	$line = $ENV{'QUERY_STRING'};
}

$path_info = $ARGV[0];
$_ = $path_info;

# /<length>/$indexdir/$path is the format of the PATH_INFO
if ( m|^/([0-9]*)(.*)$| ) {
	$length = $1;
	$path = $2;
	$path =~ s|"||g;
} else {
	&err_noscript;
}

$indexdir = substr($path,0,$length);
$file = substr($path,$length,length($indexdir));

# make sure the indexdir exists, and is a valid archive dir
if (!(-d $indexdir)){
	&err_noindexdir;
}
if (!(-e "$indexdir/archive.cfg")){
	&err_badindexdir;
}

# get the url for the file from the config
open(CFG, "$indexdir/archive.cfg") || &err_badconfig;
$input = <CFG>;
($title,$url,$subindex) = split("\t", $input);
close(CFG);

# print "indexdir = $indexdir<br>file = $file<br>line = $line\n";

# you may uncomment this if you want to increase security
# at the expense of longer execution
#if (system("egrep $1\$ $check/.glimpse_filenames >/dev/null".
#	" 2>/dev/null") != 0) {
#    &err_noaccess;
#}

$effname = $path;
$name = $path;
if ($path =~ /^(.*)\.Z$/) {
	$effname = "exec $gunzip < $path|";
	$name = $1;
} elsif ($path =~ /^(.*)\.gz$/) {
	$effname = "exec $gunzip < $path|";
	$name = $1;
} elsif ($path =~ /^(.*)\.zip$/) {
	$effname = "exec $gunzip < $path|";
	$name = $1;
}

if (!-f $path) {
        print "<TITLE>File \"$path\" does not exist</TITLE>\n";
        print "<H1>File \"$path\" does not exist</H1>\n";
        exit;
}
if (!open(INPUT,$effname)) {
        print "<TITLE>Cannot read file \"$path\"</TITLE>\n";
        print "<H1>Cannot read file \"$path\": $!</H1>\n";
        exit;
}

$HTML = 1;
if ($name !~ /\.s?html?$/) {
	print "<PRE>\n";
	$HTML = 0;
}


$baseurl = $url.$file;


LINE:
while (<INPUT>) {
	if ($HTML) {
		$baseurl &&
			s#<title>#<BASE HREF=\"$baseurl\">$&#i;
		s|\&lt;([\w\$][-\w.\$]*\@\w[-\w.]+)&gt;|\<a href="/cgi-bin/artbyid?$1"\>\&lt;$1\&gt;\</a\>|g;
	} else {
		s|\&|\&amp;|g;
		s|\<|\&lt;|g;
		s|\>|\&gt;|g;
		s|\&lt;([\w\$][-\w.\$]*\@\w[-\w.]+)&gt;|\<a href="/cgi-bin/artbyid?$1"\>\&lt;$1\&gt;\</a\>|g;
		# s|\bgopher://([^ ><'")(]*[\w\/])\b|\<a href="$&">$&\</a>|g;
		# s|\bhttp://[-.\w?/+&\%:]+[.\w/]\b|\<a href="$&">$&\</a>|g;
		# s|\bftp://[-.\w/+]+[.\w/]\b|\<a href="$&"\>$&\</a\>|g;
		s#\bgopher://[^\s><'")(]+[\w/]#\<a href="$&">$&\</a>#g;
		s#\bhttp://[^\s><'")(]+[\w/]#\<a href="$&">$&\</a>#g;
		s#\bftp://[^\s><'")(]+[\w/]#\<a href="$&">$&\</a>#g;
	}
	if ($line && $. == $line) {
		# /a was moved here as a suggestion by seavey@openmarket.com
		# there need not be any text between <a name> and </a>, so this
		# should work ok
		print "<A NAME=\"mfs\"></A><B>";  
	}
	if ($line && $. == $line+1) {
		print " </B>";
	}

	print;
}
if ($HTML) {
	print "</PRE>\n";
}
close(INPUT);

sub err_badconfig {
	print "<TITLE>Error</TITLE>\n";
	print "<H1>Error with \"$indexdir\"</H1>\n";
	print "Cannot open configuration file for archive directory.\n";
	exit;
}
sub err_noindexdir {
	print "<TITLE>Error</TITLE>\n";
	print "<H1>Error with \"$indexdir\"</H1>\n";
	print "Archive directory does not exist.\n";
	exit;
}
sub err_badindexdir {
	print "<TITLE>Error</TITLE>\n";
	print "<H1>Error with \"$indexdir\"</H1>\n";
	print "Directory is not an archive directory.\n";
	exit;
}
sub err_noaccess {
	print "<TITLE>Access denied</TITLE>\n";
	print "<H1>Access to \"$path\" denied</H1>\n";
	print "You don't have permission to get file \"$path\"\n";
	print "from this site.\n";
	exit;
}
