#!/bin/sh
# This script will return the number of Spam and non Spam for a given day from /var/log/maillog.
# If a date is not given it will return zeros

#Date=$1
#Day=`echo $Date | tr -s ' ' | cut -d' ' -f2`
Day=`date --date='1 day ago' +%-d`
Date=`date --date='1 day ago' '+%b %_d'`
MAIL_LOG_LOC=/var/log
SPAM_LOC=/usr/pkg/mailadmin/mail_logs

# Must be root
if test "`/usr/bin/id -u`" != 0 ; then
    echo "$0: You must be root to run this script" >& 2
    exit 1
fi

# Find yesterdays Spam count
	
	/bin/egrep -i 'X-Spam-Status:' $MAIL_LOG_LOC/maillog.3 | /bin/grep "^$Date" > $SPAM_LOC/spam-rpt$Day.txt;
	/bin/egrep -i 'X-Spam-Status:' $MAIL_LOG_LOC/maillog.2 | /bin/grep "^$Date" >> $SPAM_LOC/spam-rpt$Day.txt;
	/bin/egrep -i 'X-Spam-Status:' $MAIL_LOG_LOC/maillog.1 | /bin/grep "^$Date" >> $SPAM_LOC/spam-rpt$Day.txt;
	/bin/egrep -i 'X-Spam-Status:' $MAIL_LOG_LOC/maillog | /bin/grep "^$Date" >> $SPAM_LOC/spam-rpt$Day.txt;

# Count number of Spam
	SPAMYES=`/bin/egrep -i 'X-Spam-Status:' $SPAM_LOC/spam-rpt$Day.txt | /bin/grep -c 'X-Spam-Status: Yes'`
# Count number of Clean emails
	SPAMNO=`/bin/egrep -i 'X-Spam-Status:' $SPAM_LOC/spam-rpt$Day.txt | /bin/grep -c 'X-Spam-Status: No'`
# Count more Clean emails
	SPAMNO2=`/bin/egrep -i 'X-Spam-Status:' $SPAM_LOC/spam-rpt$Day.txt | /bin/grep -c 'X-Spam-Status: from No'`
# Count all email
	SPAMTOTAL=`/bin/egrep -i 'X-Spam-Status:' $SPAM_LOC/spam-rpt$Day.txt | /bin/grep -c 'X-Spam-Status:'`
# Send it to report file
	echo $Date\;$SPAMYES\;$SPAMNO\;$SPAMNO2\;$SPAMTOTAL\; >> $SPAM_LOC/assassin_rpt.txt
# Clean up report directory
	/bin/rm -f $SPAM_LOC/spam-rpt*
# Email report file to admin
	mail -s 'SpamAssassin Report' root@localhost < $SPAM_LOC/assassin_rpt.txt

