#!/usr/bin/perl
# print "Content-type: text/html\n\n";

$filename = $ENV{"FILE"};
# print "FILE defined as $filename\n";

$data = sprintf("%s %s", $ENV{"USER"}, $ENV{"REMOTE_HOST"});

open (FILE, "$filename")
	|| &send_error ("Couldn't open file $filename for reading.\n","");

$firstline = <FILE>;
if ($firstline ne "sbloch counter file\n") {
   &send_error
  ("attempt to count hits using file '$filename' starting with '$firstline'.\n",
     $data);
   }

$count = <FILE>;
close (FILE);

# open (FILE, ">$filename")
# 	|| &send_error ("Couldn't open file $filename for writing.\n",$count+1);
# print FILE "sbloch counter file\n";
# print FILE $count+1;
# close FILE;

print "$count";

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);

    die "$error_msg";
}
