#!/usr/local/bin/perl
#
# Copyright (C) 1996 Jozsef Kadlecsik, kadlec@blackhole.kfki.hu
#
######################################################################

$table = $ENV{'QUERY_STRING'};

undef %ENV;	# We need nothing from ENV.

$dir = '/export/home/szhk/kadlec/public_html';
$welcome = "$dir/Welcome.html";	 
$udvozlet = "$dir/Udvozlet.html";

%tables = (	
'art', 460,
'computers', 1019,
'definitions', 1105,
'drugs',208,
'education',203,
'ethnic',163,
'food',198,
'fortunes',433,
'goedel',54,
'humorists',196,
'kids',150,
'law',202,
'linuxcookie',105,
'literature',256,
'love',151,
'magic',29,
'medicine',72,
'men_women',581,
'miscellaneous',644,
'news',53,
'people',1231,
'pets',51,
'platitudes',496,
'politics',691,
'riddles',135,
'science',622,
'songs_poems',719,
'sports',147,
'startrek',225,
'wisdom',402,
'work',630,
);

@tables = sort keys(%tables);
$total = 0;
foreach (@tables) { $total += $tables{$_}; };

if ($table) {
    foreach (split('&',$table)) {
	($name,$value) = split('=',$_);
	if ($name eq 'cookie') {
	    $table = $value;
	    $table = '' unless grep(/^$table$/,@tables);
	    $select = $table;
	    last;
	}
    }
}

if ($0 =~ /fortune\.cgi/) {		# Fortune page 
    &show;
} elsif ($0 =~ /fortune\.pl/) {		# Maintenance
    &maintenance;
}

sub maintenance {
    srand(time ^ $$);
    $cookie = &mkhtml(&cookie);

    &update($welcome) if -f $welcome;
    &update($udvozlet) if -f $udvozlet;
}

sub update {
    my($file) = @_;
    my($fromnow);

    open(IN,$file);
    open(OUT,">$file.new");
    while (<IN>) {
	if ($fromnow) {
	    if (/^<\!\-\- Fortune/) {
		print OUT $_;
		$fromnow = 0;
	    } else {
		next;
	    }
	} elsif (/^<\!\-\- Fortune/) {
	    print OUT $_;
	    print OUT $cookie;
	    $fromnow = 1;
	} else {
	    print OUT $_;
	}
    }
    close(OUT);
    close(IN);
    rename("$file.new",$file);
}

sub show {
    my($fromnow);
    srand(time ^ $$);
    $cookie = &mkhtml(&cookie);

    open(IN,$fortune);
    print <<'TXT';
Content-type: text/html

<html>
<head>
<title>Fortune</title>
</head>

<body bgcolor="#EEFFFA" link="#CC0000" alink="#FF3300" vlink="#0000DD">

<img vspace=18 src="http://www.kfki.hu/~kadlec/img/clear.gif"><br>

<table border=0 width=560>
<tr>
<td width=90><br></td>
<td width=470><font size=6><b>The Fortune Message Generator</b></font><br>
</td></tr></table>

<img vspace=12 src="http://www.kfki.hu/~kadlec/img/clear.gif"><br>

<table border=0 width=560>
<tr>
<td width=40><br></td>
<td width=520 valign=center><img width=520 height=1 src="http://www.kfki.hu/~kadlec/img/bdot.gif"></td></tr>
<td width=40><br></td>
<td width=520>

<img vspace=4 src="http://www.kfki.hu/~kadlec/img/clear.gif">
TXT

    print $cookie;

    print <<'TXT';
<img vspace=4 src="http://www.kfki.hu/~kadlec/img/clear.gif">
</td></tr>
<td width=40><br></td>
<td width=520 valign=center><img width=520 height=1 src="http://www.kfki.hu/~kadlec/img/bdot.gif"></td></tr>
</table>

<img vspace=12 src="http://www.kfki.hu/~kadlec/img/clear.gif"><br>

<table border=0 width=560>
<tr>
<td width=40><br></td>
<td width=520>
<form method=get action="http://www.kfki.hu/~kadlec/cgi/fortune.cgi">
<table>
<tr><td>Which fortune collection do you want to use?<br>
(number of cookies in parentheses)</td><td><select name="cookie">
TXT
    $selected = $select ? '' : ' selected';
    print "<option value=\"\"$selected> all ($total)\n";
    foreach (@tables) {
	# ($value = $_) =~ s/^fortune_//;
	$value = $_;
	($val = $value) =~ s/_/-/g;
	# $val = 'miscellaneous' if $val eq 'misc';
	$selected = $_ eq $select ? ' selected' : '';
        print "<option value=\"$value\"$selected> $val ($tables{$_})\n";
    }
    print <<'TXT';
</select>
</td></tr>
<tr><td></td><td><input type="submit" value="Go!"></td></tr>
</table>
</form>
</td></tr>
<td width=40><br></td>
<td width=520>
&copy; The Fortune Message Generator of J&oacute;zsef Kadlecsik :-)</td></tr>
</table>

<img vspace=14 src="http://www.kfki.hu/~kadlec/img/clear.gif"><br>

<table border=0 width=560>
<tr>
<td width=40><br></td>
<td width=520>
<a href="http://www.kfki.hu/~kadlec/Welcome.html">J&oacute;zsef Kadlecsik</a></td></tr>
<td width=40><br></td>
<td width=520>
<a href="mailto:kadlec@sunserv.kfki.hu">kadlec@sunserv.kfki.hu</a>
</td></tr></table>
<img src="http://www.kfki.hu/~kadlec/cgi/counter.cgi">
</body>
</html>
TXT
}

sub cookie {
    my($num);
    srand(time ^ $$);

    if (! $table) {
	my(@tbl) = @tables;
	$num = &randnum($total);
	while (! $table) {
	    $_ = shift(@tbl);
	    $num -= $tables{$_};
	    $table = $_ if $num <= 0;
	}
    }

    use Mysql;
    $db = Mysql->Connect("sunserv.kfki.hu","fortune","kadlec","");
    # $db->SelectDB("fortune");

    $num = ($db->Query(&select(-1))->FetchRow())[1];

    ($db->Query(&select(&randnum($num)))->FetchRow())[1];
 
}

sub randnum {
    my($in) = @_;
    my($num) = int(rand($in + 1));

    $num > $in ? $num - 1 : $num;
}

sub select {
    "select * from $table where nr=$_[0]";
}

sub mkhtml {
    my($fortune) = @_;
    my($vsp1,$vsp2,$bvsp1,$bvsp2,$hsp1,$hsp2,$brhsp1,$brhsp2);
    my($hspbr1,$hspbr2);

    $hsp1 = '<img hspace=8 src="http://www.kfki.hu/~kadlec/img/clear.gif">';
    $hsp2 = '<img hspace=32 src="http://www.kfki.hu/~kadlec/img/clear.gif">';
    $brhsp1 = '<br>' . $hsp1;
    $brhsp2 = '<br>' . $hsp2;

    $fortune =~ s/BRHSP1/$brhsp1/g;
    $fortune =~ s/BRHSP2/$brhsp2/g;
    $fortune =~ s/HSP1/$hsp1/g;
    $fortune =~ s/HSP2/$hsp2/g;
    "<table>\n" . $fortune . "</table>\n";
}

