#! perl -w
#!/usr/bin/perl -wT

# Script to generate BPAS photo album page.

# WARNING: ENVIROMENT SETTINGS ....
# For use on local PC:
#    use #! perl line
# For use on BPAS web:
#    use #! /usr/bin/perl line
#
# Copyright (C) 2007 Craig Nicholas

# This program is free software; you can redistribute it and/or modify it under the terms of the GNU 
# General Public License as published by the Free Software Foundation; either version 2 of the 
# License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
# See the GNU General Public License for more details.

# You should have received a copy of the GNU General Public License along with this program; 
# if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA 02111-1307 USA
#
# Craig Nicholas
# Website: www.britishplate.org.uk
#

use strict;
use CGI::Carp qw(fatalsToBrowser warningsToBrowser set_message);
use CGI qw/:standard *div/;
use File::Basename;


# relative URL root for generated web pages
my $docRoot="";
my $urlRoot="";
if ($ENV{'DOCUMENT_ROOT'} eq 'C:/WebSites') {
	$urlRoot="../bpas"; # value for local server
	$docRoot="../bpas"; # relative file path from cgi dir
} 
else {
	$urlRoot=".."; # value for web server
	$docRoot="../www"; # relative file path from cgi dir
}

# locations of files
my ($albumBase, $albumList, $configFileName) = (
	"$docRoot/albums",
	"$docRoot/albums/albumList.cfg",
	"album.cfg"
	);
	
my ($albumDir, $albumSubDir, $title, $desc, %albumTags);

my $q = new CGI;

############################
# START OF MAIN PROCESSING #
############################

set_message("For help, please send mail to the webmaster (craig.nicholas\@britishplate.org.uk), giving this error message and the time and date of the error");

print $q->header();
warningsToBrowser(1);

# get the album to display passed on the 'album' query parameter
open (ALBUM_LIST, $albumList) || croak ("Unable to open album list. $albumList: $!");
my $albumKey=$q->param('album') || "";

while (<ALBUM_LIST>) {
	chomp;
	my ($name,$dir)=split(/~/, $_);
	if ($name eq $albumKey) {
		$albumDir="$albumBase/$dir";
		$albumSubDir=$dir;
		last;
	}
}
croak ("album '$albumKey' does not exist") if (!defined $albumDir);
	
close (ALBUM_LIST);

# get the albums tags file
open (ALBUM_CFG, "$albumDir/$configFileName") || croak ("failed to open config file '$albumDir/$configFileName': $!");

while (<ALBUM_CFG>) {
	chomp;
	my ($key,$value) = split (/~/, $_);
	$albumTags{$key}=$value;
}

close (ALBUM_CFG);

# get all the images
my @imageArr=glob("$albumDir/*.jpg");
push (@imageArr, glob("$albumDir/*.JPG"));

# Print the html and standard BPAS headers

print $q->start_html({-title=>"$albumTags{'title'}", 
	  				  -dtd=>1,
					  -style=>{'src'=>"$urlRoot/bpasstyles.css"},
						meta=>{GENERATOR=>'perl CGI',
                               keywords=>'generated by perl',
					   		   description=>'BPAS photo album page'}});	

print $q->start_div({-class=>'header'});
print "<p><a href=\"$urlRoot/index.htm\" target=\"_parent\"><img src=\"$urlRoot/images/BPASbanner.jpg\" alt=\"BPAS banner [Click to go to home page]\"></a></p>";
print $q->end_div();
	  
# start of main photo area
print $q->start_div({-class=>'photoPage'});
print $q->h1($albumTags{'title'}),
 	  $q->p($albumTags{'desc'});	
 	  
if (-d "$albumDir/high") {
	print $q->p("(Click on the bordered images to see a high-res version in seperate window)");
}

# now go through image list and create the page			
foreach (@imageArr)
{
	my $filename = basename($_);
	# skip the thumbnail
	next if ($filename eq "thumb.jpg");

	# Print a caption if defined
	if (defined $albumTags{$filename . '.caption'}) {
		print $q->p({-class=>'photo_caption'},$albumTags{$filename . '.caption'});
	}
	# Print the image
	my $imgUrl = $urlRoot . '/albums/' . $albumSubDir . '/' . $filename;
	
	if (-d "$albumDir/high" && -e "$albumDir/high/$filename")
	{
		# include high res link
		my $imgHighUrl = $urlRoot . '/albums/' . $albumSubDir . '/high/' . $filename;
		
		print $q->a({href=>$imgHighUrl,
					 target=>"_blank"}, 
					 $q->img({-src=>$imgUrl,
					 		   -border=>"2",
					   		   -alt=>"$filename. Click for high res version",
					   		   -title=>"$filename. Click for high res version"})
					  );
		print $q->br();
	}
	else {
		# no high res link
		print $q->img({-src=>$imgUrl,-alt=>$filename,-title=>$filename});
		print $q->br();
	}
	
	# Print the image description if defined
	if (defined $albumTags{$filename . '.desc'}) {
		print $q->p($albumTags{$filename . '.desc'});
	}
	
}


#foreach $a (keys(%ENV)) {
#	print "$a=$ENV{$a}\n";
#}

# end the photo div and the html
print $q->end_div();
print $q->end_html();

