#!/usr/local/bin/perl
#############################################################################
#  
#   File: form.pl
#
#   Author: Paul Coddington
#
#   Version: 0.2  
#
#   Date: 12-Sep-96
#
#   Input: None - gets info from HTTP_REFERER
#
#   Output: Form for entering answer to this question. Uses template read 
#           in from $vzhome/form.html, and written out with the variables
#           $FILE replaced by the answer filename $qfile and
#           $QUESTION replaced with the question read in from that file.
#           NOTE: The question MUST be on a single line, with an <H2> tag.
#
#############################################################################

#############################################################################
#  
#   MAIN FUNCTION
#
#############################################################################

require "Global.pl";    # Global variables

# No arguments necessary
# %in = ();
# &ReadParse(*in);		# Parse any arguments passed by http server

$qurl  = $ENV{'HTTP_REFERER'};  # URL for the answer file calling this script
$qfile = &GetPath($qurl);	# Find the file to edit
&MakeForm($qfile,$qurl);        # Display the form to add an answer


#############################################################################
#  
#   SUBROUTINES
#  
#############################################################################

#############################################################################
#  Create the form for entering an answer to the question. 
#  Uses a template read in from $template, and written out with the variables
#    $FILE     replaced with the answer filename $qfile and
#    $QUESTION replaced with the question read in from that file.
#  NOTE: The question MUST be on a single line, with an <H2> tag.
#############################################################################
sub MakeForm { 
  local($qfile,$qurl) = @_;

  if (!open(ANSWERFILE, $qfile) ) {
	&PrintHeader;
 	print "Cannot open $qfile";
 	return 0;
  }
  while (<ANSWERFILE>) {   # Find the question from the answer file 
    if ((/HNAME/) && (/Question/)) {  # Find the reference to the question
      <ANSWERFILE>;   # The question is on the next line of the file 
      $q = $_;
      last;
    }
  }
  if (! $q) {
    &PrintHeader;
    print "Cannot find the question in the answer file $qfile";
    return 0;
  }
  if (!open(FORMFILE, $template) ) {
    &PrintHeader;
    print "Cannot open $template";
    return 0;
  }
  &PrintHeader;
  while (<FORMFILE>) {   # Open the form file and fill in the variables 
    if (/\$QUESTION/) {
      s/\$QUESTION/$q/;
    }
    if (/\$FILE/) {
      s/\$FILE/$qfile/;
    }
    if (/\$URL/) {
      s/\$URL/$qurl/;
    }
    print;
  }
  return 1;
}



##########################################################################
#  Get path and filename for the answer file using referer log. 
##########################################################################
sub GetPath{

  # Now convert from the URL to the file name
  if ($qurl =~ /$url/) {
    $qfile = $docurl;
    $qfile =~ s/$url/$docroot/;    # IS THIS PROPER PERL SYNAX??
  }
  else {
    &PrintHeader;
    print "ERROR! Answer file must be on same server as VitalZines CGI program.";
    die();
  }

  return $qfile;
}
1;


