#!/usr/bin/perl -w

if($#ARGV!=0) {
  print "Usage: perl sharpen.pl <directory>\n";
  exit;
}
my $imagedir=$ARGV[0];
my @filelist;
my $current_fname="";
my $last_fname="";

if($^O eq "MSWin32") {
  @filelist=glob("\"$imagedir\\\*.png\"");
} else {
  @filelist=glob("\"$imagedir/\*.png\"");
}
my $listsize=@filelist;
if($listsize==0) {
  print "Error: No PNG files found in $imagedir\n";
  exit;
}

FILELOOP: for $imagefile (@filelist) {
  my @arr;
  if($^O eq "MSWin32") {
    @arr=split(/\\/,$imagefile);
  } else {
    @arr=split(/\//,$imagefile);
  }
  $current_fname=$arr[-1];
  @arr=split(/_/,$current_fname);
  $current_fname=$arr[0];
  if($current_fname ne $last_fname) {
    print "Processing images for $current_fname...\n";
    $last_fname=$current_fname;
  }
  system("convert +dither -alpha on -sharpen 0x1.5 $imagefile $imagefile");
}

print "Files in $imagedir have been sharpened.\n";

exit;