#!/usr/local/bin/perl5 -w
#
require "cgi-lib.pl";
#
$basedir ='/usr/local/archives/public/html/users/gcf/homepage/FFE/';
$dbfile = $basedir.'madlib2.htm';
$mailprog = '/usr/lib/sendmail';
$statusofread = &ReadParse (*formdata);
#
$name='';
$body1='';
$body2='';
$adject4='';
$adject3='';
$adject1='';
$adverb1='';
$animal1='';
$building1='';
$personf1='';
$amounttime='';
$adject2='';
$noun1='';
$adverb2='';
$adject5='';
$insult2='';
$insult3='';
#
unless ($statusofread)  {
    &therewaserror ("**ALERT**ALERT**THERE IS A GLITCH**FBI HAVE BEEN ALERTED**");
    exit;
}
#
$body1=$formdata{'body1'};
$body2= $formdata{'body2'};
$adject4= $formdata{'adject4'};
$adject3= $formdata{'adject3'};
$adject1= $formdata{'adject1'};
$adverb1= $formdata{'adverb1'};
$animal1= $formdata{'animal1'};
$building1= $formdata{'building1'};
$personf1= $formdata{'personf1'};
$amounttime= $formdata{'amounttime'};
$adject2= $formdata{'adject2'};
$noun1= $formdata{'noun1'};
$secret1= $formdata{'secret1'};
$adverb2=$formdata{'adverb2'};
$adject5=$formdata{'adject5'};
$insult1=$formdata{'insult1'};
$insult2=$formdata{'insult2'};
$insult3=$formdata{'insult3'};
$name=$formdata{'name'};

