#!/usr/bin/perl
##
# License: this script is copyright 2000
# D. Scott Barninger <barninger@cvn.net>
# Copying freely permitted under the GNU license

# This script will scan a specified directory for images
# and produce a thumbnail page containing all images in
# the directory.
# usage:  thumb_page.cgi?directory_name
# directory_name must be a sub-directory of $root-path below.

#-----------------------------------
# script configuration
#-----------------------------------

# set paths
# set the paths here to the directory you want to operate
# from. Place the photo dirs under this.
#
# $full_path = $ENV{DOCUMENT_ROOT}.'/cgi-bin/thumbpage/';
$full_path = $ENV{DOCUMENT_ROOT};
$url_location = 'http://www.hier-luebeck.de/cgi-bin/thumbpage/';
$script_name = 'thumbpage.cgi';

#####
# $full_path = "/home/www/www.hier-luebeck.de/"; # this is full path from root
$root_path = "http://www.hier-luebeck.de"; # this is the dir from document root that you scan

# set how many cells you want displayed in each table row
$cells = "2";

#-------------------------------------
# end of script configuration
#-------------------------------------

# if we don't get a subdirectory name as ENV[QUERYSTRING] then die
if(! $ARGV[0]) {&HTMLdie("Sie haben kein Verzeichniss angegeben:  thumbpage.cgi?directory");}

# Initialize the table cell counter
$counter = 0;

# print page header
print "Content-type: text/html\n\n";
print "<HTML>";
print "<HEAD><TITLE>Bildervorschau</TITLE></HEAD>";
print "<BODY>";

print "<TABLE border=\"1\" cellspacing=\"5\" align=\"center\"><TR>";
print "<tr>";
print "<td class=\"head\" bgcolor=\"#FFFFCC\" width=\"191\" bordercolor=\"#FFFFCC\">";
print "<p align=\"center\"><img border=\"0\" src=\"/luebeck/7eins_Logo.jpg\" width=\"150\" height=\"45\"></td>";
print "<td class=\"head\" bgcolor=\"#FFFFCC\" width=\"191\" bordercolor=\"#FFFFCC\">";
print "<p align=\"center\"><img border=\"0\" src=\"/luebeck/hier_luebeck_logo.png\" width=\"155\" height=\"155\"></td>";
print "<td class=\"head\" bgcolor=\"#FFFFCC\" width=\"190\" bordercolor=\"#FFFFCC\">";
print "<p align=\"center\"><img border=\"0\" src=\"/luebeck/luebeck2000_Logo.jpg\" width=\"150\" height=\"45\"></td>";
print "</tr></table></tr>";

print "<form action=\"\">";
print "<p align=\"center\"><input type=\"button\" value=\"Bilderbogen schliessen\" onClick=\"self.close()\"></p>";
print "</form>";

print "<TABLE border=\"1\" cellspacing=\"5\" align=\"center\"><TR>";

# open a pipe for input
open(LS, "ls $full_path/$ARGV[0] |");
# print "$full_path/$ARGV[0]";

# read files and print links
while ($dirlist=<LS>){
	chomp($dirlist);
	@buffer = split(/\n/,$dirlist);
	foreach $full_file (@buffer) {
		if($full_file ne "." && $file ne ".." ) {
			print "<TD><CENTER><A HREF = \"$root_path/$ARGV[0]/$full_file\">";
			# Grösse ändern ?
			print "<IMG SRC=\"$root_path/$ARGV[0]/$full_file\" border=\"0\"></A>";
			print "<BR><SMALL>$full_file</SMALL>";
			print "</CENTER></TD>";
			$counter++;
			if($counter eq $cells) {
				print "</TR><TR>";
				$counter = 0;
			}
		}
	}
}

#close pipe
close(LS);

print "</TR></TABLE>";

print "<form action=\"\">";
print "<br>";
print "<p align=\"center\"><input type=\"button\" value=\"Bilderbogen schliessen\" onClick=\"self.close()\"></p>";
print "</form>";

print "</BODY>";
print "</HTML>";

sub HTMLdie {
	local($msg,$title)= @_ ;
	$title || ($title= "CGI Error") ;
	print "Content-type: text/html\n\n";
	print <<EOF ;
	<html>
	<head>
	<title>$title</title>
	</head>
	<body>
	<h1>$title</h1>
	<h3>$msg</h3>
	<form>
	<input type=button name="BackButton" value="<-Back" id="Button1" onClick="history.back()">
	</form>
	</body>
	</html>
EOF
	
	exit ;
}
