#!/usr/bin/perl -w
#---------------------------------------------------------------------------------------------------------
# boston_boats.pl - this lists the contents of a folder of photos as clickable thumbnail images
# - written by Eric Pence
#---------------------------------------------------------------------------------------------------------
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/boston_boats";

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

# Display the images as clickable thumbnails
my $file_num = 0;
foreach $filename (@dir_contents) {
if (lc($filename) =~ /\.jpg/) {
  # Extract caption from filename
  $filename =~ /^(.+-)(.+)(\.jpg)/;
  my $caption = $2;
  $boat_photos = "$boat_photos
<div style=\"float:left;width:140px;height:125px;line-height:1.2;font-size:12px\" align=\"center\">
<a href=\"boston_boats_slideshow.html?pic=$file_num\"><img src=\"pics/boston_boats/$filename\"
border=\"0\" hspace=\"20\" height=\"70\" title=\"$caption\" vspace=\"5\" /></a><br />
$caption
</div>";
$file_num++;
  }
}

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