#!/usr/bin/perl

$title = 'Things_4_free';
$thumb = "./thumb";                             # directory where thumbnails are stored
$catalog = "catalog.html";                      # catalog html page
$thumbquality =  70;                            # Thumbnail image quality, range 0 - 100
$thumbwidth = 264;                              # Thumbnail width
$thumbheight = 173;                              # Thumbnail height

$im_identify  = "/usr/local/bin/identify";      # Path to Imagemagick "identify" program
$im_convert   = "/usr/local/bin/convert";       # Path to Imagemagick "convert" 

# get list of files

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


# output catalog html


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

print CATALOG "<html>\n";
print CATALOG "<head>\n";
print CATALOG "<title>$title</title>\n";
print CATALOG "</head>\n";
print CATALOG "<body>\n";
print CATALOG "</body>\n";
print CATALOG "</html>\n";
print CATALOG "<h1>$title</h1></td>\n";

print CATALOG "<h3>Image catalog.</h3>\n";

# 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"; 
        $arguments = "-geometry $thumbwidth" ."x"."$thumbheight -quality $thumbquality $image $thumb/$image";
        $results=`$im_convert $arguments`;
        }
    }

foreach $image (@images) {
    chomp ($image);
    ($title,$extension) = split(/\./, $image);
    $title=~ s/\_|-/ /g;
    $results=`$im_identify $image`;
    ($junk,$format,$dimensions,$junk,$depth,$size,$junk)=split(/\ /,$results);
    ($dimensions,$junk)=split(/\+/,$dimensions);
    $title = $title . ' (' . $dimensions . ' ' . $size . ')';
    print CATALOG "<a href=\"$image\"><img src=\"$thumb/$image\" border=\"0\" alt=\"$title\"></a> \u$title.<p>\n";
#    print CATALOG "<a href=\"$image\"><img src=\"$thumb/$image\" border=\"0\" alt=\"$title\"></a>\n";
    }

print CATALOG "<h3></h3>\n";

foreach $sound (@sounds) {
    chomp ($sound);
    ($title,$extension) = split(/\./, $sound);
    $title=~ s/\_|-/ /g;
    print CATALOG "<a href=\"$sound\">sound</a> \u$title.<p>\n";
    }

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

close (CATALOG);

exit;

