#!/usr/bin/perl -w
#---------------------------------------------------------------------------------------------------------
# show_thumbs.pl - this lists the contents of a folder of photos as clickable thumbnails
# written by Eric Pence
#---------------------------------------------------------------------------------------------------------
use strict;
use CGI qw(-oldstyle_urls :standard);
use CGI::Carp qw(fatalsToBrowser);

# 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 $count = 0;
my $filename = '';
my $files = '';

# Set up the return link
my $penceland_href = '';
my $referrer = $ENV{'HTTP_REFERER'};
if (lc($referrer) =~ /program.html/) {
$penceland_href = "../../program.html\#perl";
}
else {
$penceland_href = "../../mypics/index.htm";
}

# Get the list of the images to display
my @dir_contents = '';
my $work_dir = $curr_dir."../../mypics";

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

# Format the images as clickable links
foreach $filename (@dir_contents) {
if (lc($filename) =~ /\.jpg/) {
my $shortname = $filename;
$shortname =~ s/\.jpg//;
$shortname =~ s/\.JPG//;
$files = "$files
<a href=\"../../mypics/$filename\"><img src=\"../../mypics/$filename\" width=\"100\" title=\"$shortname\"
border=\"0\" hspace=\"10\" vspace=\"10\" align=\"top\" /></a>";
}
}

print header();
print "
<ul>
$files
</ul>";