#!/usr/local/bin/perl
# to use, embed
# <img align=top src="/cgi-bin/odometer6042?foo">
# in your html file

if ($#ARGV >= 0) {
    $counterfile = @ARGV[0];
} else {
    die "need filename as argument";
}

require('sys/file.ph') || die "can\'t do sys/file.ph: $@";
require('ctime.pl') || die "can\'t do ctime.pl: $@";

# be suspicious, very suspicious
$counterfile =~ tr/[a-z,A-Z,0-9,-,_,\/]//cd;
$counterfile = "/a/class/6.042/fall99/.counts/" . $counterfile;

# bitmap for each digit
@invdigits = (0xff,0xff,0xff,0xc3,0x99,0x99,0x99,0x99,
	   0x99,0x99,0x99,0x99,0xc3,0xff,0xff,0xff,
	   0xff,0xff,0xff,0xcf,0xc7,0xcf,0xcf,0xcf,
	   0xcf,0xcf,0xcf,0xcf,0xcf,0xff,0xff,0xff,
	   0xff,0xff,0xff,0xc3,0x99,0x9f,0x9f,0xcf,
	   0xe7,0xf3,0xf9,0xf9,0x81,0xff,0xff,0xff,
	   0xff,0xff,0xff,0xc3,0x99,0x9f,0x9f,0xc7,
	   0x9f,0x9f,0x9f,0x99,0xc3,0xff,0xff,0xff,
	   0xff,0xff,0xff,0xcf,0xcf,0xc7,0xc7,0xcb,
	   0xcb,0xcd,0x81,0xcf,0x87,0xff,0xff,0xff,
	   0xff,0xff,0xff,0x81,0xf9,0xf9,0xf9,0xc1,
	   0x9f,0x9f,0x9f,0x99,0xc3,0xff,0xff,0xff,
	   0xff,0xff,0xff,0xc7,0xf3,0xf9,0xf9,0xc1,
	   0x99,0x99,0x99,0x99,0xc3,0xff,0xff,0xff,
	   0xff,0xff,0xff,0x81,0x99,0x9f,0x9f,0xcf,
	   0xcf,0xe7,0xe7,0xf3,0xf3,0xff,0xff,0xff,
	   0xff,0xff,0xff,0xc3,0x99,0x99,0x99,0xc3,
	   0x99,0x99,0x99,0x99,0xc3,0xff,0xff,0xff,
	   0xff,0xff,0xff,0xc3,0x99,0x99,0x99,0x99,
	   0x83,0x9f,0x9f,0xcf,0xe3,0xff,0xff,0xff
	   );
@digits = (0x00,0x00,0x00,0x3c,0x66,0x66,0x66,0x66,
	   0x66,0x66,0x66,0x66,0x3c,0x00,0x00,0x00,
	   0x00,0x00,0x00,0x30,0x38,0x30,0x30,0x30,
	   0x30,0x30,0x30,0x30,0x30,0x00,0x00,0x00,
	   0x00,0x00,0x00,0x3c,0x66,0x60,0x60,0x30,
	   0x18,0x0c,0x06,0x06,0x7e,0x00,0x00,0x00,
	   0x00,0x00,0x00,0x3c,0x66,0x60,0x60,0x38,
	   0x60,0x60,0x60,0x66,0x3c,0x00,0x00,0x00,
	   0x00,0x00,0x00,0x30,0x30,0x38,0x38,0x34,
	   0x34,0x32,0x7e,0x30,0x78,0x00,0x00,0x00,
	   0x00,0x00,0x00,0x7e,0x06,0x06,0x06,0x3e,
	   0x60,0x60,0x60,0x66,0x3c,0x00,0x00,0x00,
	   0x00,0x00,0x00,0x38,0x0c,0x06,0x06,0x3e,
	   0x66,0x66,0x66,0x66,0x3c,0x00,0x00,0x00,
	   0x00,0x00,0x00,0x7e,0x66,0x60,0x60,0x30,
	   0x30,0x18,0x18,0x0c,0x0c,0x00,0x00,0x00,
	   0x00,0x00,0x00,0x3c,0x66,0x66,0x66,0x3c,
	   0x66,0x66,0x66,0x66,0x3c,0x00,0x00,0x00,
	   0x00,0x00,0x00,0x3c,0x66,0x66,0x66,0x66,
	   0x7c,0x60,0x60,0x30,0x1c,0x00,0x00,0x00
	   );

$hex = "0123456789abcdef";

# Read and increment the counter file
while (-f "${counterfile}.lock") { sleep 1 }
open(LOCK,">${counterfile}.lock");

open(HOSTSFILE,">>${counterfile}.hosts") || die "can\'t open ${counterfile}.hosts: $!\n";
#printf HOSTSFILE "%s@%s\n",$ENV{'REMOTE_USER'},$ENV{'REMOTE_HOST'};
#printf HOSTSFILE "%s@%s - - ",$ENV{'REMOTE_USER'},$ENV{'REMOTE_HOST'};
printf HOSTSFILE "%s - - ", $ENV{'REMOTE_HOST'};
$Date = &ctime(time);
printf HOSTSFILE "%s",$Date;
#($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
#printf HOSTSFILE "[%02d/%s/19%02d:%02d:%02d:%02d %s]\n",$mday,$MoY[$mon],$year,$hour,$min,$sec, $TZ;
close HOSTSFILE;

if (open(COUNTERFILE,"<$counterfile")) {
    $text = <COUNTERFILE>;
    close(COUNTERFILE);
}k else {
    $text = 0;
}
$text++;

$len = length($text) > 7 ? length($text) : 7;
open(COUNTERFILE,">$counterfile") || die "can\'t open $counterfile: $!\n";
printf COUNTERFILE "%0${len}u\n",$text;
$text = sprintf("%0${len}u",$text);
#flock(COUNTERFILE,&LOCK_UN);
close(COUNTERFILE);
unlink("${counterfile}.lock");

# Generate an X11 bitmap on STDOUT
printf STDOUT "Content-type: image/x-bitmap\n\n";
printf STDOUT "#define count_width %d\n#define count_height 16\n", $len*8;
printf STDOUT "static char count_bits[] = {\n";
for ($y=0; $y < 16; $y++) {
    for ($x=0; $x < $len; $x++) {
	$d = substr($text,$x,1) - '0';
	print STDOUT '0x';
	if ($inv) {		# $inv = 1 for inverted text
	    printf STDOUT "%1x",($invdigits[($d * 16) + $y] >> 4) & 0xf;
	    printf STDOUT "%1x",$invdigits[($d * 16) + $y] & 0xf;
	} else {
	    printf STDOUT "%1x",($digits[($d * 16) + $y] >> 4) & 0xf;
	    printf STDOUT "%1x",$digits[($d * 16) + $y] & 0xf;
	}
	if ($x < $len-1) {
	    print STDOUT ',';
	}
    }
    if ($y==15) {
	print STDOUT '};';
    } else {
	print STDOUT ',';
    }
    print STDOUT "\n";
}
