#!/usr/bin/perl -w
#-------------------------------------------------------------------------------
# viewtalbot.pl - builds a list of photos of talbot house construction
# written by Eric Pence
#-------------------------------------------------------------------------------
use strict;
use CGI qw(-oldstyle_urls :standard);
use CGI::Carp qw(fatalsToBrowser);
use Data::Dumper;
use IO::File;
use File::stat;
use Date::Format;

# Define global variables
use vars qw($curr_dir $log_file);

#---------------------------------------------------------------------------------------------------------
# Grab the current directory from $0. The following regular expression
# looks for 'CURR_DIR\xxxxx.xxx' or 'CURR_DIR/xxxxx.xxx'. Place it in
# the BEGIN block so we can use it to include modules
#---------------------------------------------------------------------------------------------------------
BEGIN {
$0 =~ '(.*[\\\/])\w+\.\w+$';
$curr_dir = $1 || "./";

# Set up error log file
$log_file = "error_log.txt";
use CGI::Carp qw(carpout);
open(LOG, ">>${curr_dir}${log_file}")
or die "Unable to append to error log: $!";
carpout(*LOG);
}

$| = 1; # Disable buffering

use lib ( $curr_dir );
use FileHandle;

my $filename = '';
my $talbot_photos = '';

my @dir_contents = '';
my $work_dir = $curr_dir."../pics/talbot";

opendir(DIR,$work_dir) || die("Cannot open directory $work_dir!\n");
@dir_contents = sort readdir(DIR);
closedir(DIR);

# Reverse the order of the photos with most recent at top (name format is YYYYMMDD_seq-nbr)
#my @display_data = reverse @dir_contents;

## Don't reverse !!!!!
my @display_data = @dir_contents;

# Display the images as clickable thumbnails
my $file_num = 0;
foreach $filename (@display_data) {
if (lc($filename) =~ /\.jpg/) {
$talbot_photos .= "<div style=\"float:left;width:140px;line-height:1.2;font-size:12px\" align=\"center\">
        <a href=\"talbot_pic_slideshow.html?pic=$file_num\"><img src=\"pics/talbot/$filename\" border=\"0\" hspace=\"10\" vspace=\"10\"
  width=\"105\" height=\"70\" /></a></div>";
  $file_num++;
}
}

print header();
print "<html>
<body background=\"../images/myback.gif\">
<table width=\"100%\">
<tr>
<td width=\"3%\"></td>
<td width=\"95%\">$talbot_photos</td>
<td width=\"2%\"></td>
</tr>
</table>

</body>
</html>";

exit;

#-------------------------------------------------------------------------------
# Replace certain characters to make browser compliant
#-------------------------------------------------------------------------------
sub clean_up {
my ($str) = @_;
$str =~ s/\s&\s/ &amp; /g;
$str =~ s/'/\'/g;
$str =~ s/\//&frasl;/g;
$str =~ s/ _ / \/ /g;
return $str;
}

#---------------------------------------------------------------------------------
# Log visit
#---------------------------------------------------------------------------------
sub log_visit {
my $user_ip = $ENV{'REMOTE_ADDR'} || '';
# ignore visits from my PCs at work and home
  if (($user_ip =~ /^12\.130\.32\./) # Safety
   || ($user_ip =~ /^24\.218\.31\./) # home
   ||  ($user_ip =~ /^70\.215\.2\./)) # my Droid
   {
   return;
}
my $browser = '';
my $agent = '';
$agent = $ENV{"HTTP_USER_AGENT"} || '';
if ($agent =~ /Firefox/) {
my $loc = index($agent, 'Firefox');
$browser = substr($agent,$loc,11);
substr($browser,7,1) = ' ';
}
elsif ($agent =~ /MSIE/) {
my $loc = index($agent, 'MSIE');
$browser = substr($agent,$loc+2,6);
}
elsif ($agent =~ /Opera/) {
my $loc = index($agent, 'Opera');
$browser = substr($agent,$loc,10);
substr($browser,5,1) = ' ';
}
elsif ($agent =~ /Chrome/) {
my $loc = index($agent, 'Chrome');
$browser = substr($agent,$loc,17);
substr($browser,6,1) = ' ';
}
else {
$browser = "Other";
}
my $os = '';
if ($agent =~ /Windows NT 5.1/) {
$os = "Windows XP";
}
elsif ($agent =~ /Windows NT 6.0/) {
$os = "Windows Vista";
}
elsif ($agent =~ /Windows NT 5.0/) {
$os = "Windows 2000";
}
elsif ($agent =~ /Windows NT 4.0/) {
$os = "Windows NT.";
}
elsif ($agent =~ /Windows NT 5.2/) {
$os = "Windows Server 2003";
}
elsif ($agent =~ /Win 9x 4.90/) {
$os = "Windows ME";
}
elsif (($agent =~ /Windows 98/) || ($agent =~ /Win98/)) {
$os = "Windows 98.";
}
elsif ($agent =~ /Linux/) {
$os = "Linux";
}
elsif ($agent =~ /Mac/i) {
$os = "MacOS";
}
else {
$os = "$agent";
}

  my $webpage = 'talbot.html';
  my $access_file = "web_access.txt";
  open(ACCESS_FILE, ">>$access_file") || die "Can't open '$access_file'";
  my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  $year = $year + 1900;
  if ($mon < 10) {$mon = "0".$mon;}
  if ($mday < 10) {$mday = "0".$mday;}
  if ($hour < 10) {$hour = "0".$hour;}
  if ($min < 10) {$min = "0".$min;}
  $mon++;
  my $timeData = "$mon/$mday/$year - $hour:$min";
  print ACCESS_FILE "$timeData :: $webpage :: $user_ip :: $browser :: $os\n";
  close(ACCESS_FILE);
return;
}