#!/usr/bin/perl

use strict;

my $THUMBSIZE = "x100";
my $RESIZEVALUE = 650;
my $COLS = 4;
my $PERPAGE = $COLS * 3;

my $verbose = 1;
my $doimages = 1;

my $outputdir = 'OUTPUT';
my $imagesdir = "$outputdir/images";
my $thumbsdir = "$outputdir/thumbs";

my $title;

$| = 1;

# *****************************************************************************
my $HTML_BEGINPAGE  = "<html>\n<body text=FFFFFF bgcolor=000000 link=88FF88 vlink=88FF88 alink=88FF88>\n<center>\n<font face=\"Palatino Linotype\">\n"; 
my $HTML_ENDPAGE    = "</font>\n</center>\n</body>\n</html>\n";

my $HTML_BEGINTABLE = "<table cellspacing=4 cellpadding=0>\n";
my $HTML_ENDTABLE   = "</table>\n";
my $HTML_BEGINROW   = "<tr>\n";
my $HTML_ENDROW     = "</tr>\n";

my $HTML_BEGINCELL  = "<td align=center valign=center>\n";
my $HTML_ENDCELL    = "</td>\n";

my $HTML_BLANKCELL  = "$HTML_BEGINCELL &nbsp; $HTML_ENDCELL\n";
# *****************************************************************************

sub makeinfofile
{
	my @filelist = <*.*>;

	open INFO,"> wginfo";

	print INFO "Web Gallery\nx100 650 4 3\n";
	
	for(sort @filelist)
	{
		print INFO "$_ \n";
	}
	
	close(INFO);
}

sub generatethumb
{
	# Image Filename, Thumb Filename
	my ($ifn,$tfn) = @_;

	print "Generating thumb ($tfn)..." if($verbose);
	my $cmd = `convert -resize $THUMBSIZE $ifn $tfn`;
	print "Done.\n" if($verbose);
}

sub generateimage
{
	# this will either just copy the file or move it, depending on the rotate value
	
	my ($ofn,$ifn,$rotate) = @_;
	my $rotatenumber = 0;

	$rotatenumber = 270 if($rotate eq 'l');
	$rotatenumber = 90  if($rotate eq 'r');

	my $resizepart;
	$resizepart = "-resize $RESIZEVALUE" if($RESIZEVALUE > 0);

	print "Generating image ($ifn)..." if($verbose);

	if($rotatenumber)
	{
		# use convert to make the ifn
		my $cmd = `convert -rotate $rotatenumber $resizepart $ofn $ifn`;
	}
	else
	{
		if($resizepart)
		{
			my $cmd = `convert $resizepart $ofn $ifn`;
		}
		else
		{
			my $cmd = `cp $ofn $ifn`;
		}
	}

	print "Done.\n" if($verbose);
}

sub makesingle
{
	my ($dir,$last,$up,$next,$current,$description) = @_;

	open SINGLEPAGE, "> $dir/$current.html";

	print SINGLEPAGE $HTML_BEGINPAGE;
	print SINGLEPAGE "<b>$title</b><br>\n";
	
	print SINGLEPAGE "<a href=\"$last.html\">Back</a> - \n" if($last);
	print SINGLEPAGE "<a href=\"$up\">Up</a> - \n";
	print SINGLEPAGE "<a href=\"$next.html\">Forward</a>\n\n" if($next);
	print SINGLEPAGE "<br><br>$description\n";
	my $width = $RESIZEVALUE;
	$width = 100 unless($RESIZEVALUE);
	print SINGLEPAGE "<hr size=1 width=$width>";
	print SINGLEPAGE "<BR>";
	if($next)
	{
	print SINGLEPAGE "\n<a href=\"$next.html\"><img src=\"images/$current\" border=0></a>\n";
	}
	else
	{
	print SINGLEPAGE "\n<a href=\"index.html\"><img src=\"images/$current\" border=0></a>\n";
	}

	print SINGLEPAGE $HTML_ENDPAGE;
	close(SINGLEPAGE);
}

if(@ARGV < 1)
{
	print "wg (makeinfo|go)\n";
	exit;
}

my $action = shift @ARGV;

if($action eq 'makeinfo')
{
	print "Creating Info File...";
	makeinfofile();
	print "Done.\n";
	exit;
}

unless($action eq 'go')
{
	print "wg (makeinfo|go)\n";
	exit;
}

{
	mkdir($outputdir);
	mkdir($imagesdir);
	mkdir($thumbsdir);
}

