#!/bin/bash
#
#  daily update of SpamAssasin rules (used with clamav & amavis)
#
# ::copy::
# ::maintainer::
#
CMD="sa-update"

# options default
#
OPTIONS=
CFGDIR="/etc/spamassassin"
CHANNELFILE="$CFGDIR/channels"
KEYFILE="$CFGDIR/gpgkeys"
LOGFILE="/var/log/$CMD.log"

[ -f /etc/default/$CMD ] && . /etc/default/$CMD

cfile_opt=
kfile_opt=

[ "X$CHANNELFILE" != "X" ] && cfile_opt="--channelfile"
[ "X$KEYFILE" != "X" ] && kfile_opt="--gpgkeyfile"

echo "starting on `date`" >>$LOGFILE

$CMD $OPTIONS $cfile_opt $CHANNELFILE $kfile_opt $KEYFILE >>$LOGFILE 2>&1

stat=$?

echo "$CMD returned $stat" >>$LOGFILE

[ $stat -gt 4 ] && {
	echo "ERROR $stat, abort" >>$LOGFILE
	logger "[ku] sa-update-cron ERROR $stat -- check script"
	exit $stat
}

[ $stat == 0 ] && {
	echo "updates detected, restarting amavis daemon" >>$LOGFILE
	chown -R amavis:amavis /var/lib/spamassassin
	/etc/init.d/amavis restart >>$LOGFILE 2>&1
}

exit 0
