#!/usr/local/bin/perl5 -w
#
require "cgi-lib.pl";
#
$today =`date`;
chomp($today);
#
$basedir ='/usr/local/archives/public/html/users/gcf/homepage/FFE/';
$dbfile = $basedir.'madlib.htm';
$mailprog = '/usr/lib/sendmail';
$statusofread = &ReadParse (*formdata);
#
$name='';
$noun1='';
$adverb1='';
$food1='';
$adjective1='';
$plnoun1='';
$plnoun2='';
$adjective2='';
$color1='';
$place1='';
$adjective3='';
$pattern1='';
$animal1='';
$money1='';
#
unless ($statusofread)	{
	&therewaserror ("**ALERT**ALERT**THERE IS A GLITCH**FBI HAVE BEEN ALERTED**");
	exit;
}
#
$name=$formdata{'name'};
$noun1= $formdata{'noun1'};
$adverb1= $formdata{'adverb1'};
$food1= $formdata{'food1'};
$adjective1= $formdata{'adjective1'};
$plnoun1= $formdata{'plnoun1'};
$plnoun2= $formdata{'plnoun2'};
$adjective2= $formdata{'adjective2'};
$color1= $formdata{'color1'};
$place1= $formdata{'place1'};
$adjective3= $formdata{'adjective3'};
$pattern1= $formdata{'pattern1'};
$animal1= $formdata{'animal1'};
$money1=$formdata{'money1'};
#
&therewaserror("Please enter all fields")   unless($name);
&therewaserror("Please enter all fields")	unless($noun1);
&therewaserror("Please enter all fields")	unless($adverb1);
&therewaserror("Please enter all fields")	unless($food1);
&therewaserror("Please enter all fields")	unless($adjective1);
&therewaserror("Please enter all fields")	unless($plnoun1);
&therewaserror("Please enter all fields")	unless($plnoun2);
&therewaserror("Please enter all fields")	unless($adjective2);
&therewaserror("Please enter all fields")	unless($color1);
&therewaserror("Please enter all fields")	unless($place1);
&therewaserror("Please enter all fields")	unless($adjective3);
&therewaserror("Please enter all fields")	unless($pattern1);
&therewaserror("Please enter all fields")	unless($animal1);
&therewaserror("Please enter all fields")	unless($money1);
$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;
This is the mad lib of $name.
My <b>$noun1</b> and I <b>$adverb1</b> skipped to the local <b>$food1</b>
King.<br> We ordered <b>$adjective1</b><b> $plnoun1</b> and
<b>$plnoun2</b>.<br>
My <b>$noun1</b> turned an <b>$adjective2</b> shade of <b>$color1</b>.
<br>My <b>$noun1</b> rushed too the nearest <b>$place1</b> and spewed up
<b>$adjective3</b><b> $plnoun1</b>.<br>
Then we went to the mall.<br> We saw the newest beanie baby, which was a
<b>$pattern1</b> <b>$animal1</b>. <br>My <b>$noun1</b> bought it for
<b>$money1</b>.<br><hr>
EOF
}
	print LIBFILE $line;
}
close(LIBFILE);

#
&UnlockDatabase($dbfile);
#
#	End Up
print "Location: http://www.npac.syr.edu/users/gcf/homepage/FFE/madlib.htm\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 ffejoin.pl
EOF
		close(MAIL);
	}
}
#
sub therewaserror	{

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

<HTML>
<HEAD> 
<TITLE>Error in Mad Lib</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 Error $mess"
onMouseOver="window.status=' Sadness!.';return true">Mail Complaint</A>
<hr>
<CENTER><TT><FONT SIZE=5>PAM'S MAD LIBS</FONT></TT></CENTER>
<P>
<CENTER><TT><FONT SIZE=5>You must enter input in ALL
FIELDS!</FONT></TT></CENTER>
<FORM ACTION="http://boss.npac.syr.edu:8080/cgi-bin/foxmaniac/madlibs.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 a noun </TT><INPUT NAME="noun1" VALUE="$noun1"
MAXLENGTH="30" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter a adverb</TT><INPUT NAME="adverb1"
VALUE="$adverb1" MAXLENGTH="30" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter a food </TT><INPUT NAME="food1" VALUE="$food1"
MAXLENGTH="30" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter an adjective</TT><INPUT NAME="adjective1"
VALUE="$adjective1" MAXLENGTH="30" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter a plural noun </TT><INPUT NAME="plnoun1"
VALUE="$plnoun1" MAXLENGTH="30" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter a plural noun </TT><INPUT NAME="plnoun2"
VALUE="$plnoun2" MAXLENGTH="30" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter an adjective </TT><INPUT NAME="adjective2"
VALUE="$adjective2" MAXLENGTH="30" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter a color </TT><INPUT NAME="color1" VALUE="$color1"
MAXLENGTH="25" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter a place </TT><INPUT NAME="place1" VALUE="$place1"
MAXLENGTH="30" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter a adjective </TT><INPUT NAME="adjective3"
VALUE="$adjective3" MAXLENGTH="30" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter a pattern </TT><INPUT NAME="pattern1"
VALUE="$pattern1" MAXLENGTH="30" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter a animal </TT><INPUT NAME="animal1"
VALUE="$animal1" MAXLENGTH="35" SIZE=25></CENTER>
<P>
<CENTER><TT>Please enter an amount of money</TT><INPUT NAME="money1"
VALUE="$money1" MAXLENGTH="50" SIZE=25></CENTER>
<P>
<CENTER><INPUT TYPE=SUBMIT VALUE="Create the Mad Lib"
NAME="submit"></CENTER>
</FORM>

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

