#!/usr/bin/perl

sub thispage ()
{
  my $thispage = $ENV{"DOCUMENT_URI"};
  
  # Sometimes, particularly the case of going directly to the script,
  # DOCUMENT_URI doesn't work:
  if (! ($thispage =~ /[a-zA-Z0-9]/)) {
    $thispage = $ENV{"REQUEST_URI"};
  }

  if (! ($thispage =~ /[a-zA-Z0-9]/)) {
    $thispage = "";
  }

  $thispage =~ s/\/~([a-zA-Z])/$1/;
  return $thispage;
}


sub not_a_hitcount ()
{
  # Just return a random number.  If viewers want to interpret it as a
  # hitcount, that's none of our business.

  srand (time);
  return int (rand (32768));
}


sub print_footer ()
{
  print "Content-type: text/html\n\n";

  my $prefix = "bin/";
  my $thispage = &thispage ();

  # todo: we can count the number of /'s in $thispage and set prefix
  # according to that.  I'll need to look up a Perl builtin or two
  # first, though; in the meantime, just hardcode the subdirectories:
  if ($thispage =~ /kfogel\/.*\//) {
    $prefix = "../bin/";
  }

  if ($thispage =~ /footer\.cgi/)
  {
    $prefix = "";
    my $sourcefile = "footer.cgi";

    print "<center><strong>${sourcefile}</strong></center>\n";
    print "<br>\n<hr>\n<p>\n";
    print "<p>\n\n";
    
    open (SELF, "<$sourcefile") or die "unable to open $sourcefile ($!)";
    my $contents = "";
    while (<SELF>) { $contents .= $_; }
    close (SELF);
    
    # If you're seeing this in a browser, it probably looks funny:
    $contents =~ s/</&lt;/gs;
    $contents =~ s/>/&gt;/gs;
    
    print "<html>\n<body bgcolor=\"#FFFFFF\">\n";
    print "<pre>\n";
    print $contents;
    print "</pre>\n";
    print "<p>\n";

    print "<p>\n";
    print "<hr>\n";
    print "<p>\n";
  }

  if ($thispage =~ /kfogel\/index\.shtml/) {
    # don't print the "Back to ..." message.
  }
  else {
    print "(Back to <a href=\"http://www.red-bean.com/kfogel\">\n";
    print "Karl Fogel</a>'s home page.)\n";
    print "<br>\n";
  }

  my $thiscount = &not_a_hitcount ();

  print "<a href=\"${prefix}footer.cgi\">";
  print "$thiscount";
  print "</a>\n";
}
  



&print_footer ();
