#!/usr/bin/perl

$thumbdir = "thumbnails/";
$descfile = "desc.txt";

open FILE, ">index.html" || die "Can't open index.html: $!\n";
@title = `cat "$descfile"`;
print FILE "<html><head><title>@title</title></head>" ;
print FILE "<body bgcolor=\"white\" link=\"blue\" vlink=\"blue\">" ;
print FILE "<table align=center cellpadding=9>" ;
print FILE "\n";

`rm -rf "$thumbdir"`;

@list = `ls *.JPG *.jpg`;
for( $i = 0; defined $list[$i]; $i++ ) {
	chomp $list[$i];
	if( $i % 4 == 0 ) {
		print FILE "<tr>";
	}
	print FILE "<td>";
	print FILE "\n";

	`jhead -autorot "$list[$i]" > /dev/null`;
	mkdir("$thumbdir");
	$foo = `jhead -st /dev/null "$list[$i]" 2>&1 | grep -c "contains no thumb"`;
	if( $foo == 1 ) {
		`cp "$list[$i]" "$thumbdir"`;
		`cd "$thumbdir" ; mogrify -geometry 160x160 "$list[$i]"`;
	} else {
		`jhead -st "$thumbdir/$list[$i]" "$list[$i]"`;
	}
	print FILE "<a href=\"$list[$i]\"><img src=\"$thumbdir/$list[$i]\"></a>\n";
#	print FILE "<a href=\"$list[$i]\"><img src=\"show.cgi?$list[$i]\"></a>\n";
	
	print FILE "</td>";
#	print FILE "<td><font size=-3><pre><!--#exec cmd=\"/usr/bin/jhead $list[$i]\" --></pre></font></td>";
	@exifinfo = `jhead "$list[$i]"`;
	print FILE "<td><pre>@exifinfo</pre></td>";

	if( $i % 2 == 1 ) {
		print FILE "</tr>";
	}
	print FILE "\n";
}

print FILE "</table></body></html>";
close FILE;
