#!/usr/local/bin/perl5 -w
#       Process Form to Join FFE
#
require "cgi-lib.pl";
#
$basedir ='/npac/server/foxsden00/sroot/droot/psf/FFE/';
$dbfile = $basedir.'members.htm';
$dbfilelock = $basedir.'lock/members.htm';
$mailprog = '/usr/lib/sendmail';
#
$statusofread =  &ReadParse(*formdata);
#
#	Set up Parameter Defaults
$name ='';
$birthdate='';
$yoururl='';
$email='';
$hptitle='';
#
$jobselected{'Something Else'}=0;
$jobselected{'HTML Helper'}=0;
$jobselected{'Advertiser'}=0;
$jobselected{'Newsletter Contributor'}=0;
$jobselected{'Thinker'}=0;
$jobselected{'Mentor'}=0;
$jobselected{'Normal'}=1;
#
unless( $statusofread )	{
	&therewaserror("This is a Server Error -- send email Please");
	exit;
}
#
#	Process Input Data
$name = $formdata{'name'};
$birthdate = $formdata{'birthdate'};
$yoururl = $formdata{'url'};
$email =$formdata{'email'};
$hptitle = $formdata{'title'};
$js = $formdata{'job'};
if( defined($js) && exists($jobselected{$js}) )	{
	$NameofJob = $js;
	foreach	$jobkeys (keys(%jobselected))     {
		if( $js eq $jobkeys )	{
			$jobselected{$jobkeys} =1;
		}
		else	{
			$jobselected{$jobkeys} = 0;
		}
	}
}
else	{
	$NameofJob = 'Something Else';
}
#
#	Check for Missing Information
&therewaserror("Please Give us your name")	unless($name);
&therewaserror("Please Give us your birthdate")	unless($birthdate);
#
# Loop until we can get a sucessful "lock" on the database
$count = 0;
while (!&LockDatabase($dbfilelock)) { # Sleep for a second
	sleep(1);   
	++$count;
	&therewaserror("Server out to lunch -- please email -- lock stuck") if($count > 20);
}
#
open(TABLEFILE,"<$dbfile") || &therewaserror("Server Error Cant Open Member File for Read -- please email");
@ffemembers = <TABLEFILE> ;
close(TABLEFILE);
open(TABLEFILE,">$dbfile") || &therewaserror("Server Error Cant Open Member File for Rewrite -- please email");
#
foreach $line (@ffemembers)	{
#	Insert Line
	if( $line =~ m%^</TABLE>% )	{
		print TABLEFILE <<EOF;
<TR><TD WIDTH=121><CENTER><FONT SIZE=2 COLOR=#FFFF00 FACE="Arial" >$name</FONT></CENTER>
</TD><TD WIDTH=48><CENTER><FONT SIZE=2 COLOR=#FFFF00 FACE="Arial" >$birthdate</FONT></CENTER>
EOF
                if(defined($email) && $email)	{
			print TABLEFILE <<EOF;
</TD><TD WIDTH=221><CENTER><A HREF="mailto:$email" >$email</A></CENTER>
EOF
		}
		else	{
			print TABLEFILE <<EOF;
</TD><TD WIDTH=216><CENTER>none</CENTER>
EOF
		}
		if(defined($yoururl) && $yoururl)	{
			$hptitle ='No Title Given' unless($hptitle);
			print TABLEFILE <<EOF;
</TD><TD WIDTH=216><CENTER><A HREF="$yoururl" >$hptitle</A></CENTER>
EOF
		}
		else	{
			print TABLEFILE <<EOF;
</TD><TD WIDTH=216><CENTER>*none*</CENTER>
EOF
	}
		print TABLEFILE <<EOF;
</TD><TD WIDTH=120><CENTER><FONT SIZE=2 COLOR=#FFFF00 FACE="Arial" >$NameofJob</FONT></CENTER>
</TD></TR>
EOF
	}
	print TABLEFILE $line;
}
close(TABLEFILE);
#
&UnlockDatabase($dbfilelock);
#
#	End Up
print "Location: http://www.foxsden.org/psf/FFE/members.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 FFE Form Processing!</title>
</head>
<h2>Error in Processing FFE Join: $mess</H2>
Please correct or if it's our fault 
<A HREF="mailto:psf\@npac.syr.edu?subject=FFE Join Error $mess" onMouseOver="window.status=' Sadness!.';return true">Mail Complaint</A>
<hr>
<FONT FACE="Arial">When filling in form also tell me whether
you'd like to be a: <BR>
HTML helper-helps outsiders and members wih HTML problems <BR>
Newsletter Contributor-helps create the many sections of the newspaper <BR>
Thinker-thinks up new ideas to spice up the club <BR>
Mentor-helps outsiders and members with problems in life <BR>
Anything else you can think of..<BR>
<A HREF="mailto:psf\@npac.syr.edu?subject=Hi! I want to join FFE. Included is my name,job in club, age, email, and homepage" onMouseOver="window.status=' Join!.';return true">Join By Mail</A>
</FONT></CENTER>
<FORM name="formtojoin" ACTION="http://boss.npac.syr.edu:8080/cgi-bin/pamfox/ffejoin.pl" METHOD="post"
ENCTYPE="application/x-www-form-urlencoded"  >
<P>
Name <INPUT NAME="name" VALUE="$name" MAXLENGTH="40" SIZE=25>
<P>
birthdate
<INPUT NAME="age" VALUE="$birthdate" MAXLENGTH="2" SIZE=2>
<P>
Email <INPUT NAME="email" VALUE="$email" MAXLENGTH="30" SIZE=25>
<P>
Homepage URL: <INPUT NAME="url" VALUE="$yoururl" MAXLENGTH="1024" SIZE=50>
<P>
Homepage title: <INPUT NAME="title" VALUE="$hptitle" MAXLENGTH="30" SIZE=25>
<P>
JOB: <SELECT NAME="job">
EOF
	foreach $jobkeys (keys(%jobselected))	{
		if( $jobselected{$jobkeys} )	{
			print "<OPTION SELECTED>$jobkeys\n";
		}
		else	{
			print "<OPTION >$jobkeys\n";
		}
	}
	print <<EOF;
</SELECT>
<P>
<INPUT TYPE=SUBMIT VALUE="Submit" NAME="submit">   <INPUT TYPE=RESET VALUE="Reset">
</FORM>
<CENTER><A HREF="http://www.foxsden.org/psf/mainpage.htm">Back To The Foxs Den</A></CENTER>
</BODY>
</HTML>
EOF
	exit 1;
}
#
