#!/usr/bin/perl
# The above line is a throw back to my Unix days...ignore it.
#
# Description:
#
# A Perl script to create an HTML photo album of .GIF. and .JPG files.  It
# specifically looks for all graphic files in the directory specified on the
# URL and lists them and includes the text from corresponding .TXT files.  
# This is all done in table format, creating an index (or album) of the graphics 
# in the directory.
#
# The directory specified must be a directory under the DocumentRoot defined
# in the Web Server's SRM.CFG file.
#
# The directory to album is specified on the URL line following the name of 
# the script.
#
# Example: http://server:port/perl/album.pl/images gives an album of the 
# "images" directory.
# 
# Descriptions of the graphic files are given by creating a text file whose 
# name is the same as the graphic file, except with an extension of .TXT.  
# The description file must be in the same directory as the graphic file.  
# The description will be displayed on the album page.
#
# This may not be the most efficient script in the world <g>, but it works.

# Standard opening commands for a Web Server Perl CGI script
require("cgi-lib.pl");
print &PrintHeader;
&ReadParse;

# Set up the necessary variables
$dir = $ENV{"PATH_TRANSLATED"}; 	#The directory to look in for images
					#This is the real dir on the server
if ( rindex($dir, "/") != length($dir)-1 ) {
  $dir = $dir."/"; 			#If $dir doesn't end in '/', add it
}
$filespec = $dir."*.???";			#filespec will be globbed for image files
$path = $ENV{"PATH_INFO"};		#The dir to the images relative to the DocumentRoot
if ( rindex($path, "/") != length($path)-1 ) {
  $path = $path."/";			#If $path doesn't end in '/', add it
}
@types = ("GIF", "JPG");		#The valid extensions to search for

#--------- HTML Document Setup --------------
print "<html>\n";
print "<title>Album of $path</title>\n";
print "<body bgcolor=#ffffff>\n";
print "<h3>Album of $path</h3>\n";
print "Click on the file name of an image to have the full file delivered.<hr>\n";

#-------- Build Table -------------------
# Set up the table for this .GIF/.JPG
print "<table border=1>\n";
opendir(DIR, $filespec);	#Has to be opened with globbing because of a Perl bug.
foreach $file (sort(readdir(DIR))) {	#Loops through pictures, making the index as it goes.
  $name = substr($file, 0, length($file)-4);
  $ext = substr($file, length($file)-3, 3);
  if (grep(/$ext/, @types) > 0) {
    print "<tr>\n";
    print "  <td valign=top align=left>\n";
    # Display link
    print "    [<a href=$path$file>";
    print "      $file";
    print "    </a>]\n";
    # Display text description
    if (-e "$dir$name.TXT") {
      open(DESC, "$dir$name.TXT");
      while (!eof(DESC)) {
        print <DESC>;
      }
    }
    else {
      print "No description available.\n";
    }
    print "  </td>\n";
    # Display picture
    print "  <td>\n";
    print "    <img src=$path$file border=0>\n";
    print "  </td>\n";
    print "  </td>\n";
    print "</tr>\n";
  }
}
# When all the files are listed, close the directory
closedir(DIR);
# Close the table
print "</table><p>\n";

#---------- HTML Document Wrap-up ------------
print "</body>\n";
print "</html>\n";
