#!/usr/bin/perl
# This script depends on the program mogrify (from ImageMagik)

use File::Basename;

if (@ARGV != 1) {
    die "usage: $0 <image_dir>\n";
}

my $image_dir = shift;
my @extensions = (".jpg",".jpeg",".png",".gif");

# clear out all the old thumbnails:
system("find $image_dir -name \"_thb_*\" -exec rm {} \\;");

foreach $extension (@extensions) {
    print $extension; print "\n";
    my @pics = `find $image_dir -name "*$extension"`;
    foreach $pic (@pics) {
        print $pic;
        chomp ($pic);
        my $thumb = dirname($pic) . "/_thb_" . basename($pic);
        system(
          "convert -size 120x120 \"$pic\" -resize 120x120 +profile \"*\" \"$thumb\""
        );
    }
}

#TOP=`pwd`
#for d in *
#do
  #if [ -d $d ]
  #then
    #rm -rf $d/thumbgen
    #rm -f $d/_thb_*
    #mkdir $d/thumbgen
    #cp $d/* $d/thumbgen
    #cd $d/thumbgen
    #mogrify -size 100x -resize 100x *
    #for f in *; do mv $f _thb_$f; done
    #for f in *; do mv $f ..; done
    #cd $TOP
    #rmdir $d/thumbgen
  #fi
#done
