#!/bin/bash
# 
# Copyright (C) 2002 Laird Breyer
#  
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
# 
# Author:   Laird Breyer <laird@lbreyer.com>
#

PROGNAME="$0"
PROGNAME2=`basename $0`
VERSION="$PROGNAME version 1.11\nTrain On Error simulator"
MXDIR="$PWD/mailtoe.d"
ALOG="$MXDIR/log/activity.log"
CLOG="$MXDIR/log/toe.log"
SLOG="$MXDIR/log/summary.log"
TEMPDIR="$MXDIR/tmp"
BOOTSTRAP="/usr/share/dbacl/testsuite"
FILTERS="$MXDIR/filters"

# use this for debugging
# set -x

usage() {
    echo "
Usage: 

    $PROGNAME2 prepare size
    $PROGNAME2 add category [MBOX]...
    $PROGNAME2 run
    $PROGNAME2 summarize [LEVEL]
    $PROGNAME2 plot [ps|logscale]
    $PROGNAME2 review TRUECAT PREDCAT
    $PROGNAME2 clean
    $PROGNAME2 killall

    $PROGNAME2 testsuite select [FILTER]...
    $PROGNAME2 testsuite deselect [FILTER]...
    $PROGNAME2 testsuite list
    $PROGNAME2 testsuite status
    $PROGNAME2 testsuite run [plots]
    $PROGNAME2 testsuite summarize
"
    exit 1
}

mbox_multiplex() {
    perl -e '
# usage: $0 seqno seed [MBOX]... -s COMMAND ARGS 
use strict; 
$SIG{PIPE} = "IGNORE"; 
my %mbox; 
my %line; 
my $cmd = ""; 
my $args = ""; 
my $n = shift;
srand shift; 
 
foreach my $g (@ARGV) { 
  if( "$g" eq "-s" ) { 
    $cmd = "|"; 
  } elsif( $cmd ne "" ) { 
    $cmd .= " $g"; 
  } else { 
    if( open($mbox{$g}, "<$g") ) {
      $args .= " $g"; 
    } else {
      delete $mbox{$g}; 
    }
  } 
} 

$args =~ s/\.mbox//g;
$args =~ s|/mbox/|/$n/|g;
$| = 1; 

while( scalar keys %mbox > 0 ) { 
  my $j = int(rand scalar keys %mbox); 
  foreach my $f (keys %mbox) { 
    if( $j-- <= 0 ) { 
      if( eof($mbox{$f}) ) { 
        close($mbox{$f}); 
        delete $mbox{$f}; 
      } else { 
	my $c = $f;
	$c =~ s|\.mbox$||;
	$c =~ s|/mbox/|/$n/|;

        open(CPIPE, "$cmd $c $args"); 
        while( ($line{$f} !~ /^From /) && !eof($mbox{$f}) ) { 
          $line{$f} = readline $mbox{$f}; 
        } 

	my $fromline = $line{$f};
	$fromline =~ s/^From//;
	$c =~ s|^.*/||;

	print "$n $c ";
        print CPIPE $line{$f}; 
        $line{$f} = readline $mbox{$f}; 
        while( !eof($mbox{$f}) && ($line{$f} !~ /^From /) ) { 
          print CPIPE $line{$f}; 
          $line{$f} = readline $mbox{$f}; 
        } 
        close(CPIPE); 
	# expect the piped command to output result without trailing newline
	print $fromline;
      } 
      last; 
    } 
  } 
} 
' "$@"
}

# this is the default filter
if [ -z "$MAILTOE_FILTER" ]; then
    MAILTOE_FILTER="/usr/share/dbacl/dbaclB toe"
fi


