#!/usr/local/bin/perl  


    print "Content-type: text/html\n\n";
# use diagnostics;

my($fortune, $nextline, $readin, $which,
   $fortunecount, $fortuneline, $tester);
my @fortuneoffset;
#fortune program.  

&chugCGI(*input);


$which = $input{"direct"};


$fortune = "blendisms";
$readin="";
open (DATAFILE,$fortune.".rnd") || die "couldn't open $fortune.rnd file!";
if (defined DATAFILE){
while(defined($nextline=<DATAFILE>)) {    #just in case a packed line 
    $readin .= $nextline;         #holds onto a \n....
}
@fortuneoffset = unpack "L*",$readin; #file is a pack'd series of offsets
close DATFILE;
}
$fortunecount = @fortuneoffset;
#print "$fortunecount\n";

open(FILE,$fortune) ||  die "couldn't open $fortune.rnd file!";



srand;
if($which eq ""){
$which = int(rand $fortunecount); # between 0 and number of fortunes - 1
} else {
    $which--;
}
$displaywhich = $which+1;


seek (FILE, $fortuneoffset[$which], 0) ;
my $fullfortune = "";
$fortuneline = <FILE>;		
#print "START:$fortuneline\n";
$tester = substr ($fortuneline, 0, 2);
while($tester ne "%\n" && defined($fortuneline)) {	# read til EOF or next fortune

    $fullfortune .= $fortuneline;
    $fortuneline = <FILE>;
    if(defined($fortuneline)){
	$tester = substr ($fortuneline, 0, 2);
    }
}
close FILE;

$prev = $displaywhich  - 1;
$next = $displaywhich + 1;

print<<__EOQ__;

<BODY  BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#000000" VLINK="#000000" ALINK="#000000"> 
<TABLE BORDER=0 WIDTH=75%>
<TR>
<TD>&nbsp;<BR></TD>
<TD>
<TABLE BORDER=0>
    <TR>
<TD>
<FONT SIZE=+1>
<B><PRE>
$fullfortune
</PRE></B>
</TD></TR>
</TABLE>
</TD>
<TD>
&nbsp;<BR>
&nbsp;<BR>
&nbsp;<BR>
&nbsp;<BR>
&nbsp;<BR>
&nbsp;<BR>
&nbsp;<BR>
&nbsp;<BR>
&nbsp;<BR>
&nbsp;<BR>
&nbsp;<BR>
&nbsp;<BR>
</TD>

</TR>
<TR><TD COLSPAN=2>
<TABLE CELLPADDING=0 CELLSPACING=1 BORDER=0>
<TR>

<TD ALIGN="CENTER" WIDTH="11%">

</TD>

<TD BGCOLOR="#999999" ALIGN="CENTER" WIDTH="11%">
<A HREF="index.cgi"><B><FONT SIZE="10">
?</FONT><BR></B></A>

</TD>

<TD BGCOLOR="#999999" ALIGN="CENTER" WIDTH="11%">
<B><FONT SIZE="10">#</FONT><BR></B>
</TD>

<TD BGCOLOR="#999999" ALIGN="CENTER" WIDTH="11%">
<A HREF="about.html">
<B><FONT SIZE="10">A</FONT><BR></B></A>
</TD>



<TD BGCOLOR="#999999" ALIGN="CENTER" WIDTH="11%">
<A HREF="index.cgi?direct=$prev"><B><FONT SIZE="10">&lt;</FONT><BR></B></A>
</TD>

<TD BGCOLOR="#999999" ALIGN="CENTER" WIDTH="11%">
<A HREF="/"><B><FONT SIZE="10">^</FONT><BR></B></A>
</TD>


<TD BGCOLOR="#999999" ALIGN="CENTER" WIDTH="11%">
<A HREF="index.cgi?direct=$next"><B><FONT SIZE="10">&gt;</FONT><BR></B></A>
</TD>



</TR>
<TR>
<TD></TD>
<TD ALIGN="CENTER"><FONT SIZE="1">Rand</A></TD>
<FORM>
<TD ALIGN="CENTER"><FONT SIZE="1"><INPUT TYPE="TEXT" SIZE=3 NAME="direct" VALUE="$displaywhich">/$fortunecount</A></TD>

</FORM>
<TD ALIGN="CENTER"><FONT SIZE="1">About</A></TD>   
<TD ALIGN="CENTER"><FONT SIZE="1">Prev</A></TD>
<TD ALIGN="CENTER"><FONT SIZE="1">Home</A></TD>
<TD ALIGN="CENTER"><FONT SIZE="1">Next</A></TD>
</TR>
<TR><TD></TD><TD COLSPAN=6 ALIGN="CENTER"><pre>b l e n d - o - m a t i c</pre>
</TD></TR>

</TABLE>
</TD></TR></TABLE>
__EOQ__




sub chugCGI {
  local (*in) = @_ if @_;
  local ($i, $key, $val);
  # Read in text
  if ($ENV{'REQUEST_METHOD'} eq "GET") {
    $in = $ENV{'QUERY_STRING'};
  } elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
    read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
  }
  @in = split(/&/,$in);
  foreach $i (0 .. $#in) {
    # Convert plus's to spaces
    $in[$i] =~ s/\+/ /g;
    # Split into key and value.  
    ($key, $val) = split(/=/,$in[$i],2); # splits on the first =.
    # Convert %XX from hex numbers to alphanumeric
    $key =~ s/%(..)/pack("c",hex($1))/ge;
    $val =~ s/%(..)/pack("c",hex($1))/ge;
    # Associate key and value
    $in{$key} .= "\0" if (defined($in{$key})); # \0 is the multiple separator
    $in{$key} .= $val;
  }
  return length($in); 
}
