#!/usr/local/bin/perl -w
# -*- Perl -*- Mon Dec 17 16:58:18 CST 2001
###############################################################################
# make_tn - makes a web page of thumbnails for a given director(y|ies).
# Depends on ImageMagick, at http://www.imagemagick.org/
# Written by Tim Skirvin <tskirvin@ks.uiuc.edu>.  
# Copyright 2001, Tim Skirvin and UIUC Board of Trustees. 
###############################################################################

use vars qw( $CONVERT $IDENTIFY $VERSION $HTMLHEAD $HTMLFOOT $COLUMNS $SIZE );
$VERSION = "1.00";

###############################################################################
### CONFIGURATION + PRIVATE DATA ##############################################
###############################################################################

## These are references to code that will output the headers and footers
## for the messages.  If you want to change these, you can either modify
## the code (which is below) or create a new set of functions and change
## the below code references appropriately.

$HTMLHEAD = \&html_head;
$HTMLFOOT = \&html_foot;

## Locations of the 'convert' and 'identify' binaries from the ImageMagick
## package.  These are necessary to create the 

$CONVERT  = "/usr/local/bin/convert";	
$IDENTIFY = "/usr/local/bin/identify";

## How many columns do you want per page?  

$COLUMNS    = 4;

## How how many pixels wide should the thumbnails be?  You probably don't
## want to change this, but I leave it to you.  

$SIZE        = 100;

###############################################################################
### main() ####################################################################
###############################################################################

use strict;

$0 =~ s%.*/%%;

# my $title = shift @ARGV or die "Usage: $0 TITLE [DIRECTORY]\n";
my $title = shift @ARGV or usage();
push @ARGV, "." unless scalar @ARGV;

my @files;
foreach my $dir (@ARGV) { 
  opendir(DIR, $dir) or (warn "Couldn't open directory '$dir'\n" and next);
  foreach my $file (readdir(DIR)) {
    next if ($file =~ /^\./);			# Skip .*
    next if ($file =~ /-tn\d?\./);		# Skip thumbnails
    next if ($file =~ /html$/i);		# Skip HTML files
    push @files, "$dir/$file" if identify("$dir/$file");
  }
  closedir(DIR);
}

open (INDEX, ">index.html") or die "Couldn't write to index.html: $!\n";
print INDEX &$HTMLHEAD($title);
print INDEX "<TABLE>\n <TR>\n";

my $count = 0;
foreach my $file (@files) {
  my ($start, $extension) = $file =~ /^(.+)\.([^\.]+)$/;
  
  # Create a thumbnail, unless it already exists.
  my $thumb = "$start-tn.$extension";	
  unless (-r $thumb) {	
    my $string = "$CONVERT -size $SIZE $file $thumb";
    print "Executing '$string'\n";
    system($string);
  }

  # Don't print the "new table line" code unless we've printed $COLUMNS tns
  if ($count >= $COLUMNS) { print INDEX " </TR><TR>\n"; $count = 0 } 
  $count++;	

  print INDEX "  <TD ALIGN=CENTER>\n";
  print INDEX "   <A HREF='$file'><IMG SRC=\"$thumb\" width=$SIZE></A>\n";
  print INDEX "  </TD>\n";
}

print INDEX " </TR>\n</TABLE>\n";
print INDEX &$HTMLFOOT();
close INDEX;

## Figures out whether a given file is actually an image, using $IDENTIFY

sub identify {
  my ($file) = @_;
  return undef unless (-r $file);
  open(IDENTIFY, "$IDENTIFY $file |") or return undef;
  my @strings = <IDENTIFY>;
  close(IDENTIFY);
  chomp @strings;
  scalar @strings eq 1 ? $file : undef;
}

## Subroutine to print the usage information and exit.  The usage
## information just happens to be the manual page.  Hooray!
sub usage {
  use Pod::Text;
  my $parser = Pod::Text->new( sentence => 0, width => 78 );
  $parser->parse_from_file($0);
  exit(0);
}

## Subroutine to print the HTML headers.  Takes 'title' as an input, and 
## that's it.  Edit this as you see fit.

sub html_head {
  my $title = shift;
  return <<EOL;
<HTML><HEAD><TITLE>$title</TITLE></HEAD>
<BODY>
 <h1>$title</h1>
EOL
}

## Subroutine to print the HTML footers.  Edit this as you see fit.

sub html_foot {
  return <<EOL;
<hr>
<i><font size=-1>
Thumbnails created using <a href="http://www.imagemagick.org/">ImageMagick</a>
and <a href="http://www.ks.uiuc.edu/Development/MDTools/maketn">make_tn</a></font>.
</BODY></HTML>
EOL
}

=head1 NAME

make_tn - makes a web page of thumbnails for a given director(y|ies).

=head1 SYNOPSIS

  make_tn TITLE DIRECTORY [DIRECTORY [DIRECTORY]]
  make_tn 'My Title' ~/pics ~/pics2
 
=head1 DESCRIPTION

make_tn is used to create thumbnail images and a web page to index them.
The general steps:

  - Creates an index.html file in the directory you're currently in, with
    the title of TITLE.  
  - Looks through all of the files in the specified DIRECTORY/s to see 
    if they're images.  If no directories are specified, assumes that you
    want to look in the current directory.
  - Creates thumbnails for those files for which it's necessary 
    ('*-tn.jpg' is the general nomenclature).
  - Writes the rest of the page out using on a table with the number of 
    COLUMNS set in the configuration.

The middle two steps depend on the ImageMagick package.  

=head1 REQUIREMENTS

Requires Perl 5 (and the included Pod::Text module), and the ImageMagick 
package to identify and convert image sizes.  

=head1 SEE ALSO

B<http://www.imagemagick.org>,
B<http://www.ks.uiuc.edu/Development/MDTools/maketn>

=head1 AUTHOR

Tim Skirvin <tskirvin@ks.uiuc.edu>

=head1 COPYRIGHT

Copyright 2001, Tim Skirvin and UIUC Board of Trustees.

=head1 LICENSE

See B<http://www.ks.uiuc.edu/Development/MDTools/maketn/license>

=cut

##### Version History
# v1.0		Tue Dec 18 08:44:12 CST 2001
### Documented and ready for release.
