#!/usr/bin/perl -w
#---------------------------------------------------------------------------------------------------------
# aerials.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 );

my $filename = '';
my $aerials = '';
my @dir_contents = '';
my $work_dir = "../pics/aerials";

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

# Display the images as clickable thumbnails
foreach $filename (@dir_contents) {
if (lc($filename) =~ /\.jpg/) {
# Extract caption from filename
my $caption = $filename;
$caption =~ s/\.jpg//; # Remove extension
$caption =~ s/\.JPG//;
$caption =~ s/(\D+)\d*/$1/; # Remove any numbers at the end of the filename
$aerials = "$aerials
<div style=\"float:left;width:125px;height:125px\" align=\"center\">
<a href=\"../pics/aerials/$filename\"><img src=\"../pics/aerials/$filename\"
border=\"0\" hspace=\"20\" height=\"70\" title=\"$caption\" /></a><br />
$caption
</div>";
}
}

print header();
print "<html>
<table cellpadding=\"15\">
<tr>
<td width=\"5\"></td>
<td valign=\"top\">
$aerials
</td>
<td width=\"5\"></td>
</tr>
</table>
</body>
</html>";

exit;