#!/usr/bin/perl
$path="texte/";

@fnames=("arzt","berufe","blond","bund","computer","ddr","eriwan",
  "fies","geschichte","juden","kellner","kinder","kirche","mannfrau",
  "manta","musiker","schotten","ossi","ostfriesen","polizei","kram",
  "sport","studenten","tiere","wanderer","zeit","sonstiges");

%Names=("arzt","Arzt","berufe","Berufe","blond","Blondinen","bund",
  "Bund","computer","Computer","ddr","DDR","eriwan","Sender Eriwan",
  "fies","Fies!","geschichte","Geschichte","juden","Jüdische Witze",
  "kellner","Kellner","kinder","Kinder","kirche","Kirche","mannfrau",
  "Männer und Frauen","manta","Manta","musiker","Musiker","schotten",
  "Nationalitäten","ossi","ossis und Wessis","ostfriesen","Ostfriesen",
  "piloten","Piloten","polizei","Polizisten","kram","Schweinskram",
  "sport","Sportler","studenten","Profs und Studenten","tiere","Tiere",
  "wanderer","Wanderer","zeit","Zeitgeschehen","sonstiges","Sonstiges");

$i=rand(@fnames);
@file=($path,$fnames[$i],".txt");
$random_file = join("",@file);
$delimiter = "----------\n";
$del2 = "<>";
open(FILE,"$random_file") || &error('open->random_file',$random_file);
while(<FILE>) {push(@FILE,$_);}
close(FILE);
$phrasen = join('',@FILE);
@phrases = split(/$delimiter/,$phrasen);
$phrase = rand(@phrases-1);
@zwitz = split(/$del2/,$phrases[$phrase]);
print "Content-type: text/html\n\n";
print "<html><head>\n<title>Witz per Zufall V1.01</title>\n";
print "<meta http-equiv=\"expires\" content=\"0\">\n</head>","\n";
print "<body bgcolor=\"#ffffff\" background=\"../hgg.gif\">";
print "<center><br>";

print "<div style=\"margin-top:10px; border-width:2pt; ";
print "border-style:outset; border-color:#a0a0a0; width:90%;\">";

print "<table width=100% border=0 cellpadding=3 cellspacing=0>\n";
print "<tr bgcolor=\"#e0e0e0\"><td colspan=\"3\">\n";
print "<center>\n<font size=4 color=\"#009000\"><b>Der Zufallswitz";
print "</b></font> - Kategorie: <i>",$Names{$fnames[$i]},"</i>\n";
print "</center>\n</td></tr>\n";
print "<tr><td colspan=\"3\" cellpadding=\"5\">\n",$zwitz[3],"\n</td>";
print "</tr>\n<tr><td width=40% bgcolor=\"#e0e0e0\">";
print "<center>\n<a href=\"wpz.pl\" target=\"base\">\n";
print "Den n\&auml\;chsten Witz bitte!</a>\n";
print "</center>\n</td>\n<td width=15% bgcolor=\"#e0e0e0\">&nbsp;</td>";
print "<td bgcolor=\"#e0e0e0\" align=\"right\"><font size=\"2\">Script ";
print "by A. Bienert ";
print "& R. B&ouml;defeld";
print "</font>\n</td></tr></table>\n</div>\n";

print "<br><br>\n\n";

print "</center>\n</body></html>";
exit;

sub error {
    ($error,$file) = @_;
    print <<"END_ERROR";
Content-type: text/html

<html>
 <head>
  <title>ERROR: Random File Unopenable</title>
 </head>
 <body bgcolor=#FFFFFF text=#000000>
  <center>
   <h1>ERROR: Random File Unopenable</h1>
  </center>

The random file, as specified in the \$random_file perl variable was
unopenable.<p>
END_ERROR

    if (-e $random_file) {
        print "The file was found on your system, so make sure that it is\n";
        print "readable by the web server.  This means you will need to\n";
        print "execute the following command:<pre>\n";
        print "    chmod 744 $random_file\n";
        print "</pre>\n";
    }
    else {
        print "The file was not found on your file system.  This means that\n";
        print "it has either not been created or the path you have specified\n";
        print "in \$trrandom_file is incorrect.\n";
    }
    exit;
}
