#!/bin/sh

#noticesfrom=`perl -n -e 'print "$_" if chomp && s/^\s*Notices\s*From\s*=\s*([^\s#]+).*$/$1/' /etc/MailScanner/MailScanner.conf`
#localpostmaster=`perl -n -e 'print "$_" if chomp && s/^\s*Local\s*Postmaster\s*=\s*([^\s#]+).*$/$1/' /etc/MailScanner/MailScanner.conf`
#noticesto=`perl -n -e 'print "$_" if chomp && s/^\s*Notices\s*To\s*=\s*([^\s#]+).*$/$1/' /etc/MailScanner/MailScanner.conf`
#sendnotices=`perl -n -e 'print "$_" if chomp && s/^\s*Send\s*Notices\s*=\s*([^\s#]+).*$/$1/' /etc/MailScanner/MailScanner.conf`
noticesfrom=`/usr/local/bin/Quick.Peek noticesfrom /etc/MailScanner/MailScanner.conf`
localpostmaster=`/usr/local/bin/Quick.Peek localpostmaster /etc/MailScanner/MailScanner.conf`
noticesto=`/usr/local/bin/Quick.Peek noticesto /etc/MailScanner/MailScanner.conf`
sendnotices=`/usr/local/bin/Quick.Peek sendnotices /etc/MailScanner/MailScanner.conf`

# Only do this if they want to send notices to postmaster at all
if [ "x$sendnotices" = "xno" ]; then
  exit 0
fi

# If there was a / at the start of the destination email address, then the
# setting was a ruleset which Quick.Peek does not support. So use the
# localpostmaster setting instead, it's better than creating an error.
if ( echo $noticesto | grep -q '^/' ); then
  noticesto=$localpostmaster
fi

alert=`/usr/local/bin/MailScanner --processing`

if [ \! "x$alert" = "x" ]; then
  #cat <<EOM
  sendmail -oem -t <<EOM
From: $noticesfrom <$localpostmaster>
To: MailScanner Administrator <$noticesto>
Subject: Problem Messages

$alert

-- 
MailScanner
EOM
fi