#**must go over**
$count = 0;
while (!&LockDatabase($dbfile)) { # Sleep for a second
    sleep(1);
++$count;
&therewaserror("Server out to lunch -- please email -- lock stuck")
if($count > 20);
}
#
open(LIBFILE,"<$dbfile") || &therewaserror("Server Error Cant Open Mad File
for Read -- please email");
@madlibs = <LIBFILE> ;
close(LIBFILE);
open(LIBFILE,">$dbfile") || &therewaserror("Server Error Cant Open Mad Libs
File for Rewrite -- please email");
#
foreach $line (@madlibs)    {

#   Insert Line
if( $line =~ m%^<!--LIBS-->% )  {
print LIBFILE <<EOF;
<b>This is the mad lib of $name.</b>
One <font color=red><b>$adject1</b></font> day, I went <font color=red><b>$adverb1</b></font> along to the local<b> <font
color=red>$animal1</b></font> <b><font color=red>$building1</b></font>.<br>
 I saw my freind,<b> <font color=red>$personf1</b></font>. We talked for <b><font color=red>$amounttime</font></b> just like <font
color=red><b>$adject2</b></font> <font color=red><b>$noun1</font></b>.<br>
Then he said, "Wow, your <b><font color=red>$body1</b></font> looks <b><font color=red>$adject3</b></font>!". I said, "Thanks, your <font
color=red><b>$body2</b></font> looks <font color=red><b>$adject4</b></font> too!" <br>
"Oh, and by the way, I <font color=red><b>$secret1</font></b>", he said <font color=red><b>$adverb2</font></b>. I was <font color=red><b>$adject5</b></font>! <br>
I just said, "Well, fine, you <b><font color=red>$insult1</font>, <font color=red>$insult2</font>, <font
color=red>$insult3!!!</font></b>," and stormed out of that place.<br><hr>
EOF
}
    print LIBFILE $line;
}
close(LIBFILE);

#
&UnlockDatabase($dbfile);
#
#   End Up
print "Location: http://www.npac.syr.edu/users/gcf/homepage/FFE/madlib2.htm\n";
print "Cache-control:must-revalidate\n\n";
exit;
#
sub LockDatabase {
# PURPOSE: Attempt to "lock" the database by creating a lock file.
# INPUTS:  Database file name
# OUTPUTS: A boolean telling if the lock was successful

    my ($dbfile) = @_;

    if (-e "$dbfile.lock") {  # DB is already locked
        return 0;
    }
    else {
        system("touch $dbfile.lock");  # Create the lock file
        return 1;
    }
}

sub UnlockDatabase {
# PURPOSE: Attempt to "unlock" the database by deleting the lock file.
# INPUTS:  Database file name
# OUTPUTS: None (but should probably check that the unlock was successful)

    my ($dbfile) = @_;
    unless( (unlink("$dbfile.lock")) )  {
        open (MAIL, "|$mailprog gcf") || (&therewaserror("Server Error Can't Mail -- Please email us")) ;
        print MAIL <<EOF;
From:gcf
To:gcf
Subject:Error in unlink
------------------------------------------------------

Please check madlib2.pl
EOF
        close(MAIL);
    }
}
#
sub therewaserror   {

    my($mess) = @_;
#
    print <<EOF;
Content-Type: text/html

<HTML>
<HEAD>
<TITLE>Error in Mad Lib #2</TITLE>
</HEAD>
<BODY>
<P>
<h2>Error in Processing Mad LIbs: $mess</H2>
Please correct or if it's our fault
<A HREF="mailto:psf\@npac.syr.edu?subject=Mad LIbs #2 Error $mess"
onMouseOver="window.status=' Sadness!.';return true">Mail Complaint</A>
<hr>
<font face="Courier"><h3><CENTER>Pams Mad Libs #2 </h3>
	<CENTER><TT><FONT SIZE=5>You must enter input in ALL
FIELDS!If you like this, please email me <a
href="mailto:psf\@npac.syr.edu">here. </a> Or please visit my homepage!<a
href="http://www.npac.syr.edu/users/gcf/homepage/pamhomepage.html">heer</a></FONT></TT></CENTER>
<FORM ACTION="http://boss.npac.syr.edu:8080/cgi-bin/foxmaniac/madlib2.pl"
METHOD="POST" ENCTYPE="application/x-www-form-urlencoded">
<P>
<CENTER><TT>Please enter your name </TT><INPUT NAME="name" VALUE="$name"
MAXLENGTH="90" SIZE=25></CENTER>
<p>
<CENTER><TT>Please enter an insult </TT><INPUT NAME="insult3" VALUE="$insult3"
MAXLENGTH="50" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter an insult</TT><INPUT NAME="insult2" VALUE="$insult2"
MAXLENGTH="50" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter an insult </TT><INPUT NAME="insult1" VALUE="$insult1"
MAXLENGTH="50" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter an adverb</TT><INPUT NAME="adverb2" VALUE="$adverb2"
MAXLENGTH="30" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter an adjective </TT><INPUT NAME="adject5" VALUE="$adject5"
MAXLENGTH="30" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter a deep, dark secret someone could have </TT><INPUT NAME="secret1" VALUE="$secret1"
MAXLENGTH="100" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter a plural noun</TT><INPUT NAME="noun1"
VALUE="$noun1" MAXLENGTH="30" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter an adjective </TT><INPUT NAME="adject2" VALUE="$adject2"
MAXLENGTH="25" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter an amount of time</TT><INPUT NAME="amounttime" VALUE="$amounttime"
MAXLENGTH="30" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter a in/famous person </TT><INPUT NAME="personf1" VALUE="$personf1"
MAXLENGTH="30" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter a type of building </TT><INPUT NAME="building1" VALUE="$building1"
MAXLENGTH="35" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter an animal</TT><INPUT NAME="animal1"
VALUE="$animal1" MAXLENGTH="50" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter an adverb</TT><INPUT NAME="adverb1"
VALUE="$adverb1" MAXLENGTH="50" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter an adjective</TT><INPUT NAME="adject1"
VALUE="$adject1" MAXLENGTH="50" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter a part of the body</TT><INPUT NAME="body1"
VALUE="$body1" MAXLENGTH="50" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter an adjective</TT><INPUT NAME="adject3"
VALUE="$adject3" MAXLENGTH="50" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter a part of the body</TT><INPUT NAME="body2"
VALUE="$body2" MAXLENGTH="50" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter an adjective</TT><INPUT NAME="adject4"
VALUE="$adject4" MAXLENGTH="50" SIZE=25></CENTER>
<P>
<CENTER><INPUT TYPE=SUBMIT VALUE="Create the Mad Lib"
NAME="submit"></CENTER>
</FORM>

</BODY>
</HTML>
EOF
    exit 1;
}

