#!/usr/bin/perl
# statout - Statistic generator for bink-style outbound
# by George Brink (2:5004/28, white.owl@usa.net)
# ver 0.2 from 16 nov 98 
#   (fixed) age of echomail for points
#   (modified) mask for files listed in *.?lo
# ver 0.1 from 08 nov 98

$outbound="/var/spool/ifmail/outb";

opendir(NODES,$outbound);
for(;;) {
  $de=readdir(NODES);
  last if ! defined $de;
  if($de =~ /(.*)\.out$/) {
    $mail{$1}=1;
    $out{$1} += (stat("$outbound/$de"))[7];
    $old = (stat("$outbound/$de"))[9];
    $old1{$1} = $old if(($old1{$1} > $old) || (!defined $old1{$1}));
    $old2{$1} = $old if($old2{$1} < $old);
  } elsif ($de =~ /(.*)\.[hf]lo$/) {
    $addr=$1;
    $old = (stat("$outbound/$de"))[9];
    $old1{$1} = $old if(($old1{$1} > $old) || (!defined $old1{$1}));
    $old2{$1} = $old if($old2{$1} < $old);
    open(HLO,"<$outbound/$de");
    while(<HLO>) {
      $_ =~ /[#^~]?(.*)\n/;
      $file=$1;
      $size = (stat("$file"))[7];
      if($file =~ /\.su|mo|tu|we|th|fr|sa[0-9a-z]$/) {
        if ($size > 0) {
          $echo{$addr}=1;
          $old = (stat("$file"))[9];
          $old1{$addr} = $old if(($old1{$addr} > $old) || (!defined $old1{$addr}));
          $old2{$addr} = $old if($old2{$addr} < $old);
	}
      } else {
        $files{$addr}=1;
      }
      $out{$addr} += $size;
    }
    close(HLO);
  } elsif ($de =~ /(.*)\.pnt$/) {
    opendir(POINTS,"$outbound/$de");
    for(;;) {
      $dep=readdir(POINTS);
      last if !defined $dep;
      if($dep =~ /(.*)\.out$/) {
        $addr=$de.$1;
	$mail{$addr}=1;
        $out{$addr} += (stat("$outbound/$de/$dep"))[7];
        $old = (stat("$outbound/$de/$dep"))[9];
        $old1{$addr} = $old if(($old1{$addr} > $old) || (!defined ($old1{$addr})));
        $old2{$addr} = $old if($old2{$addr} < $old);
      } elsif ($dep =~ /(.*)\.[hf]lo$/) {
        $addr=$de.$1;
        $old = (stat("$outbound/$de/$dep"))[9];
        $old1{$addr} = $old if(($old1{$addr} > $old) || (!defined ($old1{$addr})));
        $old2{$addr} = $old if($old2{$addr} < $old);
        open(HLO,"<$outbound/$de/$dep");
        while(<HLO>) {
          $_ =~ /[#^~]?(.*)\n/;
          $file=$1;
          $size=(stat("$file"))[7];
          if($file =~ /\.su|mo|tu|we|th|fr|sa[0-9a-z]$/) {
	    if ($size > 0) {
              $echo{$addr}=1;
              $old = (stat("$file"))[9];
              $old1{$addr} = $old if(($old1{$addr} > $old) || (!defined ($old1{$addr})));
              $old2{$addr} = $old if($old2{$addr} < $old);
	    }
          } else {
            $files{$addr}=1 if ($size > 0);
          }
          $out{$addr}+=$size;
        }
        close(HLO);
      }
    }
    closedir(POINTS);
  }
}
closedir(NODES);

@address=sort(keys %out);
foreach (@address) {
  if (/^(.{4})(.{4})\.pnt0{4}(.{4})/) {
    eval("\$net=0x$1");
    eval("\$node=0x$2");
    eval("\$point=0x$3");
    $addr="($3) $net/$node\.$point";
  } else {
    $_ =~ /^(.{4})(.{4})/;
    eval("\$net=0x$1");
    eval("\$node=0x$2");
    $addr="($2) $net/$node.0";
  }
  print "$addr\t";
  print "\t" if(length($addr)<16);
  
  if(defined $mail{$_}) {print "M";} else {print "-";}
  if(defined $echo{$_}) {print "E";} else {print "-";}
  if(defined $files{$_}) {print "F";} else {print "-";}
  printf " %8d",$out{$_};

  @_=localtime(time() - $old1{$_});
  if ($_[4]>0) {printf " %2dm",$_[4];} else {print "    ";}
  if (($_[3]>14) || ($_[4]>0)) {printf "%02d",$_[3];} else {print "  ";}

  @_=localtime($old1{$_});
  $_[4]++;
  for($i=1;$i<5;$i++) {
    $_[$i]="0".$_[$i] if length($_[$i])<2;
  }    
  print " $_[3].$_[4] $_[2]:$_[1] ";

  @_=localtime($old2{$_});
  $_[4]++;
  for($i=1;$i<5;$i++) {
    $_[$i]="0".$_[$i] if length($_[$i])<2;
  }    
  print "- $_[3].$_[4] $_[2]:$_[1]\n";
}
