#!/bin/bash
# Public domain (C) 2001 Martin Macok <martin.macok@underground.cz>

function DEBUG { echo "$@" ; }
#function DEBUG { }

if [ "$#" -lt "2" -o "x$1" == "x--help" ] ; then
	echo "Usage: $0 category mailbox "
	echo "Prints theoretical succes of ifile on mailbox."
	exit 1
fi

FTMP=${TMPDIR:-/tmp}
FMSG=`mktemp $FTMP/ifile.tmp.XXXXXX`
[ -z "$FMSG" ] && echo "ERR: cannot create tmp file!" >&2 && exit 1
trap "rm -f $FMSG" 1 2 3 4 5 6 7 8  9 10 11 12 13 14 15

CAT="$1"

shift 1

for MBOX in "$@" ; do

  #DEBUG "Testing mailbox $MBOX with category $CAT"
  formail -s ifile.test.message <"$MBOX" >"$FMSG"
  
  SUCCESS=$(grep "^${CAT}$" <"$FMSG"|wc -l)/$(wc -l <"$FMSG")
  SUCCESS=$(echo $SUCCESS|sed 's/\ //g')
  
  echo "${MBOX} (${CAT})" : "$SUCCESS" " ("$(echo "100 * $SUCCESS"|bc)"%)"
  
  #DEBUG "In detail:"
  for CATS in $(sort -u <"$FMSG") ; do
  	DEBUG "$CATS:" $(grep "$CATS" <"$FMSG"|wc -l)
  done

done

rm -f "$FMSG"
