#!/usr/bin/perl -w

use strict;
use DirHandle;
use CGI;
use Image::Size;

use vars qw ($TOP_URL $TOP_DIR %PHOTOGRAPHER);
use vars qw ($query $show @images $num $caption $copyright $size);

$TOP_DIR = "/web/www.strictlybluegrass.com/www/2004/gallery";
$TOP_URL = "/2004/gallery";
%PHOTOGRAPHER = (
    'jb'	=> 'jay blakesberg',
    'rb'	=> 'rachel bleckman',
    'mh'	=> 'matt hartenstein',
);


sub file2caption
{
    my $file = shift;
    my $caption = '';
    my $t;

    $file =~ s/^.*\///g;
    $file =~ s/-.+/ /g;
#    $file =~ s/=(.+)/ - $1/;
    $file =~ s/=(.+)//;
    $file =~ s/\.(jpg|jpeg|gif)$//gi;
    $file =~ s/_/ /g;

    foreach $t ( split (/\s/, $file) )
    {
	if ( $caption ) { $caption .= " "; }
	$caption .= ucfirst ($t);
    }

    $caption =~ s/(\s+)and(\s+)/$1&amp;$2/gi;
    return $caption;
}

sub file2copyright
{
    my $file = shift;
    my $copyright = '';
    my $who = 'jb';

    if ( $file =~ /.+-([a-zA-Z]+)\.(jpg|jpeg|gif|pnm)/i )
	{ $who = $1; }

    $copyright = $PHOTOGRAPHER{$who};
    return $copyright;
}

###
## load_images() - reads the directory tree and creates the image array
##   of images to display
###
sub load_images
{
    my($dir) = shift;
    my($d, @dirs, $pred, $dh, $file);

    if ( !defined ($dh = new DirHandle ($dir)) )
    {
	print "<P><B>Error:</B> Can't access \"$dir\": $!</P>\n";
	return 0;
    }

    if ( $dir ne "." ) { $pred = "$dir/"; } 
    else { $pred = ""; }

    ## avoid silly URLS like http://www.foo.bar/blip/lwpg/./running
    $pred =~ s/^\.\///g;

READ_LOOP:
    while ( defined ($file = $dh->read) )
    {
#	print "<P>GOT $dir [$file]</P>\n";
	if ( $file eq "." || $file eq ".." ) { next; }

	if ( -d "$dir/$file" ) 
	{
	    push (@dirs, "$dir/$file");
	    next;
	}

	if ( $file =~ /^(.+)\.(gif|jpg|jpeg)$/i )
	{
	    my($f, $s);
	    $f = $1;
	    $s = $2;

	    push (@images, "${pred}${f}.${s}");
	}
    }

    undef $dh;

    foreach $d ( @dirs )
	{ undef $d; }

    return 1;
}

$query = new CGI;
$show = $query->param('show');
$num = $query->param('num');

print $query->header;

chdir $TOP_DIR || die "<P><B>Error:</B> Can't change to photo dir</P>\n";

if ( ! $show )
{
    print "<P><B>Error:</B> No folder specified</P>\n";
    exit 1;
}

if ( load_images ($show) == 0 )
{
    exit 1;
}

if ( scalar (@images) < 1 )
{
    print "<P><B>Error:</B> No images found in $show</P>\n";
    exit 1;
}

@images = sort (@images);

if ( ! $num )
    { $num = 0; }

$caption = file2caption ($images[$num]);
$copyright = file2copyright ($images[$num]);
$size = Image::Size::html_imgsize($images[$num]);

printf <<_EOF_, $caption;
<HTML>
<HEAD>
<TITLE>HSBF 2004: %s</TITLE>
_EOF_

if ( $num+1 <= $#images )
{
    printf <<_EOF_,  sprintf ("%s/%s", $TOP_URL, $images[$num+1]);
<SCRIPT LANGUAGE="JavaScript">
<!--
var img = new Image ();
img.src = "%s";
// -->
</SCRIPT>
_EOF_
}

printf <<_EOF_, ucfirst ($show);
</HEAD>

<BODY BGCOLOR="#C8C296" TEXT="#4D1C0E">
<FONT FACE="helvetica,arial" SIZE=2>
<DIV ALIGN="center">
<DIV STYLE="border: 1px solid #000000; padding: 5px">
<B>Hardly Strictly Bluegrass Festival 2004 - %s Photo Gallery</B>
</DIV>
_EOF_

my $prev_str = '&lt;&lt;';

if ( $num > 0 )
{
    $prev_str = sprintf "<A HREF=\"$ENV{SCRIPT_NAME}?show=%s&num=%d\">&lt;&lt;</A>\n",
	$show, $num - 1;
}

my $next_str = '&gt;&gt;';

if ( $num+1 < scalar (@images) )
{
    $next_str = sprintf "<A HREF=\"$ENV{SCRIPT_NAME}?show=%s&num=%d\">&gt;&gt;</A>\n",
	$show, $num + 1;
}

print <<_EOF_;
<FORM METHOD="GET" ACTION="$ENV{SCRIPT_NAME}" STYLE="margin-top: 0px; margin-bottom: 0px;">
<INPUT TYPE="hidden" NAME="show" VALUE="$show">
<TABLE BORDER=0 CELLSPACING=3 CELLPADDING=0>
<TR>
    <TD ALIGN="left"><FONT FACE="helvetica,arial" SIZE=2>$prev_str</FONT></TD>
    <TD>
    <SELECT NAME="num"
	onChange="window.open('$ENV{SCRIPT_NAME}?show=$show&num=' + this.options[this.selectedIndex].value, '_top');">
_EOF_

my $i;
for ( $i = 0; $i < scalar (@images); $i++ )
{
    printf "<OPTION VALUE=\"%d\" %s>#%d %s\n",
	$i,
	$i == $num ? " selected" : "",
	$i + 1, file2caption ($images[$i]);
}

print <<_EOF_;
    </SELECT>
    </TD>
    <TD ALIGN="right"><FONT FACE="helvetica,arial" SIZE=2>$next_str</FONT></TD>
</TR>
</TABLE>
_EOF_

print <<_EOF_;
<FONT SIZE=3><B>$caption</B></FONT>
<P>
<TABLE CELLPADDING="10" BORDER=0 CELLSPACING="3"><TR>
    <TD BGCOLOR="#000000"><IMG
	ALT="$images[$num]"
	$size
	SRC="${TOP_URL}/$images[$num]"></TD>
</TR></TABLE>
<FONT SIZE=2 FACE="helvetica,arial">&#169;2004 
<A TARGET="new" HREF="/">hardly strictly bluegrass festival</A>
- photo by $copyright</FONT>
</P>
<P>
<FONT SIZE=1>
[ <A HREF="javascript:window.close();">close window</A> ]
</FONT>
</P>
_EOF_

print <<_EOF_;
</FORM>
</DIV></FONT>
_EOF_
