use Image::Size;

my $fname = "JPGfilelist.txt";
###  File should be text formatted as such:
#    title:Title Name For Every Page
#    filename.jpg:Description

open (IN, $fname) || die "can't open $fname\n";

my $prevpic = undef;
my $settitle = "";

while (<IN>) {
    chomp;
    next if /^#/;
    my ($picname, $pictext) = split (/:/,$_);

    $settitle = $pictext and next
	if ($picname eq "title");

    my $picroot = $picname;
    $picroot =~ s/\.JPG//i;

    # make list of pictures, save their captions and real names
    push @pics, $picroot;
    $captions{$picroot} = $pictext;
    $longname{$picroot} = $picname;

    # if it doesn't have a thumbnail we have to make one
    make_thumbnail($picname)
	unless -e "thumb_$picroot.jpg";

    # make the next-and-prev relationships
    if (defined($prevpic)) {
	$prev{$picroot} = $prevpic;
    }
    else { $prev{$picroot} = "index" };
    $next{$prevpic} = $picroot
	unless $prevpic eq "";
    $prevpic = $picroot;
}

# set the last individual page to have its next go to the index page
$next{$prevpic} = "index";


#####  Making the index page for the set with all thumbnails
open OUT, ">index.html" || die "can't open index.html $!\n";

print OUT "<HTML><HEAD>\n";
print OUT "<TITLE>$settitle</TITLE>\n";
print OUT "</HEAD>\n";
print OUT "<BODY BGCOLOR=\"#FFFFFF\"> <div align=\"center\"> <FONT FACE=\"Tahoma\" Size=\"+2\" Color=\"#000000\"><p>$settitle</p></FONT>\n";

print OUT "<TABLE border=0 width=500 cellpadding=8 cellspacing=8><TR>\n";

my $count = 0;
for (@pics) {
    my $picroot = $_;
    my $pictext = $captions{$picroot};
    my $picname = $longname{$picroot};
    print OUT "<TD valign=\"middle\" align=\"center\" bgcolor=\"#EEEEEE\"><FONT FACE=\"Tahoma\" Size=\"-1\" Color=\"#000000\"><a href=\"$picroot.html\"><img src=\"thumb_$picroot.jpg\" border=\"0\" ></a><br>$pictext</FONT></TD>\n";
    $count++;
    if ($count eq 4) {
	$count = 0;
	print OUT "</TR><TR>\n";
    }
}
close OUT;


##### Making individual picture pages
for (@pics) {

    my $filename = "$_.html";
    open OUT, ">$filename" || die "can't open $filename $!\n";

    $nextpagename = "$next{$_}.html";
    $prevpagename = "$prev{$_}.html";
    my $nextlink = "";
    my $prevlink = "";

    # no next link if it's the last page
    $nextlink = "<a href=\"$nextpagename\">Next</a>"
	unless ($nextpagename eq "index.html");

    # prev part should link Prev and Index, or just Index if first page
    if ($prevpagename eq "index.html") {
	$prevlink = "<a href=\"index.html\">Index Page</a>&nbsp;";
    }
    else {
	$prevlink = "<a href=\"$prevpagename\">Previous</a>&nbsp;<a href=\"index.html\">Index Page</a>&nbsp;";
    }

    print OUT "<HTML><HEAD>\n";
    print OUT "<TITLE>$settitle</TITLE>\n";
    print OUT "</HEAD>\n";
    print OUT "<BODY BGCOLOR=\"#FFFFFF\"> <div align=\"center\"> <FONT FACE=\"Tahoma\" Size=\"+2\" Color=\"#000000\"><p>$settitle</p></FONT>\n";
    print OUT "<TABLE border=0 width=500 cellpadding=8 cellspacing=8><TR><TD align=\"center\"><FONT FACE=\"Tahoma\" Size=\"-1\" Color=\"#000000\"><p>$prevlink$nextlink</p></FONT></td></tr></TABLE>\n";
    print OUT "<TABLE border=0 width=500 cellpadding=8 cellspacing=8><TR>\n";
    print OUT "<TD align=\"center\" bgcolor=\"#EEEEEE\"><img src=\"$longname{$_}\">\n";
    print OUT "<p><FONT FACE=\"Tahoma\" Size=\"-1\" Color=\"#000000\">$captions{$_}</font></p>\n";
    print OUT "</td></tr></table>\n";
    print OUT "<FONT FACE=\"Tahoma\" Size=\"-1\" Color=\"#000000\"><p>(c) Deanna Rubin 2009.</p></FONT>\n";
    print OUT "</BODY></HTML>\n";

    close OUT;
}


##### Making thumbnail files using mogrify
#  We want them to be a max of 150 x 150
sub make_thumbnail {

    my $picname = shift;
    my $picroot = $picname;
    $picroot =~ s/\.JPG//i;

    my ($width, $height) = imgsize($picname);
    return unless $width;

    print "making thumbnail for $width x $height $picname\n";
    my ($thumbheight, $thumbwidth);
    if ($width > $height) {
	$thumbwidth = 150;
	$thumbheight = (150 * $height) / $width;
    }
    else {
	$thumbheight = 150;
	$thumbwidth = int ((150 * $width) / $height);
    }
    my $thumbname = "thumb_$picroot.jpg";

    system "cp $picname $thumbname";
    system "mogrify -resize ${thumbwidth}x$thumbheight $thumbname";
}
