#!/usr/bin/perl
push(@INC,"/www/cgi-bin");

# Survey.cgi takes in arguments from survey.html, saves raw data to
# survey.dat and formatted data to survey.txt

print "Content-type:text/html\n\n";

# The following formats the incoming arguments and puts them into the
# vector $FORM with fields Name, EMail, Gender, Hand, Arm, and Thumb

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $value =~ s/\n/ /g; # replace newlines with spaces
    $FORM{$name} = $value;
}

# If the user did not put in a name, this will give it 'No Name'
if ($FORM{'Name'} eq "") {$FORM{'Name'} = "No Name";}

# If the user did not put in an address, this will give it 'No@Address'
if ($FORM{'EMail'} eq "") {$FORM{'EMail'} = "No Address";}

# If the user didn't fill in one of the four bubbles, it will give
# an error and redirect the program to dienice, which will direct
# the user back to the survey.html page.

if ($FORM{'Gender'} eq "" or $FORM{'Hand'} eq "" or 
    $FORM{'Arm'} eq "" or $FORM{'Thumb'} eq "") {
  dienice("Please complete the survey!");
  }

# This writes the formatted data to survey.txt to be read easier

open(OUTT,">>/home/bjabram/html/archives/fall00/survey/survey.txt") or dienice("Can't open
survey.txt for writing: $!");
flock(OUTT,2); 
seek(OUTT,0,2); 
print OUTT ("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n"); 
print OUTT ("Name   = $FORM{'Name'}\n"); 
print OUTT ("EMail  = $FORM{'EMail'}\n"); 
print OUTT ("Gender = $FORM{'Gender'}\n"); 
print OUTT ("Hand   = $FORM{'Hand'}\n"); 
print OUTT ("Arm    = $FORM{'Arm'}\n"); 
print OUTT ("Thumb  = $FORM{'Thumb'}\n"); 
close(OUTT); 

# This writes the data to survey.dat, which has one line per person
# The survey.txt file had one line per datum.  The data is separated
# by the character 'ê', which is ASCII character code 0234.  (I do
# not believe anyone will enter ê as their name.

open(OUTD,">>/home/bjabram/html/archives/fall00/survey/survey.dat") or dienice("Can't open
survey.dat for writing: $!");
flock(OUTD,2);
seek(OUTD,0,2);
print OUTD ("$FORM{'Name'}ê");
print OUTD ("$FORM{'EMail'}ê");
print OUTD ("$FORM{'Gender'}ê");
print OUTD ("$FORM{'Hand'}ê");
print OUTD ("$FORM{'Arm'}ê");
print OUTD ("$FORM{'Thumb'}\n");
close(OUTD);

# Prints out the success web page and thanks the use
# This part should have a data input to send feedback through email to
# myself or to someone else, I suppose.  On top of this, it should
# respond immediately saying that I received the email, thank the user
# for sending in comments/complaints, and that I will respond if needed.

print <<EndOfHTML;
<html><head><title>Thank you</title></head>
<body>
<font face="Verdana" size="2">
<h2><center>Thank you for your time!</center></h2>
<p><center>Your answers have been recorded. Thank you!</center></p>
<p><center>If you would like to leave a message to the class, please do
so!<br>Any comments or complaints are greatly appreciated.</center></p>
<center><form
action="http://johnny.sas.upenn.edu/~bjabram/cgi-bin/fall00/survey/surveymail.cgi" method="post">
<b>Name:</b><input name="Name" size="30" value="$FORM{'Name'}"><br>
<b>E-Mail:</b><input name="EMail" size="30" value="$FORM{'EMail'}"><br>
<textarea name="Comments" rows=8 cols=60 align="center" wrap="virtual">
</textarea><br>
<input type="submit" value="Submit Comment"> <input type="reset"><br>
</center>
<p><center><font color="Red">NEW!</font> <a
href="http://johnny.sas.upenn.edu/~bjabram/cgi-bin/survey/results.cgi">Current
Results of the Survey!</a></center></p>
<p><center>Thanks again from the <a
href="http://www.math.upenn.edu/~kazdan/210">Math 210 Class</a> at the
<a href="http://www.upenn.edu">University of Pennsylvania</a></center></p>
</font>
</body>
</html>
EndOfHTML

# dienice subroutine that does just that--dies nicely.  Quits the script
# and leaves no files open.  More to happen if someone doesn't fill in
# all the information and I want to tell them to do so!

sub dienice {
my($errmsg) = @_;
print <<EndOfDie;
<html><head><title>Sorry!</title></head>
<body>
<h2>Error</h2>
<p>$errmsg</p>
<a href="http://johnny.sas.upenn.edu/~bjabram/survey/survey.html">Click 
here to return to survey</a>
</body>
</html>
EndOfDie
exit;
}

