#!/usr/bin/perl
eval "exec /usr/bin/perl -S $0 $*"
    if $running_under_some_shell;
    # this emulates #! processing on NIH machines.
    # (remove #! line above if indigestible)

# procmailcount - count mails received for each folder from procmail's log file
# author: Mikko Hänninen <wiz@iki.fi>

# Usage:
#   procmailcount [-s] [-t] procmail-log(s)
# OR
#   procmailcount [-s] [-t] < procmail-log

# Options:
#   -s  Display statistics about each folder (average, min and max message
#         sizes)
#   -t  Display total number of delivered messages, if more than one folder
#   -n  No explanative strings ("delivered messages", "bytes"), overridden
#         by -s
#   -b  Display total bytes counts
#   -i folder1[:folder2[:...]]
#       Ignore folder: no display, don't include in statistics
#   -d folder1=name1[:folder2=name2[:...]]
#       Substitute name for folder
#   -h  Display usage info and exit
#   -r  Do not not check for Maildir deliveries (.../tmp/...)
# 
# Example for -d option:
#   procmailcount -d /usr/spool/mail/user=INBOX Mail/procmail.log
#   -> this would make the /usr/spool/mail/user folder appear as INBOX

# When        Who  What
# 2000-08-20  wiz  Fixed bug in -s, thanks to Brian Salter-Duke <b_duke@lacebark.ntu.edu.au>
# 2000-08-19  wiz  Usage typo fix, by Jon Parise <jon@csh.rit.edu>
# 1999-??-??  wiz  Added support for maildir format deliveries, -r disables support
# 1998-11-10  wiz  Support for folder names with spaces in them (eg. for piped commands)
# 1998-04-24  wiz  Added the -b switch, reworked the printing code
# 1998-01-08  wiz  Added the -n switch
# 1997-10-14  wiz  Added support for pipe folders (may have more than a single word)
# 1997-02-12  wiz  Added the -i switch
# 1996-11-21  wiz  Added the -d switch
# 1996-01-25  wiz  Added -t switch
#                  Fixed bugs in folder statistics
# 1995-08-14  wiz  Original version

require "getopts.pl";

do Getopts('hstnbrd:i:') && !($opt_h) ||
        die
"Usage: $0 [-stnbr] [-i folder] [-d folder=name] procmail-log
-s  Display statistics about each folder
-t  Display total number of delivered messages
-n  No explanative strings, overridden by -s
-h  Usage info (this help)
-r  Do not check for Maildir format folders
-i folder1[:folder2[:...]]  Ignore folder(s)
-d folder1=name1[:folder2=name2[:...]]  Folder name aliasing
";

sub foldername {
  if ($foldername{$_[0]}) { return $foldername{$_[0]}; }
  else { return $_[0]; }
}

if ($opt_d) {
  @subs = split(/:/, $opt_d);
  foreach (@subs) {
    if (/\s*(.+)\s*=\s*(.+)\s*/) {
      $foldername{$1} = $2;
      #print "DEBUG: $1 set to $2\n";
    }
  }
}

if ($opt_i) {
  @igns = split(/:/, $opt_i);
  foreach (@igns) {
    $folderignore{$_} = 1;
  }
}

while (<>) {
  chomp;
  if (/Folder: +(.+)\s+(\d*)$/) {
    $msgsize = $2;
    ($folder = $1) =~ s/\s+$//;
    if (!$opt_r && $folder =~ m#(.*)\b/+(tmp|new)/[0-9a-zA-Z\.,_+\-]+$#) { $folder = $1; }

    #if (($folder eq "/dev/null") || ($folderignore{$folder})) { next; }
    if ($folderignore{$folder}) { next; }
    if ($folder{$folder} == 0) { $totfolders++; }
    $folder{$folder}++;
    $foldertot{$folder} += $msgsize;
    $foldermin{$folder} = $msgsize if $msgsize < $foldermin{$folder} || $foldermin{$folder} == 0;
    $foldermax{$folder} = $msgsize if $msgsize > $foldermax{$folder};
  }
  #else { print "No match: $_\n"; }
}

foreach (sort(keys(%folder))) {
  $msgcount  = $folder{$_};
  $totmsgs  += $msgcount;
  $bytecount = $foldertot{$_};
  $totbytes += $bytecount;
  undef($msgtag); undef($folderbytes); undef($folderstats);
  $len = length;

  if (!$opt_n || $opt_s) { $msgtag = " delivered message" . (($msgcount == 1) ? "" : "s") ; }
  if ($opt_b) { $folderbytes = " (" . $bytecount . ($opt_n ? "" : " bytes") . ")"; }
  if ($opt_s) {
    if ($msgcount == 1) {
      $folderstats = ", size " . $bytecount . ".";
    } else {
      $folderstats = "\n" . sprintf("%${len}s", ' ') .
                     "  avg size " . int($bytecount/$msgcount+0.5) .
                     ", max size " . $foldermax{$_} .
                     ", min size " . $foldermin{$_} . ".";
    }
  }

  print &foldername($_), ": ", $msgcount, $msgtag, $folderbytes, $folderstats, "\n";
}

if (($opt_t) && ($totfolders > 1)) {
  if ($opt_b) {
    print "Total of $totmsgs delivered messages in $totfolders folders ($totbytes bytes).\n";
  } else {
    print "Total of $totmsgs delivered messages in $totfolders folders.\n";
  }
}
