#!/usr/bin/perl
# Note: I detest, absolutely _hate_ safeperl.  I mean, what?  I can't even
# use these two:
#use warnings;
#use strict;
#
# You can see the results of this script here:
# http://users.ox.ac.uk/cgi-bin/safeperl/worc2070/fortune.cgi
# You can grab the fortune file from:
# http://users.ox.ac.uk/~worc2070/fortune/firefly-quotes

print "Content-type: text/plain\n\n" if $ENV{HTTP_HOST};
my $file;
if ( defined( $ENV{HTTP_HOST} ) ) {
	$file = "../public_html/fortune/firefly-quotes";
} else {
	$file = "/u3/d/worc2070/public_html/fortune/firefly-quotes";
}


open(F, $file) || print "Ack: couldn't open fortune file '$file' - don't expect the rest to work!";

srand(time);

my $number = 0;
my @fortune;

while ( <F>) {
	chomp;
	next if m/^$/;
	if ( m/^%$/ ) {
		$number++ if $fortune[$number];
	} else {
		$fortune[$number] .= $_ . "\n";
	}
}

my $thisfortune = $fortune[ (rand(1000)*time)%($number + 1) ];
print "$thisfortune";
# http://users.ox.ac.uk/cgi-bin/safeperl/worc2070/fortune.cgi
