#!/usr/bin/perl
#
# Michael's uber-thumbnailer
# copyright (c) 2000-2007 by Michael Stella
# email: uberthumb@ my domain, figure it out :)
#
# Permission to use, copy, modify, distribute, and sell this software and its
# documentation for any purpose is hereby granted without fee, provided that
# the above copyright notice appear in all copies and that both that copyright
# notice and this permission notice appear in supporting documentation.  No
# representations are made about the suitability of this software for any
# purpose.  It is provided "as is" without express or implied warranty.
#
# On the web at:
# http://www.thismetalsky.org/magic/projects/uberthumb.html
#
# Note this program *requires*:
# ImageMagick (http://www.imagemagick.org/)

# path to your ImageMagick convert program
my $convert = "/usr/local/bin/convert";

## if you want to have defaults, define them here.
## command-line options *always* override defaults.
my $stylesheet = '/style.css';	# stylesheet
my $copyright = "Copyright (c) 2007 Michael Stella";	# optional
my $blurb = 'blurb.txt';		# blurb added above table of images	
								# HTML codes encouraged!
my $title = "pictures";			# page title and above-table heading
my $img_size = "100x80";		# thumbnail size (roughly)
my $img_per_row = 6;			# number of images per table row
my $makeindex = 1;				# if 1, make an index.html file
my $verbose = 0;				# verbosity - higher = more


### NO MOLESTE! ###
use strict;
use Getopt::Long qw(GetOptions);


## process command-line arguments
my %opts;
GetOptions(\%opts,
					'index|i',
					'noindex|n',
					'title:s',
					'blurb:s',
					'stylesheet:s',
					'p:i',
					'size|s=s',
					'help|h',
					'verbose|v+',
					);

$verbose = $opts{verbose} if (defined ($opts{verbose}));

$title = $opts{title} if (defined $opts{title});
$blurb = $opts{blurb} if (defined $opts{blurb});
$stylesheet = $opts{stylesheet} if (defined $opts{stylesheet});
$img_per_row = $opts{p} if (defined($opts{p}));
$makeindex = 1 if (defined($opts{'index'}));
$makeindex = 0 if (defined($opts{noindex}));

if ($opts{help}) {
    &helpmsg;
	exit 1;
}
if ($opts{size}) {
    if ($opts{size} !~ m/^(\d+)x(\d+)$/i) {
        die "Invalid image size format. Should be XxY.\n";
    }
    $img_size = $opts{size};
}

if ($verbose > 2) {
	print "blurb_file: $blurb\n";
	print "page_title: $title\n";
	print "image_size: $img_size\n";
	print "images_per_row: $img_per_row\n";
	print "make_index: $makeindex\n";
	print "verbosity: $verbose\n";
}

## read the directory for image files

opendir DIR, $ENV{PWD} or die "Error obtaining directory! ($!)";
my @files = sort (grep !/^\./, readdir DIR);
closedir DIR;

if ($makeindex) {
	open HTML, ">index.html" || die "Error opening index.html file ($!)";

	print HTML  qq|<HTML>\n<HEAD>\n|;
	if ($title) {
		print HTML  qq|  <TITLE>$title</TITLE>\n|;
	}
	else {
		print HTML  qq|  <TITLE>pictures</TITLE>\n|;
	}
	print HTML qq|  <META NAME="generator" |.
			   qq|CONTENT="uberthumb.pl Copyright (c) 2000-2003 Michael Stella">\n|;
	
	if ($stylesheet) {
		print HTML qq|  <LINK REL="StyleSheet" HREF="$stylesheet" |.
					  qq|TYPE="text/css" MEDIA="screen">\n|;
	}
	print HTML  qq|  <!-- $copyright -->\n|;
	print HTML  qq|</HEAD>\n\n<BODY>\n\n|;
	
	print HTML  qq|<H2>$title</H2>\n\n| if $title;

	# if a blurb file was specified, read it in if it exists
	if ($blurb) {
		($verbose <2) || print "inserting blurb...\n";
		if (open (BLURB, $blurb)) {
			while (<BLURB>) {
				print HTML $_;
			}
			close BLURB;
			print HTML "\n";
		}
		else {
			($verbose <2) || print "blurb not found.\n";
		}
	}
	
	print HTML qq|<TABLE BORDER="2">\n|;
}

my $count = $img_per_row;

# step through each file in the directory
($verbose < 3) || print "Header done, starting files...\n";
foreach my $file (sort @files) {
	chomp $file;
	my $thumbfile = "thumb." . $file;

	# only process image files, and only image files that aren't 
	# thumb'd already
	if (($file !~ /\.(jpeg|jpg|gif|png)$/i) or ($file =~ /^thumb\./)) {
		next;
	}
	
	# only thumb if we have to - if there's no thumb for this one.
	unless (-f $thumbfile) {
		($verbose < 1) || 
			print "thumbing $file -> $thumbfile\n";
		`$convert -geometry $img_size $file $thumbfile`;
		chmod 0644,$thumbfile;
	}
	else {
		($verbose < 4) ||
			print "skipping $file, thumbnail exists\n";
	}
	if ($makeindex) {

		# if we're at the first in the row, print the row start
		if ($count == $img_per_row) {
			print HTML "<TR>\n";
		}

		# print thumbnail <TD>
		print HTML qq|  <TD ALIGN="center"><A HREF="$file">|.
			   	qq|<IMG BORDER="0" SRC="$thumbfile" ALT=""></A></TD>\n|;
	}

	$count --;

	# if we're at the last image in the row, end the row.
	if ($count <= 0) {
		$count = $img_per_row;
		if ($makeindex) {
			print HTML "</TR>\n";
		}
	}
}

if ($makeindex) {
	print HTML "</TABLE>\n</BODY></HTML>";
	($verbose <3) || print "Saving index.html.\n";
	close HTML;
}
exit 0;

sub helpmsg {
	print "Usage:\n";
	print "\ti,index        generate index.html\n";
	print "\tn,noindex      do NOT generate index.html\n";
	print "\ttitle          page title/heading\n";
	print "\tstylesheet     stylesheet file\n";
	print "\tblurb          blurb text file\n";
	print "\n";
	print "\tp=x            x images per row\n";
	print "\ts,size=XxY     make thumbnails XxY resolution (approx)\n";
	print "\n";
	print "\th,help         help\n";
	print "\tv,verbose      verbose\n";
	print "All of the above can have defaults set in the program -\n";
	print "see within for additional documentation.\n";
}

