#!/usr/bin/perl

# This is a companion program to the "counter.xbm" script.  
# When invoked, it responds with some HTML that it builds on
# the fly that give usage statistics.

# This script was written by Michael Morse (mmorse@nsf.gov) at
# the National Science Foundation.  Feel free to modify and use
# it any way you wish.  You will certainly have to modify
# initialize(), since most of the NSF-specific stuff is there.
# Also, createHTML() has some more NSF-specific stuff (like my
# address!), that you'll want to change.

   &initialize;
   &getDate;
   &getNumbers;
   &createHTML;
exit(0);

sub createHTML {
print<<EOMEOM;
Content-type: text/html

<TITLE>Usage Statistics for the $forString</TITLE>
<H1>Usage Statistics for the $forString</H1>
<HR>
<H2>Numbers That May Interest You</H2>
As of now ($date):
<UL>
<LI>
The $theString has been retrieved from the server 
<B>$count</B> 
times since October 20, 1995
<LI>
A total of 
<B>$hosts</B> 
different computers have retrieved the 
University of Chicago Physics Home Page
Since most computers that run WWW clients are single-user, this 
number is pretty close to the number of different users.
<LI>
Your computer has retrieved the Physics Home Page
<B>$you</B> 
times.
</UL>
<HR>
<H2>How the Numbers are Created</H2>
The "odometer" on the Physics Home Page is generated by a Perl program that
counts the number of times it is invoked, keeping track, each time,
of the IP address of the client. 
This page is generated by another program (also in Perl) that reads the data
produced by the odometer program.  Actually, the odometer is incremented only
when your browser tries to retrieve the odometer image. <p>

The odometer counter was adapted from a similar program
used at 
<a href="http://www.larc.nasa.gov/">
NASA Langley Research Center. 
</A>  This script was cribbed directly from <a href=http://guraldi.hgp.med.umich.edu:80/beer/>The Beer Page </a>.

<HR>
EOMEOM
}



sub initialize {
   $port = $ENV{SERVER_PORT};
      $serverRoot = "/usr/local/WWW";
      $pageName = "Physics_odometer";
      $forString = "Physics Home Page";
      $theString = "Physics Home Page";
   $counterFile = "$serverRoot/odometers/$pageName/counter.txt";
   $hostsFile = "$serverRoot/odometers/$pageName/counthosts.txt";
   $dbmFile =   "$serverRoot/odometers/$pageName/hosts";
}



sub getNumbers {
   open(COUNTERFILE,"<$counterFile") || die "$0: can\'t open $counterFile: $!\n";
   $count = <COUNTERFILE>;
   chop $count;
   close(COUNTERFILE);
   open(COUNTERFILE,"<$hostsFile") || die "$0: can\'t open $hostsFile: $!\n";
   $hosts = <COUNTERFILE>;
   chop $hosts;
   close(COUNTERFILE);
   
   dbmopen(HOSTS,$dbmFile,0666);
   $who = $ENV{REMOTE_ADDR};
   $you = $HOSTS{$who};
   dbmclose(HOSTS);
   $count =~ s/^0+//;
   $hosts =~ s/^0+//;
   $you =~ s/^0+//;
   
}



sub getDate{
   @month_names = ("January","February","March","April","May","June",
           "July","August","September","October","November","December");
   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
   $date = sprintf("%s $mday, 19$year, $hour:%02d",$month_names[$mon],$min);
}
