#!/usr/bin/perl

$project_title = 'Travel to Primislje 2009';
$homepage_url = 'http://irational.org/vahida/primislje';
$homepage ='Travel to Primislje';
$thumb = "./thumb";                             # directory where thumbnails are stored
$catalog = "catalog.html";                      # catalog html page
$thumbquality =  70;                            # Thumbnail image quality, range 0 - 100
$thumbwidth =300;                              # Thumbnail width
$thumbheight = 200;                              # Thumbnail height
$max_columns = '4';

$im_convert   = "/usr/bin/convert";       # Path to Imagemagick "convert" 

($second, $minute, $hour, $currentdayofmonth, $month, $year, $weekday, $dayofyear, $IsDST)= localtime(time);
$currentmonth=$month+1;
$year=$year+'1900';

$currentdayofmonth=sprintf("%.2u",$currentdayofmonth);
$currentmonth=sprintf("%.2u",$currentmonth);

$date=$currentdayofmonth . '/' . $currentmonth . '/' . $year;


# get list of files

    @files = `ls ./ -1`; # get list of images
    $n=0;
    foreach $file (@files) {
        chomp($file);
        if ($file =~ /jpg|JPG|jpeg|gif|GIF/) {push (@images,$file); }
        }


# output catalog html


# verify thumbnail directory exists

if (!(-d $thumb)) {mkdir $thumb, 0777; }

# create missing thumb nails

foreach $image (@images) {

    if (!(-f "$thumb/$image")) {
#        print "making $thumb/$image<br>\n"; 
        print "making $thumb/$image\n"; 
        $arguments = "-geometry $thumbwidth" ."x"."$thumbheight -quality $thumbquality $image $thumb/$image";
        $results=`$im_convert $arguments`;
        }
    }

open(CATALOG,">$catalog") || die "Can't open: $catalog\n";

print CATALOG "<html>\n";
print CATALOG "<head>\n";
print CATALOG "<title>$project_title</title>\n";

foreach $image (@images) {
    chomp ($image);
    ($title,$extension) = split(/\./, $image);
    $title=~ s/\_|-/ /g;
    $title=~ s/[0-9]//g;
    (@temp_keywords)=split(/ /,$title);
    foreach $temp (@temp_keywords) {
        if (!$found{$temp}) {push (@keywords,$temp); $found{$temp}='1'; }
        }
    }

print CATALOG "<meta NAME=\"DESCRIPTION\" CONTENT=\"$project_title\">\n";

print CATALOG "<meta NAME=\"KEYWORDS\" CONTENT=\"";

foreach $key (@keywords) {
    print CATALOG "$key, ";
    }

print CATALOG "\">\n";

print CATALOG "</head>\n";
print CATALOG "<body>\n\n";
print CATALOG "&nbsp;<p>\n\n";
print CATALOG "<p align=\"center\"><font size=\"6\">$project_title</font>\n\n";
print CATALOG "<p align=\"center\">&nbsp;</p>\n\n";

$column_count='0';
print CATALOG "<table align=\"center\">\n";
print CATALOG "<tr>\n";
foreach $image (@images) {
    chomp ($image);
    ($title,$extension) = split(/\./, $image);
    $title=~ s/\_|-/ /g;
    $title=~ s/[0-9]//g;
    print CATALOG "<td><a href=\"$image\"><img src=\"$thumb/$image\" border=\"0\" alt=\"$title\"></a><br></td>\n";
    $column_count++;
    if ($column_count >= $max_columns) {$column_count='0'; print CATALOG "</tr>\n<tr>"; }
    }
print CATALOG "</tr>\n";
print CATALOG "</table>\n";

print CATALOG "\n<a href=\"$homepage_url\">$homepage</a><p>\n\n";

#print CATALOG "$date\n<p>";

print CATALOG "</body>\n";
print CATALOG "</html>\n";

close (CATALOG);

exit;

