#!/usr/bin/perl
# generates a html-overview of a directory with jpeg's and their thumbs.

use CGI qw/:standard/;

sub chdir_error {
  print start_html('Error'), h1('Could not change into directory!'), html_end;
  exit 0;
}

print header;

$id = param('id');

if ($id) {
  if ($id =~ m/[^a-z0-9-]/) {	# check for invalid chars
    print start_html('Error'), h1('Invalid request!'), end_html;
    exit 0;
  }

  if (!chdir "fotos/$id") { chdir_error }
  print start_html("Foto-Album $id"), h1("Foto-Album $id");

  JPG: foreach $jpg (glob("*.jpg")) {
    next JPG if ($jpg =~ m/_thumb/);
    ($thumb = $jpg) =~ s/\.jpg$/_thumb.jpg/;
    print "<a href=\"fotos/$id/$jpg\"><img src=\"fotos/$id/$thumb\"></a>";
    if (open INFO, "<$jpg.info") {
      my @info = <INFO>;
      print " - ";
      print @info;
      close INFO;
    }
#    print "<br>\n";
  }
} else {
  if (!chdir "fotos") { chdir_error }
  print start_html("Foto-Alben"), h1("Foto-Alben");
  foreach $album (glob("*")) {
    print "<a href=\"fotos.pl?id=$album\">$album</a>";
    if (open INFO, "<$album/.info") {
      my @info = <INFO>;
      print " - ";
      print @info;
      close INFO;
    }
    print "<br>\n";
  }
}

print <<\FOOTER;
<hr>
generated from fotos.pl, written by <a href="mailto:programme@skora.net">Thomas Skora</a>
FOOTER
print end_html;
