#!/usr/bin/perl

# brian v0.1
# a question/answer/guestbook fish
# by chris heschong (chris@xtn.net) 09/08/96

$datafile = "/home/chris/html/brian.dat";
$mailprog = "/usr/lib/sendmail";
$mailto = "anderson\@wiw.org";

$header = "
<html><head><title>This is a great sandwich!</title></head>
<body bgcolor=white>
";

$footer = "
</body></html>
";

&unweb;				# get form data
print "Content-type: text/html\n\n";

open(FOO,$datafile);		# read in datafile
@bar = <FOO>;
close(FOO);
chop(@bar);

print $header;

if (!$ARGV[0]) {
 &guest;
} #if

elsif ($ARGV[0] eq "ask") {
 print "<form action=brian.cgi?answer method=post>\n";
 foreach (@bar) {
  (@moo) = split(/\t+/);
  $acount = 1; $qcount++;
  if ($#moo > 0) {
   $question = $moo[0];
   $answer = $moo[$#moo];
   print "<font size=+1>$question</font><br>\n";
   while ($acount < $#moo) {
    print "<input type=radio name=q$qcount value=a$acount>$moo[$acount]<br>\n";
    $acount++;
   } #while
  } #if
 } #foreach
 print "<input type=submit><input type=reset><br>\n";
 print "</form>\n";
} #if

elsif ($ARGV[0] eq "answer") {
 $played = "yes";
 foreach(@bar) {
  $qcount++;
  @moo = split(/\t+/);
  if ($FORM{"q$qcount"} eq "a$moo[$#moo]") {
   $totalright++;
  } #if
 } #foreach
 &guest;
} #elsif

elsif ($ARGV[0] eq "submit") {
 open(MAIL, "|$mailprog $mailto") || die "Can't open $mailprog\n"; 
 print MAIL "Reply-to: $FORM{email}\n";
 print MAIL "Subject: Guestbook ";
 if ($FORM{totalright}) { print MAIL "[$FORM{totalright} correct]"; }
 print MAIL "\n\n";

 print MAIL "Name:     $FORM{name}\n";
 print MAIL "Address:  $FORM{address}\n";
 print MAIL "Phone:    $FORM{phone}\n";
 print MAIL "Fax:      $FORM{fax}\n";
 print MAIL "E-Mail:   $FORM{email}\n";
 print MAIL "Company:  $FORM{company}\n";
 print MAIL "Position: $FORM{position}\n";
 print MAIL "Innovator Mailing List: $FORM{list}\n";
 print MAIL "More Info On:\n";
 print MAIL " Decontamination & Decommissioning: $FORM{info1}\n";
 print MAIL " Advanced Seperation Technologies:  $FORM{info2}\n";
 print MAIL " Environmental Remediation:         $FORM{info3}\n";
 print MAIL " M&I Facility Support:              $FORM{info4}\n";

 close(MAIL);
 print "Thanks for playing the camel game or whatever\n";
} #elsif

else {
 print "Unrecognized Command\n";
} #else

print $footer;

sub guest {
 print "<form action=brian.cgi?submit method=post>\n";
 if ($played) {  
  print "Thanks for answering questions or whatever<br>\n";
  print "(we won't tell you that you answered $totalright correct)<br>\n";
  print "<input type=hidden name=totalright value=$totalright>\n";
 } #if
 print "<pre>\n";
 print "Your Name: <input size=40 name=name>\n";
 print "Address  : <textarea name=address rows=2 cols=40></textarea>\n";
 print "Phone    : <input size=15 name=phone> Fax: <input size=15 name=fax>\n";
 print "E-Mail   : <input size=40 name=email>\n";
 print "Company  : <input size=40 name=company>\n";
 print "Position : <input size=40 name=position>\n";
 print "Yes, Add My Name to the Mailing List for the NFS Innovator : <input type=checkbox name=list checked>\n";

 print "Send Me More Information Regarding:\n";
 print "  Decontamination & Decommissioning : <input type=checkbox name=info1>\n";
 print "  Advanced Seperation Technologies  : <input type=checkbox name=info2>\n";
 print "  Environmental Remediation         : <input type=checkbox name=info3>\n";
 print "  M&I Facility Support              : <input type=checkbox name=info4>\n";

 print "</pre>\n";
 print "<input type=submit><input type=reset><br>\n";
 print "</form>\n";
} #guest

sub unweb {			# get form data
 read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
 @pairs = split(/&/, $buffer);
 foreach $pair (@pairs) {
   local($formfield, $value) = split(/=/, $pair);
   $value =~ tr/+/ /;
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $FORM{$formfield} = $value;
 } #foreach
} #unweb

