#! /usr/bin/perl
###############################################################################
#
# Page loader (for doing postprocessing of data)
#
# Written by Behan Webster <behanw@verisim.com>
# Copyright (c) 1996 by Verisim, Inc.  All rights reserved.
#
###############################################################################


#
# Default user routines
#

sub PostProcessURL {
	my($ferret, $url) = @_;
	return $url;
}

sub PostProcessURLData {
	# NOTE: that $data, $head, and $words are references
	#       and need to be used as $$data, $$head, and
	#       @$words respectfully.

	my($ferret, $url, $data, $head, $words, $pristine) = @_;

	if ($$head =~ m|^content-type:\s*text/html$|mi) {
		Ferret::MarkHTMLWords($data,"<font color=#FF0000>","</font>",@$words);
	} elsif ($$head =~ m|^content-type:\s*text/|mi) {
		Ferret::MarkTextWords($data,">>>","<<<",@$words);
	}

	return 1;
}


require query;


###########################################################################
# Parse the command arguments
#
sub loadError {
	my ($data, $msg) = @_;

	$$data =~ s!(</?html.*?>|<head.*?>.*?</head.*?>|<title.*?>.*?</title.*?>|</?body.*?>)!!gis;

	contentHTML();
	title("Results Error");

print qq{
<body>
<blink>$msg</blink>
<hr size=8>
$$data
</body>
};

	end();

}

###########################################################################
# Parse the command arguments
#
my ($pristine, $query, $url, @words);
if ($#ARGV<0) {
	if ($ENV{'QUERY_STRING'}) {
		#
		# Read key pairs from the get
		#
		splitPairs($ENV{'QUERY_STRING'});
		$pristine = $FORM{'pristine'};
		$query    = $FORM{'query'};
		$url      = $FORM{'url'};
	}
} else {
	#
	# Read the argument list from the command line
	#
	$pristine = $ARGV[0];
	$query    = $ARGV[1];
	$url      = $ARGV[2];
}

@words = split(/:/, unarmorURL($query) );
$url   = unarmorURL($url);

#
# Make the url into something useful
#
my $ferret   = openIndex();
my $localurl = PostProcessURL($ferret, $url);

#
# Load the url into memory
#
my $head;
my $data = eval { Ferret::LoadURL($localurl, \$head, 1) };

if ($@) {
	loadError( \$data,
		qq{<h2>The following error occurred while loading "$url":</h2>\n}.
		qq{<h2>$@</h2>} );
	return;
}

#
# Make changes to the data if it's HTML text
#
if ($head =~ m|Content-type:\s+text/html|i) {
	#
	# Add <base> tag and clean things up a bit
	#
	my $base = qq{<base href="$localurl">};
	if ($data =~ m|<head.*?</head|is) {
		$data =~ s|(</head.*?>)|$base\n$1|i;
	} elsif ($data =~ m|<title|i) {
		$data =~ s|(<title.*?</title.*?>)|\n<head>\n  $1\n  $base\n</head>\n|is;
	} else {
		$data =~ s|^(\s*(<html.*?>)?)|$2\n<head>\n  <title>$url</title>\n  $base\n</head>\n|is;
	}
}

#
# Format the data into something useful
#
PostProcessURLData($ferret, $url, \$data, \$head, \@words, $pristine);


#
# Print out the data
#
$head =~ s/^.*(Content-type:\s+\S+).*$/$1/si;
if ($1) {
	print "$head\n\n$data";
} else {
	loadError( \$data,
		qq{<h2>No valid "Content-type" was returned for the following results:</h2>} );
}
