#!/usr/local/bin/perl
##############################################################################
# wget -N -P older http://www.irkutsk.com/webcam.jpg

# change this according to your installation

$basedir = "/export/home/nautilus/www/webcam";
$latest = "webcam.jpg";
#$ENV{'TZ'} = 'Asia/Irkutsk';  # POSIX.1 timezone of your webcam
$ENV{'TZ'} = 'MSK-8MSD,M3.5.0,M10.5.0'; 

##############################################################################
use File::stat;
use Time::localtime;

$file = "$basedir/$latest";

print "Content-type: text/html\n\n";
print '<html><head><META HTTP-EQUIV="Pragma" CONTENT="no-cache">';

# check if the picture file is there
if (not -r $file) {
    print "File not found: $file<br>";
    exit;
}
$time = ctime(time());
print "<table border=1>";
print "<tr><td align=center>Current date/time:</td></tr>";
print "<tr><td align=center> <b>$time</b> </td></tr> ";
print "<tr><td align=center> Picture received: </td></tr>";
print '<tr><td align=center> <b>', ctime(stat($file)->mtime), '</b> </td></tr>';
# calculating how old is the picture
$deltatime = time()-stat($file)->mtime;
#print "<p>Time: $time <p>";
#print "<p>Deltatime in sec: $deltatime <p>";

$deltaday = int($deltatime / 86400);
$deltatime %= 86400; # drop the days
$deltahour = int($deltatime / 3600); 
$deltatime %= 3600; # drop the hours
# only minutes and seconds left
$deltasec = $deltatime % 60; # seconds only
$deltamin = int($deltatime / 60); # minutes only

print '<tr><td align=center>or <b>';
if ($deltaday) {print "$deltaday days ";}
if ($deltahour) {print "$deltahour hours ";}
if ($deltamin) {print "$deltamin min ";}
#if ($deltasec) {print "$deltasec seconds ";}
print '</b>ago </td></tr>';
print "<tr><td align=center>Time shown may fluctuate<br> due to caching policy<br> of your provider</td></tr>";
print "</table>";
exit;
