#!/usr/bin/perl
# little perl bofh cgi excuse jobbie
# copyleft Paul Sladen, released under the terms of the GNU GPL

###############################################################################

# $verbose  controls printing of dates in the headers - may need for proxies
$verbose = 0;

# $filename  where to get the errors from
$filename = "./bofh-excuses";

# $bluecoat  fixes the chances of getting "Alan Marshall"
$bluecoat_enable = 0;
$bluecoat_chance = 8;
$bluecoat_proxy = "10.0.0.2";

###############################################################################

open(INPUT, "<$filename") || die "Can't open excuses file: \'$filename\'\n";

@excuses = <INPUT>;
close INPUT;

# date, internet style
if ( $verbose )
{
    $todays_date = `date -u "+%a, %d %b %Y %T GMT"`;
    chomp $todays_date;
}

#### generate the excuse ####

# fetch a random excuse
$todays_excuse = @excuses[rand $#excuses];

# choose a not *so* random quote
if ( $bluecoat_enable )
{
#    $foo = $ENV{"REMOTE_ADDR"};
#    $bar = $ENV{"REMOTE_ADDR"} eq $bluecoat_proxy;
#    $radi = int(rand $bluecoat_chance);
#    $todays_excuse = "enabled [$bluecoat_proxy] [$foo] [$bar] [$radi]";

    if (( $ENV{"REMOTE_ADDR"} eq $bluecoat_proxy ) &&
	not int(rand $bluecoat_chance))
    {
	$todays_excuse = @excuses[0];
    }
}

#### http headers ####

print <<FOOBAR1;
Content-type: text/html
FOOBAR1

$verbose && print <<FOOBAR2;
Date: $todays_date
Last-Modified: $todays_date
FOOBAR2

#### html headers ####

# the two empty lines following *ARE* neededXS
print <<FOOBAR3;
\n\n
<html>
<head>
<title>error: bofh says</title>
<meta name="author" content="Paul Sladen &lt;paul\@mycampus.com&gt;">
<meta name="version" content="0.0.2">
FOOBAR3

$verbose && print <<FOOBAR4;
<meta name="modified" content="$todays_date">
FOOBAR4

#### html body ####
print <<FOOBAR5;
Er, slight problem....<p>
<blockquote>
<center>
<big><big>
$todays_excuse<p>
</big></big>
</center>
</blockquote>

</body>
</html>
FOOBAR5






