#!/usr/local/bin/perl
use strict;

my $job = $ENV{'QUERY_STRING'};

print "Content-type: text/html\n\n";
print "<HTML>", "\n";

use constant PAGESIZE => 16;
use constant ROWSIZE => 4;
use constant THMBSIZE => 200;

my $picdir = '';
my $page = 1;
if ($ENV{'QUERY_STRING'} && $ENV{'QUERY_STRING'} =~ /^([^&]+)/) 
  {
   $picdir = $1;
  }
if ($ENV{'QUERY_STRING'} && $ENV{'QUERY_STRING'} =~ /^[^&]+\&(\d+)$/) 
  {
   $page = $1;
  }

if ($picdir)
  {
   my $title = $picdir;
   my $desc = '';
   my $ldir = 'webpics/' . $picdir;
   if (-e "$ldir/desc.txt")
     {
      my $desctext = `cat $ldir/desc.txt`;
      ($title,$desc) = $desctext =~ m/([^\n\r]+)(.*)/s;
      $title =~ s/=:=(-?\d+)$//;
     }
   print "<HEAD><TITLE>$title - page $page" .
      "</TITLE></HEAD>\n";
   print "<BODY><H1>$title</H1>\n";
   print "<p>$desc</p>", "\n";
   my @piclist;
   my @opics;
   if (-d $ldir)
     {
      foreach my $picture (`ls -1 $ldir`)
        {
         chomp $picture;
         unless ($picture =~ /.txt$/i || $picture =~ /\_THMB\.\w+$/ ||
            $picture =~ /.db$/i)
           {
            if ($picture =~ /^dsc/i)
              {
               push @piclist, $picture;
              }
            else
              {
               push @opics, $picture;
              }
           }
        }
     }
   unless (@piclist)
     {
      print "<p><strong>Warning: no pictures found in $title album" .
         "</strong></p>\n";
      $picdir = '';
     }
   else
     {
      @piclist = (@opics,@piclist);
      my $lastpage = int ($#piclist / PAGESIZE) + 1;
      ($page < 1) && ($page = 1);
      ($page > $lastpage) && ($page = $lastpage);
      my $start = PAGESIZE * ($page - 1);
      my $nextst = PAGESIZE * $page;
      ($nextst > @piclist) && ($nextst = scalar(@piclist) + 1);
      my $navbar = '<center>';
      $navbar .= '<p><a href="showpic.cgi">Album List</a></p><p>';
      if (($page > 1) && ($page <= $lastpage))
        {
         $navbar .= '<a href="showpic.cgi?' . $picdir . '&' . ($page - 1) .
            '">[PREV]</a><br>';
        }
      if ($lastpage > 1)
        {
         for (my $pgct = 1; $pgct <= $lastpage; $pgct++)
           {
            if ($pgct != $page)
              {
               $navbar .= '<a href="showpic.cgi?' . $picdir . '&' . $pgct .
               "\">$pgct</a> ";
              }
            else
              {
               $navbar .= " <b>$pgct</b> ";
              }
           }
        }
      if ($page < $lastpage)
        {
         $navbar .= '<br><a href="showpic.cgi?' . $picdir . '&' . ($page + 1) .
            '">[NEXT]</a> ';
        }
      $navbar .= "</p></center>\n";
      print $navbar;
      print "<table border=0 cellpadding=5>\n";
      for (my $rst = $start; $rst < $nextst; $rst += ROWSIZE)
        {
         print "<tr>\n";
         for (my $idx = $rst; $idx < $rst + ROWSIZE; $idx++)
           {
            if ($idx > $#piclist)
              {
               print "<td><br></td>\n";
               next;
              }
            my $picture = $piclist[$idx];
            my $pfile = $ldir . '/' . $picture;
            (my $tfile = $pfile) =~ s/(\.\w+)$/_THMB$1/;
            (my $cfile = $pfile) =~ s/\.\w+$/.TXT/;
            my $psize = sprintf "%.0f", (stat($pfile))[7] / 1000;
            print '<td width=' . THMBSIZE . ' valign=top>';
            print "<a href=\"$pfile\">";
            (-e $tfile) &&
               (print "<img src=\"$tfile\" alt=\"$picture\">");
            my $caption = '';
            (-e $cfile) && ($caption = `cat $cfile`);
            $caption =~ s/[\n\r]/ /g;
            print "<br>$picture</a> [$psize kb]<br>$caption";
            print "</td>\n";
           }
         print "</tr>\n";
        }
      print "</table>\n";
      print $navbar;
     }
  }
else
  {
   print "<HEAD><TITLE>Brian's Picture Page</TITLE></HEAD>\n";
   print "<BODY><H1>Brian's Picture Page</H1>\n";
  }

unless ($picdir)
  {
   print <<ENDP;
<p>
This is a list of pictures that Brian Saunders and Andreia Maer have taken
or have had taken.  Please select one of the following albums:
</p>
ENDP

   my %picdirs;
   foreach my $dir (`ls -1 webpics`)
     {
      chomp $dir;
      my $ldir = 'webpics/' . $dir;
      next unless (-d $ldir);
      my $title = $dir;
      my $desc = '';
      my $score = 0;
      if (-e "$ldir/desc.txt")
        {
         my $desctext = `cat $ldir/desc.txt`;
         ($title,$desc) = $desctext =~ m/([^\n\r]+)(.*)/s;
         if ($title =~ s/=:=(-?\d+)$//)
           {
            $score = $1;
           }
        }
      $picdirs{$dir}{'title'} = $title;
      $picdirs{$dir}{'desc'} = $desc;
      $picdirs{$dir}{'score'} = $score;
     }
 #  print "<dl>";
   foreach my $dir (sort {
         $picdirs{$b}{'score'} <=> lc $picdirs{$a}{'score'}
                              ||
     lc $picdirs{$a}{'title'} cmp lc $picdirs{$b}{'title'}
    } keys %picdirs)
     {
      next if ($picdirs{$dir}{'score'} <= 0);
      my $title = $picdirs{$dir}{'title'};
      my $desc = $picdirs{$dir}{'desc'};
      print "<p><a href=\"showpic.cgi?$dir\">$title</a></p>\n";
      #print "<dt><a href=\"showpic.cgi?$dir&1\">$title</a></dt>\n";
      #print "<dd>$desc</dd>\n";
     }
 #  print "</dl>";
   print "<hr />";

   print "<p>Go to Brian's <a href=\"personal.html\">personal</a> page</p>\n";
   print "<p>Go to Brian's <a href=\"index.html\">home</a> page</p>\n";

   print "</BODY></HTML>", "\n";
  }
