#!/usr/local/bin/perl -w

# By Lars Johannesson (lars@gruk.net)
# version 1.2  copyright 2002 Lars Johannesson

# A somewhat messy script.  Blame it on feature creep..

# This program takes a set of image files in a directory and makes a set of
# container html files to display those images, for ease of viewing. An index 
# (index.html) is created, as well as an html file for each image, with
# "previous, next, # index" links to string them all together. From the
# command line, run the script while in the directory containing the images 
# or run the script with the directory pathname as argument.

# WARNING!!! This program will delete any existing .html files in that
# directory, as well as any files whose name begins with "th_" before going
# to work. This is so that you can add images to the directory and run the
# program again to update the files.  The images will need to have the proper
# suffix for the program to recognize them.  It currently sees files
# with the .gif .GIF .jpg .JPG .jpeg .JPEG suffix.  

# The program takes arguments for background color (currently just -b for black
# - it defaults to white), -nt for 'notext mode', and -th to make thumbnails
# using imagemagick.  Thus one can invoke the script as
# "images.pl -b -th /home/lars/pictures/mypics", to give it a black
# background and to include thumbnails in the index file.  

# If you would like to include some text to the side of the picture, make a
# textfile named <picturename>.txt, put it in the same directory as the
# pictures and it will be included.  The name _has_ to be exactly the same as
# the picture (except for the suffix -- .txt instead of .jpg) to be found, so
# the image foopic.jpg would have a textfile named foopic.txt.

# DISCLAIMER: This script is provided AS-IS, and comes with no guarantees
# whatsoever.  If you mangle your filesystem with it you only have yourself
# to blame for not reading the source before trying it.  There are definitely
# ways to run this script that would cause damage to parts of your computer.
# You have been warned.

use Cwd;	# the current working directory module

if (defined($ARGV[0])) {        # do we have any command line arguments?
    foreach (@ARGV) {
	if ($_ eq "-b") {       # check for color argument.
	    $bgcolor = "000000";
 	    $fontcolor = "ffffff";	
	} elsif ($_ eq "-nt") {	# check for textmode argument.
	    $sidetext_toggle = "off";
	} elsif ($_ eq "-th") {	# check for thumbnail argument.
	    $thumbnails = "on";
	} else {
	    chdir ($_) || die "BAD Options! Not a directory.";         # must have been a directory argument.
	}
    }
}

unless (defined($bgcolor)) {  
	$bgcolor = "ffffff";     # defaults to white background.
        $fontcolor = "000000";	
}

unless (defined($sidetext_toggle)) {  
	$sidetext_toggle = "on";
}

# FIX ME PLEASE!
#if (defined($thumbnails)) {  
	## check for imagemagick and set to "off" if absent.
	#$convert = system ("which convert");
	#unless (-x "$convert") {
		#print "You need to have imagemagick installed, or in your path\n";
		#$thumbnails = "off";
	#}
#} else {
	#$thumbnails = "off";
#}

unless (defined($thumbnails)) {
	$thumbnails = "off";
}

$dirpath = cwd();		# get the path of the current directory
if ($dirpath =~ /(\w+$)/) { 	# matches the last directory in the path
	$dirname = $1;		# assigns it to $dirname.
}

# find all .html files and remove them
while (defined($htdoc = <*.html>)) {
	unlink($htdoc);
}	

# find all thumbnails and remove them
while (defined($thnail = <th_*>)) {
        unlink($thnail);
}

# Find all image files, put them into an array. Copy the array into @images2,
# where we get rid of the suffix and the underscores, for pretty printing.
@images = <*.gif *.GIF *.jpg *.JPG *.jpeg *.JPEG>;
@images = sort(@images);
@images2 = @images;
@images3 = @images;

foreach $image(@images2) {	# get rid of the suffix and the underscore
	$image =~ s/\..*$//;
	$image =~ s/_/ /g;
}

foreach $image(@images3) {	# change suffix to .html
	$image =~ s/\..*$/.html/;
}

# ----------------------------------------------------------------------- #

# make thumbnails
if ($thumbnails eq "on") {
	foreach $image(@images) {
		system ("convert -size 160x100 $image -resize 160x100 +profile '*' -bordercolor black  -border 1x1 th_$image");
		#system ("convert -geometry 160x160 -bordercolor black -border 1x1 $image th_$image");
	}
} 

# ----------------------------------------------------------------------- #

# generate the index file.
generatehtml("index");	

#change the name from tmp.html to index.html
rename("tmp.html", "index.html") || die "can't rename file: $!";

$htdocnumber = 0;	# starts the numbering system to keep track of which image we're referring to

# generate the container files.
while($htdocnumber <= $#images) {	
	$filename = ($images3[$htdocnumber]);
	generatehtml("image");			# generate the files containing the images.
	rename ("tmp.html", $filename);		#change the name from tmp.html to (e.g.) img0.html
	$htdocnumber++;
}

