#!/usr/bin/perl
# Adapted from perl_form by Stephen Bloch, 12/96

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

&ReadParse;

$now = localtime;
# print "Time: $now\n";
$referer = $ENV{"HTTP_REFERER"};
# print "Referer: $referer\n";
$host = $ENV{"REMOTE_HOST"};
# print "Host: $host\n";

#   open (MAIL, "| /usr/sbin/sendmail sbloch\@boethius.adelphi.edu");
#   print MAIL "Subject: log file hit\n\n";
#   print MAIL "Here are all the environment variables:\n";
#   foreach $key (keys %ENV) {
#       print MAIL "   $key: $ENV{$key}\n";
#       }
#   close MAIL;

$data = sprintf "%s\t%s\t%s\n",$now,$host,$referer;

$ignore = $ENV{"IGNORE"};

$_ = $data;
if (! /$ignore/) {

   $filename = $ENV{"FILE"};
   open (FILE, "$filename")
   	|| &send_error ("Couldn't open file $filename for reading.\n",$data);
   
   $firstline = <FILE>;
   if ($firstline ne "sbloch log file\n") {
      &send_error ("attempt to log to file starting with '$firstline'.\n",$data);
      }
   
   close FILE;
   
   open (FILE, ">>$filename")
   	|| &send_error ("Couldn't open file $filename for appending.\n",$data);
   
   printf FILE $data;
   close FILE;

   }

# Adapted from cgi-lib.pl by S.E.Brenner@bioc.cam.ac.uk
# Copyright 1994 Steven E. Brenner
sub ReadParse {
  local (*in) = @_ if @_;
  local ($i, $key, $val);

  if ( $ENV{'REQUEST_METHOD'} eq "GET" ) {
    $in = $ENV{'QUERY_STRING'};
  } elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
    read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
  } else {
        # Added for command line debugging
        # Supply name/value form data as a command line argument
        # Format: name1=value1\&name2=value2\&...
        # (need to escape & for shell)
        # Find the first argument that's not a switch (-)
        $in = ( grep( !/^-/, @ARGV )) [0];
        $in =~ s/\\&/&/g;
  }

  @in = split(/&/,$in);

  foreach $i (0 .. $#in) {
    # Convert plus's to spaces
    $in[$i] =~ s/\+/ /g;

    # Split into key and value.
    ($key, $val) = split(/=/,$in[$i],2); # splits on the first =.

    # Convert %XX from hex numbers to alphanumeric
    $key =~ s/%(..)/pack("c",hex($1))/ge;
    $val =~ s/%(..)/pack("c",hex($1))/ge;

    # Associate key and value. \0 is the multiple separator
    $in{$key} .= "\0" if (defined($in{$key}));
    $in{$key} .= $val;
  }
  return length($in);
}

sub send_error {
    local ($error_msg, $text) = @_;

    open(MAIL, "|/usr/sbin/sendmail -t") 
        || die "Couldn't mail: $!";
    print MAIL <<"EOM";
To: sbloch\@boethius.adelphi.edu
Subject: $0: $error_msg
\n\n$0 was unable to complete because of the following error:
\n$error_msg
\n\nThe following needs to be processed by hand:
\n\n$text
\n.\n
EOM
    close (MAIL);

#     print  <<"ERROR_RESPONSE_PAGE";
# <title>Thanks for your response</title>
# <H1>Thanks for your response</H1>
# The system is a little confused right now, so we weren't
# able to process the data you submitted.  However, it has
# been recorded and mailed to the owner of the page, who 
# will process it by hand later.
# ERROR_RESPONSE_PAGE

    die "$error_msg";
}
