#!/usr/bin/perl

use strict;

my ($file, $results, $num_cols);
my ($d_height, $d_width, $r_height, $r_width);
my ($titles);
open (OUTFILE, ">index.html");
opendir (CURDIR, ".");

print (OUTFILE "<html>\n");
print (OUTFILE "<br><b>Current images:</b>\n");
print (OUTFILE "<table>\n");
print (OUTFILE "<tr>\n");
$num_cols = 0;
$titles = "";
foreach $file (readdir (CURDIR))
{
  next if $file =~ /^tiny.*/;
  if ($file =~ /(\.jpg)|(\.gif)/)
  {
    $results = `identify $file`;
    $results =~ m/(\d+)x(\d+)/;
    $r_width = $1;
    $r_height = $2;
    $d_height = 120;   # An arbitrary height
    $d_width  = int ($d_height * $r_width/$r_height);
    `cp $file tiny.${file}`;
    #`mogrify -geometry ${d_width}x${d_height}  -font lucidasanstypewriter-12 -pen white -draw 'text 0,0 \"${file}\"' tiny.${file}`;
    `mogrify -geometry ${d_width}x${d_height}  tiny.${file}`;

    print ("Command: mogrify -geometry ${d_width}x${d_height}  -font lucidasanstypewriter-12 -pen white -draw 'text 0,0 ${file}' tiny.${file}\n");
    print ("Processing $file\n");
    print (OUTFILE "    <td>\n");
    print (OUTFILE "        <a href=\"/current/vladi2/$file\">");
    print (OUTFILE "<img src=\"/current/vladi2/tiny.$file\"></a>\n");
    print (OUTFILE "    </td>\n");
    $titles .= "<td>\n${file}\n</td>\n";
    $num_cols++;
    if ($num_cols % 5 == 0)
    {
      print (OUTFILE "</tr>\n");
      $titles = "<tr>\n" . $titles . "</tr>";
      print (OUTFILE "$titles");
      print (OUTFILE "\n<tr>\n");
      $titles = "";
    }
  }
}
  if ($num_cols % 5 != 0)
  {
    print (OUTFILE "</tr>\n");
    $titles = "<tr>\n" . $titles . "</tr>";
    print (OUTFILE "$titles");
  }
print (OUTFILE "</table>\n");
