#!/usr/bin/perl
# by Wesley R. Elsberry
# A helper script for the "Jpegoptim" package.
# http://onlinezoologists.com/people/wre/photo/proc/jpegoptim1.2.2_win32.zip
#

opendir(DIR,".");
@files = readdir(DIR);
closedir(DIR);

foreach (@files) {
    if (/\d_...jpg$/io) {
	&optimize_pic($_);
    }
}

sub optimize_pic {
    local($inpic) = @_;
    local($_);

    print "$inpic ";
    $_ = $inpic;
    tr/A-Z/a-z/;
    $inpiclc = $_;
    $inpiclc =~ /^(.+)(....)$/o;
  $cmd = "jpegoptim --strip-all -m60 $inpic";
  system($cmd);
  
}

sub resize_pic {
    local($inpic) = @_;
    local($_);

    print "$inpic ";
    $_ = $inpic;
    tr/A-Z/a-z/;
    $inpiclc = $_;
    $inpiclc =~ /^(.+)(....)$/o;

$inpic_tn = $1 . "_tn" . $2;
$inpic_ws = $1 . "_ws" . $2;

#$cmd = "cp $inpic $inpic_tn; cp $inpic $inpic_ws";
if (!(-e $inpic_tn)) {
  print " tn ";
  $cmd = "copy $inpic $inpic_tn";
  system($cmd);
  $cmd = "mogrify -normalize -resize 240x240 $inpic_tn";
  system($cmd);
  $cmd = "jpegoptim --strip-all -m60 $inpic_tn";
  system($cmd);
}
if (!(-e $inpic_ws)) {
  print " ws ";
  $cmd = "copy $inpic $inpic_ws";
  system($cmd);
  $cmd = "mogrify -normalize -resize 560x560 $inpic_ws";
  system($cmd);
  $cmd = "jpegoptim --strip-all -m60 $inpic_ws";
  system($cmd);
}

print "done.\n";

}
