#!/usr/local/bin/perl

#===================================================================#
# Author : Navid Mehregani	  (99mehreh@utsc.utoronto.ca)           #
#                                                                   #
# Description: Given a directory that contains .jpg and/or .gif     #
#              files, it will organize all the pics in HTML format. #
#			   There are two main HTML files created.  The first    #
#			   one organizes the pictures in a table format and the #
#              second one allows for easy browsing of each single   #
#			   picture.												#
#===================================================================#

use Getopt::Std;

if ($#ARGV < 0) {
	usage();
}

$ret = getopts('');

if (!$ret)
{
	usage();
}

# Save all JPG and GIF files in this array
@directory = `dir $ARGV[0]\\\*\.jpg $ARGV[0]\\\*\.gif /on/b 2>&1`;

if ($#directory == 0) {
	print "The specified directory doesn't contain any pictures.\n";
	exit;
}

# Create the required directories
if (!(-e "$ARGV[0]\\FullSize")) {
	`md $ARGV[0]\\FullSize`;
}
if (!(-e "$ARGV[0]\\SmallerSize")) {
	`md $ARGV[0]\\SmallerSize`;
}

open (WRITE, ">$ARGV[0]\\allPics.html")  || die ("Cannot open $ARGV[0]\\allPics.html\n");
open (NEXTPIC, ">$ARGV[0]\\singlePic.html") || die ("Cannot open $ARGV[0]\\singlePic.html\n");
open (BOTTOM, ">$ARGV[0]\\SmallerSize\\bottom.html") || die ("Cannot open $ARGV[0]\\SmallerSize\\bottom.html\n");

print WRITE "<HTML>
\t<HEAD>
\t\t<TITLE>Pics Pics!! </TITLE>
\t</HEAD>
\t<BODY>
\t\t<CENTER>
\t\t\t<FONT face=\"Verdana,Arial\" size=\"5\" color=\"purple\"> Pictures Pictures!! </FONT><BR><BR>
\t\t\t<TABLE border=\"1\">
\t\t\t\t<TR>\n";

print NEXTPIC "<HTML>
\t<HEAD>
\t\t<TITLE>Pics Pics!! </TITLE>
\t</HEAD>
\t<FRAMESET rows=\"80%,*\" frameborder=\"1\"> 
\t\t<FRAME name=\"upperframe\" scrolling=\"NO\" noresize src=\"SmallerSize\\single1.html\">
\t\t<FRAME name=\"lowerframe\" scrolling=\"NO\" noresize src=\"SmallerSize\\bottom.html\">
\t</FRAMESET>
</HTML>\n";

print BOTTOM "
<HTML>
	<HEAD>
		<SCRIPT type=\"text/javascript\">
			var picNum = 1;
			function next()
			{
				if (picNum != $#directory) { picNum++; }
				parent.upperframe.location.href=\"single\" + picNum +\".html\";
			}

			function prev()
			{
				if (picNum != 1) {	picNum--; }
				parent.upperframe.location.href=\"single\" + picNum +\".html\";
			}
		</SCRIPT>
	</HEAD>

	<BODY>
		<FORM>
			<CENTER>
				&nbsp;&nbsp;&nbsp;<TD><INPUT type=\"button\" value=\"Prev\" onclick=\"prev()\"></TD>
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				&nbsp;&nbsp;&nbsp;<TD><INPUT type=\"button\" value=\"Next\" onclick=\"next()\"></TD>

				<BR><BR>&nbsp;&nbsp;&nbsp;
				Click on image for a larger view
			</CENTER>
		</FORM>
	</BODY>
</HTML>\n";


$count = 0;

$WIDTH = 150;
$HEIGHT = 140;
$NUM_PICS_EACH_ROW = 5;

if (($ARGV[1]) && ($ARGV[1] > 0)) {
	$WIDTH = $ARGV[1];
}

if (($ARGV[2]) && ($ARGV[2] > 0)) {
	$HEIGHT = $ARGV[2];
}

if (($ARGV[3]) && ($ARGV[3] > 0)) {
	$NUM_PICS_EACH_ROW = $ARGV[3];
}

foreach $pic (@directory) {
	chomp($pic);
	if (($pic =~ m%\.(jpg|JPG)%)||($pic =~ m%\.(gif|GIF)%)) {
		$count++;
		open (WRITESUB, ">$ARGV[0]\\FullSize\\pic$count\.html") || die ("Can't open $ARGV[0]\\FullSize\\pic$count\.html\n");
		open (SINGLE, ">$ARGV[0]\\SmallerSize\\single$count\.html") || die ("Can't open $ARGV[0]\\SmallerSize\\single$count\.html\n");

		print WRITESUB "<HTML>\n\t<HEAD>\n\t\t<TITLE>$pic</TITLE>\n\t<HEAD>\n\t<BODY>\n\t\t<IMG SRC=\"\.\.\\$pic\"/>\n\t</BODY>\n</HTML>";
		print SINGLE "<HTML>\n\t<HEAD>\n\t\t<TITLE>$pic</TITLE>\n\t</HEAD>\n\t<BODY>\n\t\t<CENTER><A HREF=\"\.\.\\FullSize\\pic${count}\.html\" target=\"new\"><IMG src=\"\.\.\\$pic\" width=\"550\" height=\"400\" border=\"0\"/></A></CENTER>\n\t		<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT face=\"Verdana,Arial\" size=\"1\" color=\"gray\"> $count\/$#directory </FONT></BODY>\n</HTML>";

		print WRITE "\t\t\t\t\t<TD><A HREF=\"FullSize\\pic$count\.html\" target=\"new\">  <IMG SRC=\"$pic\" width=\"$WIDTH\" height=\"$HEIGHT\" border=0/></A></TD>\t\t\t\t\n";
		if (($count%$NUM_PICS_EACH_ROW)==0) {
			print WRITE "\t\t\t\t</TR>\n\t\t\t\t<TR>\n";
			
		}
	}
}

print WRITE "\t\t\t\t</TR>
\t\t\t</TABLE>
\t\t</CENTER>
\t</BODY>
</HTML>";

print "\n           Done...Files created in: $ARGV[0]\n\n";

#=========================================================#
#														  #
# Function: Print the usage of this perl script and exit  #
#														  #
#=========================================================#

sub usage
{
	print "\n\n=======================================================================
Incorrect Usage: perl $0 directory width height pics_on_each_row
		 example: perl $0 c:\\pics 150 140 5\n
Note: by default width=150 height=140 num_pics_on_each_row=5\n
=======================================================================\n\n";

	exit;
}