#!/usr/bin/perl4
#
# sig -- 'Grep' sunsite index
#
# Thrown together by jepler@herbie.unl.edu

#Configuration: Where to keep the INDEX, the max age in days before an
#update is done, the command to get a new index, and the web viewer you want
#started to view the page

$INDEX = "/usr/src/ftp/INDEX.whole";
$MAXAGE = 3;
$NEWINDEX =
     "lynx -source ftp://sunsite.unc.edu/pub/Linux/INDEX.whole.gz|zcat>$INDEX";

#$NEWINDEX = "lynx -source ftp://sunsite.unc.edu/pub/Linux/INDEX.whole>$INDEX"; # For faster connections

if ($ARGV[0] =~ /^$/ ) {
	print "Usage: $0 regex [regex ...]\n";
	exit;
}

if ( (-M $INDEX) >= $MAXAGE ) {
	print STDERR "Getting new index file... Yours is old\n";
	system $NEWINDEX || die "Index Update Failed: $!";
}

open(IF,"<" . $INDEX) || die "$INDEX: $!";
open(OF,">/tmp/sundex$$.html") || die "/tmp/sundex$$.html: $!";

print OF "<HTML><TITLE> Index generated for @ARGV </TITLE><BODY>\n";

LINE: while(<IF>) {

	if (/^\//) {
		$prefix = $_ . <IF> ;
	}
	else {
		for $i (@ARGV) {
			if (/$i/) {
				print $prefix, $_;
				$prefix="";
				next LINE; # Only catch lines once
			}
		}
	}
}
