#!/usr/local/bin/perl

&PrintHeader;
&ReadParse;

sub ReadParse {
  local (*n) = @_ if @_;
  local ($i, $key, $val);
if (&MethGet) {
    $in = $ENV{'QUERY_STRING'};
} elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
  read(STDIN, $in, $ENV{'CONTENT_LENGTH'});
}
@in = split(/&/, $in);
foreach $i (0 .. $#in) {
  $in[$i] =~ s/\+/ /g;
  ($key, $val) = split(/=/, $in[$i],2);
  $key =~ s/%(..)/pack("c",hex($1))/ge;
  $val =~ s/%(..)/pack("c",hex($1))/ge;
  $in{$key} .= "\0" if (defined($in{$key}));
  $in{$key} .= $val;
  }

  local($comments) = $in{"COMMENTS"};

  $in{"LNAME"} =~ tr/a-z /A-Z/d;
  $in{"FNAME"} =~ tr/A-Z /a-z/d;
  $in{"MAILING"} =~ s/(\n+)|\r+/<br>/g;
  $in{"MAILING"} =~ s/<br><br>/<br>/g;
  $in{"COMMENTS"} =~ s/(\n+)|\r+/<br>/g;
  $in{"COMMENTS"} =~ s/<br><br>/<br>/g;

  local($name) = join(', ', $in{"LNAME"}, $in{"FNAME"}); 
  local($nameanchor) = join('', $in{"LNAME"}, $in{"FNAME"}); 
#  ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
#  local($time) = $mon.$mday."'".$year;
  local($day) = `date`;


  open (WRITEFORUM, ">>/home/B11B/jguo/httpd/htdocs/zu/zuforum.html")||die("Can't open file: zuforum.html");

  
  write (WRITEFORUM);

  close (WRITEFORUM);
  
  print "Your comments have been added to the ZU Forum Page, please check: <p> \
         <a href=\"http://kayak.npac.syr.edu:2020/zu/zuforum.html\#$nameanchor\"> \
              ZU Forum </a> \n";

  return length($in);
}

sub PrintHeader {
#   return "Content-type: text/html\n\n";
    print "Content-type: text/html\n\n";
}

sub MethGet {
  return ($ENV{'REQUEST_METHOD'} eq "GET");
}


# Input fields in format WRITETEXT are: 20, 40 or 60 columns, and 8 or 16 rows
  format WRITEFORUM = 

  <!-- ========================================================================= -->
  <p> <hr> <name="@*
                  $nameanchor    
  ">
  <b> @<<<<<<<<<<<<<<<<<<<< </b> (@*     
      $name,   $in{"EMAIL"}
  ) wrote 
  on @* 
     $day
  :<p>@* 
     $in{"COMMENTS"}  
  <p>Location: @* 
       $in{"MAILING"}
.

1;
