#!/usr/bin/perl -w
#---------------------------------------------------------------------------------------------------------
# boat_commute.pl - this lists the contents of a folder of photos as clickable thumbnail images
# - written by Eric Pence
# This script runs in the cgi-bin directory, so paths to images on the webpage must be relative to that location.
# The output is displayed in the root directory via Ajax (view source of webpage in browser to see this method),
# so created links are relative to that location.
# The files are named in the format 'a-some text.jpg', where 'a' is a letter of the alphabet or group of letters
# followed by a dash. This allows me to insert new files or change the order of the display by renaming files.
# The caption under the displayed thumbnail is parsed from the file name (between '-' and '.jpg').
#---------------------------------------------------------------------------------------------------------
use strict;
use CGI qw(-oldstyle_urls :standard);
use CGI::Carp qw(fatalsToBrowser);
use IO::File;

# 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);
}

use lib ( $curr_dir );
use FileHandle;

my $filename = '';
my $boat_photos = '';

my @dir_contents = '';
my $work_dir = $curr_dir."../pics/boat_commute";
  
opendir(DIR,$work_dir) || die("Cannot open directory $work_dir!\n");
@dir_contents = sort readdir(DIR);
closedir(DIR);

my $count = 0;
# Display the images as clickable thumbnails
foreach $filename (@dir_contents) {
if (lc($filename) =~ /\.jpg/) {
  # Extract caption from filename
  my $shortname = $filename;
  $shortname =~ s/\.jpg//;
  $shortname =~ s/\.JPG//;
  my $length = length($shortname);
  my $dash_loc = index($shortname,'-');
  my $name_start = $dash_loc + 1;
  my $caption = substr($shortname,$name_start,$length-$name_start);
  $boat_photos = "$boat_photos
      <td align=\"center\" valign=\"top\" width=\"100\">
        <a href=\"pics/boat_commute/$filename\"><img src=\"pics/boat_commute/$filename\"
        border=\"0\" hspace=\"20\" height=\"70\" title=\"$caption\" /></a>
        $caption
      </td>";
    $count++;
    if ($count > 6) {
      $boat_photos = "$boat_photos</tr><tr><td></td>";
      $count = 0
    }
  }
}

# Log visit -- ignore visits from my PCs at work and home
my $user_ip = $ENV{'REMOTE_ADDR'} || '';
if (!(($user_ip =~ /^38\.97\.67\./) || ($user_ip =~ /^74\.9\.36\./) || ($user_ip =~ /^24\.147\.129\./) || ($user_ip =~ /^24\.62\.134\./))) {
  my $access_file = "boatpage_visit.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 ($min < 10) {$min = "0".$min;}
  $mon++;
  my $timeData = "$mon/$mday/$year - $hour:$min";
  print ACCESS_FILE "$timeData :: $user_ip\n";
  close(ACCESS_FILE);
}

print header();
print "<html>
<body background=\"../images/myback.gif\">
<table cellpadding=\"15\">
  <tr>
    <td width=\"5\"></td>
      $boat_photos
    <td width=\"5\"></td>
  </tr>
</table>
<p />
</body>
</html>";