#!/usr/bin/perl
#
# $Id: counter.pl,v 1.2 1999/09/01 22:18:53 paxvel Exp $

use CGI;
use GD;
use DB_File;

$reqst = new CGI;
($sec,$min,$hour,$day,$mon,$year) = gmtime;
$year+=1900; $mon++;
$today = sprintf("%04d%02d%02d",$year,$mon,$day);

tie %count, "DB_File", "db/counter.db", O_RDWR|O_CREAT, 0664, $DB_HASH;
$reqid = $reqst->param('id');
$reqid =~ s/^.*?(a-z)+.*?$/$1/;
do {
  $count = sprintf("%5d",++$count{$reqid});
  do {
    $count{"$reqid?"}=$today;
    $count{"$reqid!"}=0;
  } if ($count{"$reqid?"}!=$today);
  $fresh = sprintf("%5d",++$count{"$reqid!"});
}; # if defined($count{$reqid});
untie %count;

$image = new GD::Image(100,30);
$bgcol = $image->colorAllocate(170,170,170);
$black = $image->colorAllocate(0,0,0);
$red   = $image->colorAllocate(127,0,0);
$green = $image->colorAllocate(0,127,0);
$blue  = $image->colorAllocate(0,0,127);
#$image->rectangle(0,0,80,30,$green);
#$image->fill(0,0,$bgcol);
$image->transparent($bgcol);
$image->stringUp(gdSmallFont,1,26,"HITS",$black);
$image->stringUp(gdSmallFont,1,27,"HITS",$black);
$image->string(gdMediumBoldFont,60,2,"today",$blue);
$image->string(gdMediumBoldFont,60,16,"total",$blue);
$image->string(gdMediumBoldFont,20,2,"$fresh",$red);
$image->string(gdMediumBoldFont,20,16,"$count",$red);

#print $reqst->header(-type=>'image/png',-expires=>'+30s');
print $image->png;

open ACCESS, ">>db/access.log";
print ACCESS sprintf("%02d/%02d/%04d %02d:%02d:%02d",$day,$mon,$year,$hour,$min,$sec);
print ACCESS " $ENV{'REMOTE_ADDR'}\t$reqid\n";
close ACCESS;
