#!/usr/local/bin/perl
#############################################################################
#  
#   File: answer.pl
#
#   Author: Paul Coddington
#
#   Version: 0.2  
#
#   Date: 12-Sep-96
#
#   Input: CGI input ('QUERY_STRING') from form.pl
#          QUERY_STRING: name    
#                        email 
#                        answer 
#                        filename 
#
#   Output: Add "answer" to the end of the list in "filename"
#
#
#############################################################################
#  
#   MAIN FUNCTION
#
#############################################################################

require "Global.pl";    # Global variables

%in = ();
&ReadParse(*in);		# parse arguments passed by http server
&AddAnswer();


#############################################################################
#  
#   SUBROUTINES
#
#############################################################################


#####################################################################
#   Output: Add "answer" to the end of the list in "filename"
#####################################################################
sub AddAnswer {          

  $name = $in{'name'};
  $email = $in{'email'};
  $answer = $in{'answer'};
  $filename = $in{'filename'};

  # Check that all entries from the form are correct
  # if ((!$name) || (!$email) || (!$answer)) {
  #   &Error();
  # }
  # $check = &CheckEmail();
  
  if (!open(QUESTFILE, $filename) ) {
	&PrintHeader;
 	print "Cannot open $filename";
 	return 0;
  }
  close(QUESTFILE);

#
# Do an "in place" edit on the answers file, i.e. copy $filename
# to $filename~ and create a new $filename with the answer appended
#
  $^I = "~"; 
  @ARGV = ($filename);
  $end = 0;
  while (<>) {    
    if (/<\/DL>/) {  # end of list of answers
      &Append;
      $end = 1;
    }
    if ((/<ADDRESS>/) && !($end)) {  # This is the first post
      print "<DL>\n";
      &Append;
      print "</DL>\n";
    }
    print;
  }

# Now display the updated file to the user
  &PrintHeader;
  if (!open(QUESTFILE, $filename) ) {
	&PrintHeader;
 	print "cannot open $filename";
 	return 0;
  }
  while (<QUESTFILE>) {    
    print;
  }
  
}
1;



###################################################################
# Add the new answer to the file 
###################################################################
sub Append {

  print "<P>\n";
  print "<DT><B>\n From ";
  if (email eq "") {
     print "$name ";
  } 
  else {
     print "<A HREF=\"mailto:$email\">$name</A>";
  } 
  $date = `date '+at %H:%M:%S on %m/%d/%y'`;
  chop $date;
  print " $date: \n";
  print "</B><P>\n";
  print "<DD>\n $answer \n";
  print "<HR WIDTH=60% SIZE=5 ALIGN=CENTER> \n";
}
1;


