#!/usr/bin/perl
# Procmail Log finder/deleter
# Deletes anything named 'procmail.log' that starts with the following
# keywords:  Sanitizing, Defanging, Truncating
#
#

$arg = shift(@ARGV);
if ($arg eq "-test") { $test = 1; }
if ($arg eq "-v") { $verbose = 1; }
if ($arg eq "-h" || $arg eq "-?") { &usage(); exit; }
@letters = @ARGV;

$deleted = 0;
$skipped = 0;
$total   = 0;

$users="/users";
$fname = "procmail.log";

# get list of all letters under $users
@letters = <$users/*> unless defined @letters;

# run thru the letters
foreach $let (@letters) {
   
   print "Searching $let...\n" unless(!$verbose);
   # get a list of all the names in that dir
   @unames = <$let/*>;

   # run thru the names
   foreach $name (@unames) {
 
      print "Searching $name...\n" unless(!$verbose);
      # here is the absolute filename
      $file = "$name/$fname";

      # check if it's a regular file
      if (!(-e "$name/.procmailrc") && !(-e "$name/log.save")) {

         if (-e "$file" && -f "$file") {
            print "checking $file\n" unless(!$verbose);
            open(FILE,"<$file") or print "Cannot open $file: $!\n";

            $_ = <FILE>; 

            if (/^From/ || /^Sanitizing/ || /^Defanging/ || /^Truncating/ ) {

               print "found a log: $file\n" unless(!$verbose);
               if (!$test) {
                  @args = ("/usr/bin/rm","$file");
                  system(@args) == 0 or print "Couldn't delete $file: $!\n";
                  $deleted++;
               }
            }
            close(FILE);
         }
      }
      else {
         print "$name has a .procmailrc.  Skipping.\n" unless(!$verbose);
         $skipped++;
      } # if

      $total++;
   
   } # foreach name

} #foreach letter

print "Summary statistics for procmail log finder:\n\n",
      "Deleted:       $deleted\n",
      "Skipped:       $skipped\n",
      "Total Checked: $total\n\n";

exit;

sub usage() {
   print "usage:  [-h] [-v] [-test [/users/tree]]\n";
}

