#!/usr/local/bin/perl
#
# Quote Search Utility
# (c)1996 Mark Rigby-Jones for mrjsw
# mark.rigby-jones@keble.oxford.ac.uk
# http://users.ox.ac.uk/~kebl0206/
#

$query = $ENV{QUERY_STRING};
@query = split('=', $query);
$query = $query[1];
$query =~ s/\+/ /g;
$query =~ s/%(..)/pack("c",hex($1))/ge;

print(<<BACK_TO_PERL);
Content-type: text/html

<html>
<head>
<title>Babylon 5 Quote Search</title>
</head>
<body bgcolor=#300040 text=#CFFFBF link=#EEAA77 vlink=#FF7711 alink=#FFFF00>
<p align=center><a href="/~kebl0206/babylon5/quotes/"><img src="/~kebl0206/babylon5/babsoc.gif" width=320 height=121 border=0 alt="<--"></a></p>
<h1 align=center>Babylon 5 Quote Search</h1>
<hr size=4>
BACK_TO_PERL

$found = 0;

$query =~ tr/[A-Z]\n'"`;:.,*/[a-z]         /s;

open(CNF, "out/quote.cnf");
chop($files = <CNF>);

for ($i = 0; $i <= $files; $i++) {
    chop($file = <CNF>);
    open(FILE, $file);
    chop($quotes = <FILE>);
    
    for ($j = 0 ; $j <= $quotes; $j++) {
	$quote = '';
	for ($k = 0; ($line = <FILE>) && ($line !~ /%%/); $k++) {
	    $quote .= $line;
	}

        $check = $quote;
        $check =~ tr/[A-Z] \n'"`;:.,*/[a-z]          /s;

	if ($check =~ /$query/) {
	    $found++;
	    print("<pre>\n$quote</pre>\n<hr>\n");
	}
    }
}

if ($found == 0) {
    print("<h2>Sorry, no matching quotes were found.</h2>\n");
} elsif ($found ==1) {
    print("<h2>Just one matching quote was found.</h2>\n");
} else {
    print("<h2>$found matching quotes were found.</h2>\n");
}

open(COUNT, "out/count.search");
$count = <COUNT>;
close(COUNT);
$count++;
open(COUNT, "> count.search");
print(COUNT $count);
close(COUNT);

print(<<BACK_TO_PERL);
<form method=get action="/cgi-bin/safeperl/kebl0206/search">
<input name="for" size=72><br><input type=submit value="Search the quote database for this string"> <input type=reset value="Clear your input">
</form>
<hr size=4>
<address>
<p align=center>All original material &copy;<a href="mailto:mark.rigby-jones\@keble.oxford.ac.uk">The Oxford Babylon 5 Society</a><br>
Babylon 5 and related material &copy;Warner Brothers<br>
$count searches carried out since 3rd November 1995</p>
</address>
</body>
</html>
BACK_TO_PERL
