#!/usr/bin/perl

$project_title = 'Cairo woolen chal / Vuneni sal u Kairu';
$homepage_url = 'http://irational.org/vahida/cairo_integration_diary/images/wool_chal/';
$homepage = 'Cairo Diary';
$thumb = "./thumb";                             # directory where thumbnails are stored
$catalog = "catalog.html";                      # catalog html page
$thumbquality =  70;                            # Thumbnail image quality, range 0 - 100
$thumbwidth = 260;                              # Thumbnail width
$thumbheight = 196;                              # 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|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 "<h1>$project_title</h1>\n\n";

$column_count='0';
print CATALOG "<table>\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> <div align=center><font size=2>\u$title</font><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;

