#!/usr/local/bin/perl -w

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

$filename = $ENV{'QUERY_STRING'};
@args = split /\&/,$filename;
my $width=0;

if(@args > 1){
	foreach $x (@args){
		if($x =~ /(.*)=(.*)/) {
			if($1 eq 'lines'){
				$width = $2
			}
			if($1 eq 'file'){
				$filename = $2;
			}
		} else {
			$filename = $x;
		}
	}
}

print "<html><head><title>$filename</title></head><body><pre>\n";

if(-e $filename){
	if(open FILE, "<$filename"){			
		$line = 1;
		while(<FILE>){
			s/&/&amp;/g;
			s/</&lt;/g;
			s/>/&gt;/g;
			if($width){
				print (sprintf ('<i>%' . $width . 'd</i> ' , $line));
				$line ++;
			}
			print;
		}
		close FILE;
	} else {
		print "$filename could not be opened: $!\n";
	}
} else {
	print "$filename does not exist.\n";
}

print "</pre></body></html>\n";
