#!/usr/bin/perl
use constant THUMBSIZE => 150;
use constant COLUMNS=>5;
use constant THUMBDIR=>".thumbs";
use constant INLINESIZE=> 600;
use constant INLINEDIR=>".inline";
use Image::Info qw(image_info dim);
use Cwd;
use Getopt::Std;

=head1 NAME

imagedir - generates HTML index for directory of pictures.

=head1 SINOPSIS

imagedir [-f] [B<-n>] [B<-l>] [B<-t> I<title>]

=head1 DESCRIPTION

Generates two hidden subdirs under current directory - B<.thumbs> and
B<.inlines> with smaller copies of the images and generates
B<index.html> with small (fit in the square 150x150) copies of the
images as table.

Each image is a link to generated HTML page with bigger (fit into
600x600) copy of the image and (unless supressed) link to full sized
image. If image contain JPEG or GIF comment, comment is inserted into
HTML.  

=head1 OPTIONS

=over 4

=item B<-f>
 
 Don't use filenames as headers of individual picture page

=item B<-l>

Supress link to fullsized images

=item B<-n>

If specified, all links in B<index.html> would have target="_blank"
attribute.

=item B<-t> I<string>

Title. If none given, directory name is used. 

=back

=cut

use vars qw($opt_t $opt_n $opt_l $opt_f);
getopts("t:lnf");

my $dir = $opt_t || (split ("/",cwd()))[-1];
mkdir THUMBDIR if (! -d THUMBDIR);
mkdir INLINEDIR if (! -d INLINEDIR);
my $i=0;
open OUT,">index.html";
print OUT "<HTML><HEAD><TITLE>$dir</TITLE>\n<BODY>\n<H1>$dir</H1>\n"
."<p align=\"center\"><A HREF=\"..\">back</A></p>". 
 "<TABLE CELLSPACING=10 CELLPADDING=0 BORDER=0>\n";
my @piclist=(<*.jpg>,<*.gif>,<*.png>) ;
print STDERR "@piclist\n";
my ($prev,$next);
for ($j=0;$j<=$#piclist;$j++) {
  $_ = $piclist[$j];
  $prev = $piclist[$j-1] if $j>0;
  if ($j<$#piclist) {
  $next = $piclist[$j+1];
  } else {
   $next = undef;
  } 
  print STDERR "$j:$_";
  my $info = image_info($_);
  my $thumbname=rescale($_,THUMBDIR,THUMBSIZE,$info);
  my $inlinename=rescale($_,INLINEDIR,INLINESIZE,$info);
  my $comment = make_comment_html($info);
  my ($w,$h) = dim(image_info(THUMBDIR."/$_"));
  print OUT "<tr>\n" if ($i % COLUMNS == 0);
  print OUT "<td valign=top align=center><a href=\"$_.html\"",($opt_n?"
  target=\"_blank\"":""),"><img border=0 width=$w height=$h src=\"$thumbname\"></a><br>$comment</td>\n";
  print OUT "</tr>\n" if (++$i % COLUMNS == 0); 
  print STDERR " html...";	
  make_html($_,$info,$comment,$inlinename);
  print STDERR "\b\b\b done"; 
  print STDERR "\n";	

}
print OUT "</tr>\n" unless ($i % COLUMNS == 0);
print OUT "</table></body></html>\n";
close OUT;

sub rescale {
  my ($name,$out_dir,$maxsize,$info) = @_;
  my ($w,$h) = dim($info);
  if ($w<$maxsize && $h<$maxsize) {
  	#picture is small enough to be used without rescaling
  	return $name;
  }	
  my $result = "$out_dir/$name";
  if ( ! -f $result || -M $name < -M $result) {
    print STDERR "  $out_dir...";

    system "convert", "-geometry", $maxsize."x".$maxsize,$_,$result;
    print STDERR "\b\b\b ";
  } 
  return $result;
}  

sub make_comment_html {
  my $info = shift;
my $comment =

	ref($info->{"Comment"})?join("\n",@{$info->{"Comment"}}):$info->{"Comment"};
	$comment =~s/\&/&amp;/;
	$comment =~s/"/&quot;/;
	$comment =~s/>/&gt;/;
	$comment =~s/</&lt;/;
	return $comment;
}	

sub make_html {
	my $imgfile = shift;
	my $info = shift;
	my $comment = shift;
	my $inline = shift;
	open HTML, ">$imgfile.html";
	my ($w,$h) = dim($info);
	my ($w1,$h1) = dim(image_info(INLINEDIR."/".$imgfile));
	print HTML "<html><head><title>$dir:$imgfile</title></head><body>
<h2>$dir</h2>",($opt_f?"":"<h1>$imgfile</h1>"),(defined($info->{'DateTime'})?"<p
class=\"timestamp\">Time: $info->{'DateTime'}</p>":""),"<p>$comment</p>",
"<img src=\"".$inline."\" width=$w1 height=$h1>",
"<br clear=all><hr>

<hr>
<table width=100%><tr><td align=left>
",($prev?"<a href=\"$prev.html\">&lt;&lt</a>":"&nbsp;"),
"</td><td align=center><a href=\".\">Up</A></td><td align=right>\n",
,($next?"<a href=\"$next.html\">&gt;&gt</a>":"&nbsp;"),
"</td></tr></table>\n",
($opt_l?"":"<a href=\"$imgfile\">$imgfile ($w x $h)</a>"),"
</body></html>
";
close HTML;
}

sub fix_html {
	my $imgfile = shift;
	unlink "$imgfile.html";
	make_html($imgfile);
}	