# ----------------------------------------------------------------------- #

# subroutine takes an argument ("image" or "index") and generates the appropriate
# html document with the name tmp.html. Other arguments may have other future purposes.
sub generatehtml {
	$type = $_[0];
	open(HTFILE,">tmp.html") || die("Couldn't create tmp.html");
	print HTFILE ("<HTML>\n");
	print HTFILE ("<HEAD>\n");


	if ($type eq "index") {
		print HTFILE ("<TITLE>\n" . "index of " . $dirname . "\n</TITLE>\n");
	} else {
		print HTFILE ("<TITLE>\n" . $images2[$htdocnumber] . "\n</TITLE>\n");
	}		
	print HTFILE ("</HEAD>\n");
	print HTFILE ("<BODY BGCOLOR=\"$bgcolor\" TEXT=\"#$fontcolor\" LINK=\"47A41E\" VLINK=\"OD84C4\">\n");
	
	if ($type eq "index") {
		print HTFILE ("<H2>Index of images: " . $dirname ."</H2>\n");
		print HTFILE ("<< <a href=\"../\">up a level</a>\n");
		print HTFILE ("<BLOCKQUOTE>\n");
		
		for($x=0; $x<=$#images; $x++) {
			# thumbnail support
			if ($thumbnails eq "on") {
				print HTFILE ("<A HREF = \"" . $images3[$x] . "\"><IMG SRC = \"" . "th_" . $images[$x] . "\" border=\"0\" alt=\"$images2[$x]\"></A>\n");
			} else {
				print HTFILE ("<BR><A HREF = \"" . $images3[$x] . "\">" . $images2[$x] . "</A>\n");
			}
		}
		if ($thumbnails eq "on") {
			print HTFILE ("<P>\n");
			for($x=0; $x<=$#images; $x++) {
				print HTFILE ("<BR><A HREF = \"" . $images3[$x] . "\">" . $images2[$x] . "</A>\n");
			}
		}
		print HTFILE ("</BLOCKQUOTE>");
	}
	
	if ($type eq "image") {
		print HTFILE ("<CENTER>\n");
		#use $htdocnumber to know which image to grab from the array
		if ($htdocnumber == 0) {
			print HTFILE ("<BR><BR>\n");
			print HTFILE ("<font color=\"OD84C4\">previous </font>\n");
		} else {
			print HTFILE ("<BR><BR>\n");
			print HTFILE ("<A HREF=\"" . $images3[$htdocnumber-1] . "\">previous</A>\n");
		}
		unless ($htdocnumber == $#images) {
			print HTFILE ("<A HREF=\"" . $images3[$htdocnumber+1] . "\">next</A>\n");
		}
		print HTFILE ("<A HREF=\"index.html\">index</A>\n");	
		print HTFILE ("</CENTER>\n");
		print HTFILE ("<TABLE border=\"0\" cellpadding=\"20\" align=\"center\">\n");
		print HTFILE ("<TR>\n");
		# picture cell
		print HTFILE ("<td width=\"350\" valign=\"top\">\n");
		#print HTFILE ("<CENTER>\n");
		print HTFILE ("<IMG SRC=\"" . $images[$htdocnumber] . "\" hspace=\"30\" border=\"2\">\n");
		#print HTFILE ("</CENTER>\n");
		print HTFILE ("</td>\n");
		# add sidetext
		if ($sidetext_toggle eq "on") {
			#### cat in some text here from file with same name as image: $images[$htdocnumber] (- gif, +txt)
			$textfile = $images[$htdocnumber];
			$textfile =~ s/\..*$/.txt/;
			if (-e $textfile) {
				print HTFILE ("<td width=\"200\" valign=\"top\">\n");
				#print HTFILE ("<BR><BR>");
				open (TEXTFILE, "< $textfile");
				while (defined ($line = <TEXTFILE>)) {
					print HTFILE ($line);
				}
			}
			print HTFILE ("</td>\n");
		}
		print HTFILE ("</TR>\n");
		print HTFILE ("<TR>\n");
		# Image Name under picture:
		print HTFILE ("<td>\n");
		print HTFILE ("<CENTER>\n");
		print HTFILE ("<font size=\"+1\" color=\"brown\">\n");
		print HTFILE ($images2[$htdocnumber] . "\n");
		print HTFILE ("</font>\n");
		print HTFILE ("</CENTER>\n");
		print HTFILE ("</td>\n");
		print HTFILE ("</TR>\n");
		print HTFILE ("</TABLE>");
	}
	
	print HTFILE "</BODY>\n";	
	print HTFILE "</HTML>\n";
	close(HTFILE);
}	
