#!/usr/bin/perl
# Try -? or --help for help.
# (c) Light Olli olli\@digger.org.NOSPAMru (remove NOSPAM before mailing).
# License is like current GNU GPL, but 1st requestor buys me beer :) 

use strict;         # strict syntax - simplifies & slightly secures code.
use Getopt::Long;   # command line processing module.
use File::Basename; # path splitting module.
use File::stat;     # iface to stat() (2) & lstat() (2) functions.
# required by 'use strict;' variable declarations
my ($help,$name,$prefix,$path,$lsizemargin,$hsizemargin,$doexec,$debug,$found,
$st,$size,$execopt);

# check cmdline opts:
GetOptions
('help|?!',\$help,
'prefix=s',\$prefix,
'path=s',\$path,
'lsizemargin=i',\$lsizemargin,
'hsizemargin=i',\$hsizemargin,
'exec=s',\$doexec,
'execopt',\$execopt,
'debug=i',\$debug,
) || die "\nTry `$name -? or $name --help`\n\n";

# get programme basename (split its path)
($name=basename($0));

if ($help)
{print
"\n",
"$name: Utility to execute a command on files w/ given prefix at given path.\n",
"(c) Light Olli,olli\@digger.org.NOSPAMru (remove NOSPAM before mailing).\n",
"License is like current GNU GPL, but 1st requestor buys me beer :) .\n\n",
"  Avaliable opts:\n",
"-path=PATH\t\tPath to a directory w/ files we should proceed w/.\n",
"-prefix=NamePrefix\tFile name prefix. I.e. sample.gz has prefix \"sample\"\n",
"-lsizemargin=#\t\tLowerst size that matchs\n",
"-hsizemargin=#\t\tUpper size that matchs\n",
"-exec=cmd\t\tA command to execute w/ found file as a parameter.\n",
"-execopt=opt\t\t an option for command above. (no spaces allowed)\n",
"-debug=#\t\tDebug (noise) switch. If non zero - going to be noisy\n",
"  There're known defaults:\n",
"\t\t\t\t-path=.\n",
"\t\t\t\t-prefix=.saves\n",
"\t\t\t\t-lsizemargin=0\n",
"\t\t\t\t-hsizemargin=999\n",
"\t\t\t\t-exec=/bin/rm\n",
"\t\t\t\t-execopt=undef (no opts for -exec)\n",
"\t\t\t\t-debug=0\n",
"By default, this'll rm all files named .saves* with size in range 0-999 bytes.\n",
"\n";
exit 0;
}

unless ($#ARGV+1) {$debug && warn "Running w/ defaults!\n"}
$debug && warn "Started.\nSetting defaults..\n";

# set defaults
if ($path) 
{ if ($path =~ /\/$/) # there is a '/' at the end of path?
  {chop $path} # remove it then. Just to exclude '//' in output paths later.
} else {$path="."} # if $path unset - assign default value.
unless ($prefix) {$prefix=".saves"}
unless ($lsizemargin) {$lsizemargin=0}
unless ($hsizemargin) {$hsizemargin=999}
unless ($doexec) {$doexec="/bin/rm"}
$debug && warn "opening directory '$path'\n";
opendir (DIR,$path) || die "Can't open dir:$!";
$debug && warn "reading directory '$path'..";
$found=0;
while ($name=readdir DIR) #scalar context, 1 name per cicle.
{# skipping parent & same directory pointers.
 next if ($name eq "."); next if ($name eq "..");
 # checking for prefix in filenames.
 if ( $name =~ /($prefix).*/ )
 {#checking for exact matching - if $prefix contain perl regexps this may fail.
  if ($1 eq $prefix) 
   {$found++;
    $debug && warn "found name w/ prefix $prefix: $path/$name .\n";
    $st=stat("$path/$name") or warn "File $path/$name has disappeared: $!";
    if ( ($st->size <= $hsizemargin) && ($st->size >= $lsizemargin) )
    {$debug && warn "file has matching size: ", $st->size,". It's lower or equal to",
     "$hsizemargin & grater or equal to $lsizemargin\n";
     if ($debug && ($doexec eq "/bin/rm")) 
     {if ($execopt)
      {system("$doexec","-v","$execopt","$path/$name") == 0
      || warn "Can't system $doexec  on $path/$name : $!" 
      }
      else
      {system("$doexec","-v","$path/$name") == 0
      || warn "Can't system $doexec  on $path/$name : $!" 
      }
     } # if ($debug && ($doex..
     else { if ($execopt)
            {system ("$doexec","$execopt","$path/$name") == 0  
            || warn "Can't system $doexec on $path/$name: $!"
            }
           else 
            {system ("$doexec","$path/$name") == 0
             || warn "Can't system $doexec on $path/$name: $!"
            }
         }
     } # if ( ($st->size <..
   } # if ($1 eq $prefix) ..
 } # if ( $name =~ /($pref..
} # while ($name=readdir D..
$debug && warn "found $found matching files.\n";


