#!/usr/bin/perl

#adaptation of JNcounter_spesh.pl
#JN 15/8/2007
#adapted again from Things Counter to Mil Support
#JN 23/9/2007

#prep for HTML output
print "Content-type: text/html\n\n";

#500 to screen
#use CGI::Carp qw(fatalsToBrowser);

#lib (everything else is local)
require "/home/theweekl/public_html/science/JNcounter.pl";
&JNcounter("mil_support");

#minutes to wait before repeat visitor is counted (usually 10)
$return_limit = (60 * 10);

#exts and seps
$ext = "JNtxt";
$ext2 = "JNMilhtml";
$ext_counter = $ext;
$sep = ": ";

#counters
$daily_counter = "counter_daily.$ext_counter";
$total_counter = "counter_total.$ext_counter";
$stored_date_day = "counter_day.$ext";

#set
use CGI qw(:standard);

#catches
$safety_catch_ip = "catch_ip.$ext";
$safety_catch_daily = "catch_daily.$ext";
$safety_catch_total = "catch_total.$ext";
$safety_catch_day = "catch_day.$ext";
$safety_catch_on = "on";
$safety_catch_off = "off";

#IP
$ip_file = "ip_file.$ext";

#count reader and/or find vars
&show_page(&set_counter(&shall_we_count_this_reader));

#done
exit;

###----------
sub show_page
{
my ($counter_line) = @_;

#vars
my ($page);

#read page
$page = &read_file("mil_support.$ext2");

#add
$page =~ s/<!-- put the visitor counter here, please -->/$counter_line/ig;

#straight show
print ($page);

#always last sub, so stop
exit;
}

###----------
sub shall_we_count_this_reader
{
#vars
my ($ip, $ip_list, $time_of_last_visit, $daily, $total);

#reader IP
$ip = $ENV{'REMOTE_ADDR'};

#read the IP file
$ip_list = &read_file($ip_file);

#find (IP info + sep + time + return)
if ($ip_list =~ /(^|\n)\Q$ip\E$sep(\d+)(\n|$)/)
{ $time_of_last_visit = $2; }

#count reader if not within time block
if ((time + $bst) > ($time_of_last_visit + $return_limit))
{ &count_reader($ip); }

#find counts
$daily = &read_counter($daily_counter);
$total = &read_counter($total_counter);

#and back
return ($daily, $total);
}

###----------
sub count_reader
{
my ($ip) = @_;

#vars
my ($weekday, $day, $false, $decode_date, $date, $month, $old_date, $time);

#delete old IP info
$ip_list = &read_file($ip_file);
$ip_list =~ s/(^|\n)\Q$ip\E$sep(\d+)(\n|$)/\n/g;

#find time
$time = time + $bst;

#add new info
$ip_list = "$ip_list\n$ip$sep$time";

#super-strip list
$ip_list =~ s/\n\n/\n/g;
$ip_list =~ s/^\n//g;
while ($ip_list =~ /\n$/) { chomp $ip_list; }

#save
&save_ip($ip_list);

#inc counts
&increase_daily_counter;
&increase_total_counter;

#find today's date in "Mon 1 Jan" format (ignore time and year)
$decode_date = gmtime(time + ($bst));
($weekday, $month, $day, $false, $false) = split(/\s+/, $decode_date);
$date = "$weekday $day $month";

#read stored day
$old_date = &read_file($stored_date_day);

#save if first run
if ($old_date eq "") { $old_date = $date; &save_day($old_date); }

#otherwise, new day dawned?
elsif ($date ne $old_date)
{
#yep, so save
&save_day($date);

#clear IP list and daily count
unlink($ip_file);
unlink($daily_counter);
}
}

###----------
sub save_ip
{
my ($ip_list) = @_;

#set GLOBAL catch
$safety_catch = $safety_catch_ip;

#save
&save_file($ip_list, $ip_file);
}

###----------
sub increase_daily_counter
{
#read
my ($daily) = &read_file($daily_counter);

#set GLOBAL catch
$safety_catch = $safety_catch_daily;

#inc and save
&save_file($daily + 1, $daily_counter);
}

###----------
sub increase_total_counter
{
#vars
my ($total, $ip, $host);

#read
$total = &read_file($total_counter);

#set GLOBAL catch
$safety_catch = $safety_catch_total;

#inc
$total ++;

#and save
&save_file($total, $total_counter);
}

###----------
sub save_day
{
my ($day) = @_;

#set GLOBAL catch
$safety_catch = $safety_catch_day;

#save
&save_file($day, $stored_date_day);
}

###----------
sub save_file
{
my ($file_to_save, $filename) = @_;

#save if okay
if (&catch_on) { &write_file($file_to_save, $filename); &catch_off; }

#otherwise ignore (easier and safer; not worth showing error as reader
#can't do anything anyway)
}

###----------
sub catch_on
{
#lock and flag if okay for this process, otherwise skip
if (&catch_check)
{ &write_file($safety_catch_on, $safety_catch); return 1; }
else { return 0; }
}

###----------
sub catch_off
{ &write_file($safety_catch_off, $safety_catch); }

###----------
sub catch_check
{
#find
my ($safety_test) = &read_file($safety_catch);

#add if first pass
if ($safety_test eq "") { &catch_off; }

#otherwise fail if catch on
elsif ($safety_test ne $safety_catch_off) { return 0; }

#all fine, so back
return 1;
}

###----------
sub set_counter
{
my ($daily, $total) = @_;

#vars
my ($counter_line, $date);

#tidy nums
$daily = &tidy_number($daily);
$total = &tidy_number($total);

#find date
$date = &what_is_the_time_please . " on " . &what_is_the_date_please;

#set and back
return ("<p align=\"center\"><small>It's currently $date, and so far $total people have learned of the Mail On Sunday's cowardly bullying theft ($daily today).</small></p>");
}

###----------
sub tidy_number
{
my ($number) = @_;

#force
$number ++; $number --;

#force, words or commas
$number = &add_commas(&number_to_words($number));

#and back
return ($number);
}