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

$query = $ENV{QUERY_STRING};
@season = (0,0,0,0);

if ($query =~ /0/) {
    $season[0] = 1;
}

if ($query =~ /1/) {
    $season[1] = 1;
}

if ($query =~ /2/) {
    $season[2] = 1;
}

if ($query =~ /3/) {
    $season[3] = 1;
}

if (($season[0] == 0) && ($season[1] == 0) && ($season[2] == 0) && ($season[3] == 0)) {
    @season = (1,1,1,1);
}

$total = (1 * $season[0]) + (22 * ($season[1] + $season[2] + $season[3]));

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

<html>
<head>
<title>Babylon 5 Random Quote</title>
</head>
<body bgcolor=#300040 text=#CFFFBF link=#EEAA77 link=#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>Random Babylon 5 Quote</h1>
<hr size=4>
<center>
<pre>
BACK_TO_PERL

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

if ($season[0] == 0) {
    $fileno++;
}

if (($season[1] == 0) && ($fileno > 0)) {
    $fileno += 22;
}

if (($season[2] == 0) && ($fileno > 22)) {
    $fileno += 22;
}

for ($i = 0; $i <= $fileno; $i++) {
    chop($file = <CNF>);
}

open(FILE, $file);
chop($quotes = <FILE>);
$quoteno = (($time - $fileno) / $total) % $quotes;

for ($i = 0; $i <= $quoteno; $i++) {
    while (<FILE> !~ /%%/) {
    }
}

for ($i = 0; ($line = <FILE>) && ($line !~ /%%/); $i++) {
	$quote[$i] = ($line);
}

chop(@quote);
$maxlength = 0;
foreach $line (@quote) {
    if (length($line) > $maxlength) {
	$maxlength = length($line);
    }
}

foreach $line (@quote) {
    for ($i = length($line); $i < $maxlength; $i++) {
	$line .= " ";
    }
    print("$line\n");
}

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

print(<<BACK_TO_PERL);
</pre>
</center>
<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 quotes randomly served since 3rd November 1995</p>
</body>
</html>
BACK_TO_PERL
