#!/usr/bin/env perl
use strict;
use warnings;

print "Content-type:text/html\n\n";

print <<header;
<html><head><META HTTP-EQUIV="content-type" CONTENT="text/html; charset=ISO-8859-1"><title>SK | Pictures</title>
<style><!--
body,td,a,p,.h{font-family:arial,sans-serif;} .h{font-size: 20px;} .h{color:#008000} .q{text-decoration:none; color:#0000cc;}
//--></style>

<script>
<!--
if (window != top) top.location.replace(location.href);
// -->
</script>

</head><body bgcolor='#ffffff' text='#000000' link='#0000cc' vlink='#0000cc' alink='#ff0000'><center>
<img src="../pics/logo.gif"><br>

<br>
<table border=0 cellspacing=0 cellpadding=0>
<tr>
  <td width=15>&nbsp;</td>
  <td id=0 bgcolor='#efefef' align=center width=95 nowrap onClick="document.location='../index.html';" style=cursor:pointer;cursor:hand;><a id=0a class=q href="../index.html"><font size=-1>SK</font></a></td>
  <td width=15>&nbsp;</td>
  <td id=1 bgcolor='#A00000' align=center width=95 nowrap onClick="document.location='../pics/';" style=cursor:pointer;cursor:hand;><a id=1a class=q href="../pics/"><font size=-1 color="#FFFFFF"><b>Pictures</b></font></a></td>
  <td width=15>&nbsp;</td>
  <td bgcolor='#efefef' align=center width=95 nowrap onClick="document.location='http://pages.cs.wisc.edu/~kronenfe/personal.html';" style="cursor:pointer;cursor:hand;"><a class="q" href="http://pages.cs.wisc.edu/~kronenfe/personal.html"><font size=-1>Other</font></a></td>
  <td width=15>&nbsp;</td>
</tr>
<tr><td colspan=12 bgcolor='#A00000'><img width=1 height=1 alt=""></td>
</tr>
</table>

<p><font color='#A00000'><b>Photo Albums</b></font><p>
header


my $WIDTH = 3;
if(defined($ENV{'QUERY_STRING'}) && $ENV{'QUERY_STRING'} && (-e "/u/k/r/kronenfe/public/html/pics/$ENV{'QUERY_STRING'}")) {
    show_album($ENV{'QUERY_STRING'});
}
else {
    dienice("Invalid album supplied");
}

sub show_album {
    my $dir = shift;

    if($dir) {
	opendir(DIR, "/u/k/r/kronenfe/public/html/pics/$dir/thumbs") or dienice("Could not open thumbs directory: $!");
	my @files = readdir(DIR);
	closedir(DIR);
	
	print "<table cellpadding='10px' cellspacing='5px' border='0'>\n";
	my $cnt = 0;
	foreach my $file (sort(@files)) {
	    next if($file !~ /^_/ );

	    # Form the original file name
	    (my $mfile = $file) =~ s/^_//;

	    my $caption = "&nbsp;";  # Caption to display under images

	    if($mfile =~ /^MOV_/) {
                $mfile =~ s/\.jpg/\.mov/;
                $caption = "<font size='-1'>Movie [quicktime]</font>";
            }
	    if($mfile =~ /^AVI_/) {
                $mfile =~ s/\.jpg/\.avi/;
                $caption = "<font size='-1'>Movie [quicktime]</font>";
            }
	    
	    if($cnt == 0) {
		print "<tr align=center>\n";
	    }
	    
	    print "<td><a href='http://pages.cs.wisc.edu/~kronenfe/pics/$dir/$mfile'><img src='http://pages.cs.wisc.edu/~kronenfe/pics/$dir/thumbs/$file' border=0></a><br>$caption</td>\n";
	    
	    if($cnt == ($WIDTH-1)) {
		print "</tr>\n";
	    }
	    
	    $cnt = ($cnt+1) % $WIDTH;
	}
	print "</table>\n";
    }
    else {
	print "<p>Invalid directory supplied";
    }
}

sub dienice {
    print "<h2>Error</h2><br>", $_[0], "</body></html>\n";
    exit;
}
