#!/bin/sh
if [ -n "$1" ]; then cd "$1"; fi
export title=`basename $PWD`
find . -name '*.png' -or -name '*.jpg' -or -name '*.jpeg' -or -name '*.gif' |
  LC_ALL=C sort |
perl -ne '
	BEGIN {
		print "<html><head><title>$ENV{title}</title></head><body>\n<h1>$ENV{title}</h1>\n";
		$number = 1;
	}
	chomp;
	s{^\./}{};
	$image = $_;
	($html = $_) =~ s/\.[^.]*$/.html/;
	print qq{<a href="$html">$number</a><br>\n};
	if (defined $last_html) {
		make_html_page($number-1, $last_html, $last_image, $html);
	}
	++$number;
	$last_html = $html;
	$last_image = $image;
	END {
		print "</body></html>\n";
		if (defined $last_html) {
			make_html_page($number-1, $last_html, $last_image);
		}
	}
	sub make_html_page {
		my ($number, $html, $image, $next_html) = @_;
		open HTML, ">$html" or die "cannot create file: $html\n";
		print HTML "<html><head><title>$ENV{title} $number</title></head><body>\n";
		print HTML qq{<a href="$next_html"><img src="$image" border="0"></a>\n};
		print HTML "</body></html>\n";
		close HTML;
	}
' >index.html
