#!/bin/bash

# JKF This is part of MailScanner.
# Call sa-update and sa-compile if you have them both.
# If you want to add arguments (such as channel file settings) to sa-update
# then do it by editing /etc/sysconfig/MailScanner, *NOT* by editing this
# script.

SAUPDATE=/usr/bin/sa-update
SACOMPILE=/usr/bin/sa-compile
SAUPDATEARGS=""
if [ -f /etc/sysconfig/update_spamassassin ] ; then
        . /etc/sysconfig/update_spamassassin
fi
export SAUPDATE
export SACOMPILE
export SAUPDATEARGS
UPDATESUCCESS=0
COMPILESUCCESS=0
LOGFILE=/tmp/update_spamassassin.$(date +%m%d)

# Update SpamAssassin rules
[ -x $SAUPDATE ] || exit 0
rm -f $LOGFILE
$SAUPDATE $SAUPDATEARGS >$LOGFILE 2>&1
UPDATESUCCESS=$?

if [ $UPDATESUCCESS = 0 ]; then
  # If we have sa-compile and they are using the Rule2XSBody plugin then compile
  if test -x $SACOMPILE && grep -q '^loadplugin.*Rule2XSBody' /etc/mail/spamassassin/*pre 2>/dev/null ; then
    $SACOMPILE >>$LOGFILE 2>&1
    COMPILESUCCESS=$?
  else
    echo $SACOMPILE does not exist or Rule2XSBody plugin not enabled >>$LOGFILE
    COMPILESUCCESS=1
  fi
fi

/etc/init.d/MailScanner reload >>$LOGFILE 2>&1

# Only delete the logfile if the update succeeded
if [ $UPDATESUCCESS = 0 -a $COMPILESUCCESS = 0 -o $UPDATESUCCESS = 1 ]; then
  rm -f $LOGFILE
fi

exit 0
