#!/usr/bin/perl

if ($ENV{'REQUEST_METHOD'} eq 'GET') {
    @pairs = split(/&/,$ENV{'QUERY_STRING'});
}
elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
    read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    @pairs = split(/&/, $buffer);
}
else {
    print "Content-type: text/html\n\n";
    print "<P>Use Post or Get";
}

foreach $pair (@pairs) {
    ($key, $value) = split (/=/, $pair);
    $key =~ tr/+/ /;
    $key =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $value =~ s/<!--(.|\n)*-->//g;
    if ($formdata{$key}) {
        $formdata{$key} .= ", $value";
    }
    else {
        $formdata{$key} = $value;
    }
}

$i = 1;
$ref = $ENV{'HTTP_REFERER'};
$add = $ENV{'REMOTE_ADDR'};

($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);

$mon += 1;
$year += 1900;

open (C, "football/count") or die "Can't open file: $!\n";

while ($line = <C>) {
    chop $line;
    ($total[$i], $time[$i], $date[$i], $ref[$i], $add[$i]) = split /\t/, $line;
    $i += 1;
}

close (C);

$k = $i;
$time[$i] = join ':',$hour,$min,$sec;
$date[$i] = join '-',$mon,$mday,$year;
$ref[$i] = $ENV{'HTTP_REFERER'};
$add[$i] = $ENV{'REMOTE_ADDR'};
$total[$i] = $i;
  
open (C, ">football/count") or die "Can't open file: $!\n";

for $x (1..$#total) {
    print C "$total[$x]\t$time[$x]\t$date[$x]\t$ref[$x]\t$add[$x]\n";
}

#print C "";

close (C);

print "Content-type: image/jpeg\n\n";

open (IMAGE,"../images/diamond.jpg");
while (<IMAGE>){
    print;
}




