#!/usr/bin/perl5 -w

# get_images	K. J. Turner	18/02/13

# This script gets "image001.jpeg", etc, in the current directory and prints
# them as an HTML table on the standard output. Images are in the directory
# given as the command-line parameter, while references to them are from the
# parent directory.

############################### Main Program ##############################

$IMAGE_LIMIT = 16;				# set maximum number of images
$IMAGE_BASE = "image";				# set image base name
$JPEG_SUFFIX = ".jpeg";				# set JPEG suffix
$TABLE_COLUMNS = 4;				# set number of table columns
$THUMB_SUFFIX = "s";				# set thumbnail suffix

$directory = $ARGV[0];				# get directory
if (chdir($directory)) {			# change directory success?
  $directory =~ s/.*\/([^\/]+)$/$1/o;		# get base directory
}
else {						# change directory failure
  print STDERR "could not change to directory '$directory'\n"; # report error
  exit(1);					# exit script
}

print "<center>\n";				# output centre start
print "  <table>\n";				# output table start
for ($i = 1; $i <= $IMAGE_LIMIT; $i++) {	# go through images
  $file =					# get current image filename
    sprintf("$IMAGE_BASE%03d$JPEG_SUFFIX", $i);
  if (-r $file) {				# current image file readable?
    if ($i % $TABLE_COLUMNS == 1) {		# start of row?
      if ($i != 1) {				# previous row?
      print "    </tr>\n";			# output row finish
      }
      print "    <tr>\n";			# output row start
    }
    $file_small = $file;			# initialise thumbnail file name
    $file_small =~				# get thumbnail image file name
      s/$JPEG_SUFFIX/$THUMB_SUFFIX$JPEG_SUFFIX/o;
    print("      <td align='center'>" .		# output table cell
      "<a href='$directory/$file'><img src='$directory/$file_small' " .
      "alt='Camera Image'/></a></td>\n");
  }
  else {					# current image file unreadable
    last;					# leave loop
  }
}
print "    </tr>\n";				# output row finish
print "  </table>\n";				# output table finish
print "</center>\n";				# output centre finish