my $totalimages = 0;
open INFO,'< wginfo' || die;
my $skiptitle = <INFO>;
$skiptitle = <INFO>; # skip options too
while(<INFO>)
{
	$totalimages++ if(/^(?:. )?([^ ]+)\s+(.*)$/);
}

close(INFO);

open INFO,'< wginfo' || die;

$title = <INFO>;
print "Gallery Title: $title\n";
my $options = <INFO>;

if($options =~ /^\s*(\S+)\s+(\d+)\s+(\d+)\s+(\d+)\s*$/)
{
	$THUMBSIZE = $1;
	$RESIZEVALUE = $2;
	$COLS = $3;
	$PERPAGE = $COLS * $4;
	print "Updated Options.\n";
}

my $nextidx = 3;
my $l_idxfn;
my $c_idxfn = 'index.html';
my $n_idxfn = 'index2.html';

my $imagecount = 0;

# ** FOR SINGLE PAGES **
my $single_last;
my $single_current;
my $single_next;
my $single_up;
my $single_up_next;
my $single_desc;
my $single_desc_next;
# ** FOR SINGLE PAGES **

open CURIDX,"> $outputdir/$c_idxfn";

print CURIDX $HTML_BEGINPAGE;
print CURIDX "<b>$title</b><br><br>\n";

if($totalimages > $PERPAGE)
{
	print CURIDX "<a href=\"$n_idxfn\">Forward</a><br>\n\n";
}

print CURIDX "<br>";
print CURIDX $HTML_BEGINTABLE;
print CURIDX $HTML_BEGINROW;

while(<INFO>)
{
	my ($ofn,$desc,$ifn,$tfn);
	my $rotate = 'n';

	if(/^(.) /)
	{
		$rotate = $1;
	}

	if(/^(?:. )?([^ ]+)\s+(.*)$/)
	{
		$ofn = $1;
		$desc = $2;
		$ifn = "$imagesdir/$ofn";
		$tfn = "$thumbsdir/$ofn";

		# *****
		print CURIDX $HTML_BEGINCELL;
		print CURIDX "<a href=\"$ofn.html\"><img src=\"thumbs/$ofn\" border=0></a>\n";
		print CURIDX $HTML_ENDCELL;

		# ** GENERATE SINGLE IMAGE PAGE **

		$single_last = $single_current;
		$single_current = $single_next;
		$single_next = $ofn;
		
		$single_up = $single_up_next;
		$single_up_next = $c_idxfn;

		$single_desc = $single_desc_next;
		$single_desc_next = $desc;

		if($single_current)
		{
			makesingle($outputdir,$single_last,$single_up,$single_next,$single_current,$single_desc);
		}
		# ** GENERATE SINGLE IMAGE PAGE **
		
		$imagecount++;
		$totalimages--;

		unless($imagecount % $COLS)
		{
			print CURIDX $HTML_ENDROW;

			if($imagecount < $PERPAGE)
			{
				print CURIDX $HTML_BEGINROW;
			}
			else
			{
				print CURIDX $HTML_ENDTABLE;
				print CURIDX $HTML_ENDPAGE;

				close(CURIDX);

				$l_idxfn = $c_idxfn;
				$c_idxfn = $n_idxfn;
				$n_idxfn = "index$nextidx.html";
				$nextidx++;
				open CURIDX,"> $outputdir/$c_idxfn";
				$imagecount = 0;

				print CURIDX $HTML_BEGINPAGE;
				print CURIDX "<b>$title</b><br><br>\n";

				print CURIDX "<a href=\"$l_idxfn\">Back</a> \n";
				if($totalimages > $PERPAGE)
				{
					print CURIDX " - <a href=\"$n_idxfn\">Forward</a>\n\n";
				}
				
				print CURIDX "<br><br>\n";
				print CURIDX $HTML_BEGINTABLE;
				print CURIDX $HTML_BEGINROW;
			}
		}
		# *****

		if($doimages)
		{
			generateimage($ofn,$ifn,$rotate);
			generatethumb($ifn,$tfn); # uses the potentially rotated file :-p
		}
	}
}

while($imagecount % $COLS)
{
	print CURIDX $HTML_BLANKCELL;
	$imagecount++;
}

print CURIDX $HTML_ENDROW;
print CURIDX $HTML_ENDTABLE;
print CURIDX $HTML_ENDPAGE;

$single_last = $single_current;
$single_current = $single_next;
$single_next = "";

$single_up = $single_up_next;
$single_desc = $single_desc_next;

if($single_current)
{
	makesingle($outputdir,$single_last,$single_up,$single_next,$single_current,$single_desc);
}
