#! /bin/sh

. ${srcdir:=.}/t.frame

seq="1 2 3 4 5"

# OPTS used for all
# IOPTS used for initialization
OPTS="-C -M"
IOPTS=
if [ $verbose -gt 0 ] ; then IOPTS="$IOPTS -v" ; fi

rc=0

$BOGOFILTER $OPTS -d "$TMPDIR" $IOPTS -n -I "$srcdir"/inputs/good.mbx
$BOGOFILTER $OPTS -d "$TMPDIR" $IOPTS -s -I "$srcdir"/inputs/spam.mbx

oldtraps=`trap`
case `uname -s` in
    SunOS)	oldtraps=`echo $oldtraps | sed -e 's/^trap --/trap/g'` ;;
esac
trap 'kill $pid' 1 2 3

if [ $verbose -gt 0 ] ; then echo "#### Part 1 ####" ; fi
for I in $seq ; do
    (   set +e
	$BOGOFILTER $OPTS -d "$TMPDIR" -u -I "$srcdir"/inputs/spam.mbx 
	echo $? >> "$TMPDIR/exits"
    ) &
    pid="$pid $!"
done

wait
pid=

if [ $verbose -gt 0 ] ; then echo "#### Part 2 ####" ; fi
for I in $seq ; do
    (   set +e
	$BOGOFILTER $OPTS -d "$TMPDIR" -u -p -I "$srcdir"/inputs/spam.mbx >> "$TMPDIR"/$I.out
	echo $? >> "$TMPDIR/exits"
    ) &
    pid="$pid $!"
done

wait
pid=

trap - 1 2 3
eval "$oldtraps"

# check for program failures
test "x$(grep -v 0 "$TMPDIR"/exits)" = x

# check if all programs reported their code
exits=`cat "$TMPDIR/exits" | wc -l`
set -- $seq $seq
test $exits -eq $#
