#!/usr/npac/bin/perl -w
#       Process Form to Add to Specified Page
#
require "cgi-lib.pl";
#
$basedir ='/npac/server/foxsden00/sroot/droot/psf/FFE/';
$mailprog = '/usr/lib/sendmail';
#
$today =`date`;
chomp($today);
#
$statusofread =  &ReadParse(*formdata);
#
#	Set up Parameter Defaults
$name ='';
$email='';
$textinput='';
$formtype='';
$filename='';
#
#
unless( $statusofread )	{
	&therewaserror("This is a Server Error -- send email Please");
	exit;
}
#
#	Process Input Data
$name = $formdata{'name'};
$email =$formdata{'email'};
$textinput =$formdata{'textinput'};
$formtype =$formdata{'formtype'};
#
#	Check for Missing Information
&therewaserror("Please Give us your name")	unless($name);
&therewaserror("Please fill in text input box")   unless($textinput);
#
#	Define File
$filename ="";
$filename ='poANDstor.htm'	if $formtype eq 'poANDstor';
$filename ='penpals.htm'	if $formtype eq 'penpals';
$filename ='embarrassing.htm'   if $formtype eq 'embarrassing';
&therewaserror("FBI Alerted-file has not been recognized-*alert*")   unless($filename);
$dbfile = $basedir . $filename;
$dbfilelock = $basedir .'lock/' . $filename;
#
#	Edit $textinput
if($textinput)	{
	$textinput =~ s%\n%<br>\n%sg;
	$textinput =~ s%\015%%sg;
	$textinput =~ s%\013%%sg;
}
#
# 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(DATAFILE,"<$dbfile") || &therewaserror("Server Error Cant Open Data File for Read -- please email");
@data = <DATAFILE> ;
close(DATAFILE);
open(DATAFILE,">$dbfile") || &therewaserror("Server Error Cant Open Data File for Rewrite -- please email");
# 
foreach $line (@data)	{
 
#	Insert Line
	if( $line =~ m%^<!--NUM(\d*)-->$% )	{
		$num=1+$1;
		print DATAFILE "<!--NUM$num-->\n";
		next;
	}
	if( $line =~ m%^<!--INDEX-->$% )	{
		if($email)	{
			print DATAFILE "$num:<a href='#data$num' >${name}\'s Story</a> Email:<A HREF='mailto:$email' >$email</A> $today<br>\n"; 
		}
		else	{
			print DATAFILE "$num:<a href='#data$num' >${name}\'s Story</a> No Email provided $today<br>\n";
		}
	}
	if( $line =~ m%^<!--CONTRIBUTIONS-->$% )	{
		if($email)	{
			print DATAFILE "<a name='data$num'>$num:</a>Contribution of $name <A HREF='mailto:$email' >$email</A> $today<br>\n";
		}
		else	{
			print DATAFILE "<a name='data$num'>$num:</a>Contribution of $name No Email provided $today<br>\n";
		}
		print DATAFILE "$textinput\n<hr>\n";
	}
	print DATAFILE $line;
}
close(DATAFILE);
#
&UnlockDatabase($dbfilelock);
#
#	End Up
print "Location: http://www.foxsden.org/psf/FFE/$filename\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 addtopage.pl for $filename
EOF
		close(MAIL);
	}
}
#
sub therewaserror	{
	my($mess) = @_;
#
	print <<EOF;
Content-Type: text/html

<html>
<head>
<title>Error in $filename Form Processing!</title>
</head>
<h2>Error in Processing $filename Update: $mess</H2>
Please correct or if it's our fault 
<A HREF="mailto:psf\@npac.syr.edu?subject=$filename Update Error $mess" onMouseOver="window.status=' Sadness!.';return true">Mail Complaint</A>
<hr>
<FORM ACTION="http://boss.npac.syr.edu:8080/cgi-bin/pamfox/addtopage.pl" METHOD="POST" ENCTYPE="application/x-www-form-urlencoded">
<P>
Name <INPUT NAME="name" VALUE="$name" MAXLENGTH="35" SIZE=25>
<P>
Email <INPUT NAME="email" VALUE="$email" MAXLENGTH="50" SIZE=25><INPUT NAME="formtype" VALUE="$formtype" TYPE=HIDDEN>
<P>
Put your poem or story here:
<P>
<TEXTAREA NAME="textinput" ROWS=5 COLS=50>
$textinput
</TEXTAREA>
<P>
<INPUT TYPE=SUBMIT VALUE="Submit" NAME="submit"> <INPUT TYPE=RESET VALUE="Reset">
</FORM>
</BODY>
</HTML>
EOF
	exit 1;
}
#
