#!/bin/sh

if [ -z $SDDIR ]; then
    SDDIR=/etc/statusd
fi

# Number of messages
ERR1=100
ERR2=1000
ERR3=10000

IGNORE_FROM=""

if [ -f $SDDIR/config/mailq.conf ]; then
    . $SDDIR/config/mailq.conf
fi

#msgnum=`find $DEFER/ -type f |wc -l`
#msgnum=`chroot /chroot/maild mailq |grep -E '^[0-9A-F]{4,16}[ ]' |grep -v 'MAILER-DAEMON' |wc -l`

msglist=`mailq |grep -E '^[0-9A-F]{4,16}[ ]'`

crmsglist=`echo "$msglist"`
for ignore in $IGNORE_FROM; do
	crmsglist=`echo "$crmsglist" | grep -v "$ignore"`
done

allmsgnum=`echo "$msglist" | wc -l`
crmsgnum=`echo "$crmsglist" | wc -l`

if [ $crmsgnum -gt $ERR1 ]; then

	if [ $crmsgnum -gt $ERR1 ]; then ERR=$ERR1; fi
	if [ $crmsgnum -gt $ERR2 ]; then ERR=$ERR2; fi
	if [ $crmsgnum -gt $ERR3 ]; then ERR=$ERR3; fi

	echo "$ERR+ deferred"
	exit 2

elif [ $allmsgnum -gt $ERR1 ]; then

	if [ $allmsgnum -gt $ERR1 ]; then ERR=$ERR1; fi
	if [ $allmsgnum -gt $ERR2 ]; then ERR=$ERR2; fi
	if [ $allmsgnum -gt $ERR3 ]; then ERR=$ERR3; fi

	echo "$ERR+ deferred (all)"
	exit 1

else
	echo "OK"
	exit 0
fi
