#!/usr/local/bin/./perl

$gunzip = "/usr/local/bin/gunzip";
$GLIMPSE_LOC = "/usr/local/glimpse";
$GREP_LOC = $GLIMPSE_LOC;

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

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

$query = $ENV{'QUERY_STRING'};

#  Strip the variables out from the query string,
#  and assign them into variables, prefixed by 'QS_'
@qvars = split( /\&/, $query );
foreach (@qvars) {
   split(/=/);
   $fname = $_[0];
   $fvalue = $_[1];
   $fvalue =~ s/\'//g;
   $cmd = "\$QS_$fname = '$fvalue';" ;
#    print ">>>",$cmd,"\n";
   $cmd = eval $cmd if ( $fname =~ /^[a-z_A-Z]\w*$/ );
}

$indexdir = $ARGV[0];
$file = $QS_file;
$path = $QS_file;
if ($path =~ m#/\.\./#) { &err_noaccess;}
$line = $QS_line;

# 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<br>query=$query<br>\n";

# you may comment this check if you want to decrease security
#  for a shorter execution
# $GREP_LOC = "/usr/bin/grep";
  if (system("$GREP_LOC -k $file $indexdir/.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) {
		print "<A NAME=\"mfs\"><B>";
	}
	if ($line && $. == $line+1) {
		print " </B></A>";
	}

	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;
}
