#!/usr/local/bin/bash
#
# Checks the /Mail/log/mail logfile for delivery failures due to quota
# enforcement.  If found it will deposit a message into the user's
# maildir to alert them to the condition.
#
# Later we will be reviewing the number of alert messages in a user's
# maildir to determine how long (roughly) the maildir has been over-
# quota.
#
export LD_LIBRARY_PATH=/usr/local/lib
DATESTAMP=`date +%y%m%d`
CURDATE=`date`

cat /var/scripts/quotanotify.summary.txt | sed -e "s/CURDATE/$CURDATE/" >/tmp/notify.txt
cat /var/scripts/quotanotify.summary.not.txt | sed -e "s/CURDATE/$CURDATE/" >/tmp/notnotify.txt

for address in `cat /Mail/log/mail | grep 'Mailbox full.' | sed -e 's/.*addr=<//' | sed -e 's/>\:.*//' | sed -e 'y/ABCDEFGHIJ
KLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' | sed -e 's/compusmart.ab.ca/interbaun.com/' | sort | uniq`; do
    MAILDIR=`/usr/local/bin/getmaildir $address`;
    MAILDIRSIZE=`du -k $MAILDIR | head -1 | awk '{print $1}'`;

    if [ $MAILDIRSIZE -gt 15000 ]; then
     echo "$address ($MAILDIR) at $MAILDIRSIZE kilobytes" >>/tmp/notify.txt
     cat /var/scripts/quotanotify.txt | sed -e s/NOTIFYUSER/$address/ | sed -e "s/CURDATE/$CURDATE/" >$MAILDIR/new/auto-quota
-notify-${DATESTAMP}.txt
     chown vmail:vmail $MAILDIR/new/auto-quota-notify-${DATESTAMP}.txt
     chmod 0550 $MAILDIR/new/auto-quota-notify-${DATESTAMP}.txt
    else
     echo "$address ($MAILDIR) at $MAILDIRSIZE kilobytes" >>/tmp/notnotify.txt
    fi
done

cat /tmp/notnotify.txt >>/tmp/notify.txt
cat /tmp/notify.txt | /Mail/bin/sendmail -f netadmin@interbaun.net support@interbaun.net
