#!/usr/local/bin/perl

my $cnt_file = '../dat/count.dat';
my %result;

open(CNT, "+< $cnt_file");
flock(CNT, 2);
my $cnt = <CNT>;
++$cnt;
seek(CNT, 0, 0);
print CNT $cnt;
truncate(OUT, tell(OUT));
close(CNT);

if (defined($ENV{PATH_INFO}) and ($ENV{PATH_INFO} eq '/view')) {
  $result{body} = '<!-- count_start -->'. $cnt . '<!-- count_end -->';
  $result{header} = 'text/plain';
} else {
  my $img = '%89PNG%0d%0a%1a%0a%00%00%00%0dIHDR%00%00%00%01%00%00%00%01%01%00%00%00%007n%f9%24%00%00%00%0aIDAT%18%d3ch%00%00%00%82%00%81%a7%01%ba%10%00%00%00%00IEND%aeB%60%82';
  $img =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/pack('H2', $1)/eg;
  $result{body} = $img;
  $result{header} = 'image/png';
}

my $response = <<"__HERE__";
Cache-Control: no-cache
Pragma: no-cache
__HERE__

$response .= sprintf("Content-Type: %s\n\n%s", $result{header}, $result{body});
print $response;
