#!/bin/sh

# Duncan Findlay
# duncf@debian.org

# Daily cronjob for SpamAssassin updates. This isn't pretty but it
# should do the job.

CHRBIN=/usr/sbin/chroot
CHRDIR=/chroot/maild

CRON=0

test -f $CHRDIR/etc/default/spamassassin && . $CHRDIR/etc/default/spamassassin

test -x $CHRDIR/usr/bin/sa-update || exit 0

if [ "$ENABLED" = "1" ] ; then
    test -x /etc/init.d/spamassassin-chroot || exit 0
fi

if [ "$CRON" = "0" ] ; then
    exit 0
fi

# If there's a problem with the ruleset or configs, print the output
# of spamassassin --lint (which will typically get emailed to root)
# and abort.
die_with_lint() {
    env -i LANG="$LANG" PATH="$PATH" start-stop-daemon \
	--chroot $CHRDIR \
        --chuid debian-spamd:debian-spamd --start \
        --exec /usr/bin/spamassassin -- -D --lint 2>&1
    exit 1
}

do_compile() {
# Compile rules if the required tools are available. Prior to version
# 3.3.2-8, there was an additional check to verify that an sa-compile
# run had previously been executed by hand. With sa-learn now
# distributed in a separate, optional, package, this check is no
# longer necessary.
    if [ -x $CHRDIR/usr/bin/re2c -a -x $CHRDIR/usr/bin/sa-compile ]; then

        env -i LANG="$LANG" PATH="$PATH" start-stop-daemon \
	    --chroot $CHRDIR \
            --chuid debian-spamd:debian-spamd --start \
            --exec /usr/bin/sa-compile -- --quiet

        # Fixup perms -- group and other should be able to
        # read and execute, but never write.  Works around
        # sa-compile's failure to obey umask.
	chmod -R go-w,go+rX $CHRDIR/var/lib/spamassassin/compiled

    fi
}

# Tell a running spamd to reload its configs and rules.
reload() {
    # Reload
    if [ "$ENABLED" = "1" ] ; then
	if which invoke-rc.d >/dev/null 2>&1; then
	    invoke-rc.d spamassassin reload > /dev/null
	else
	    /etc/init.d/spamassassin reload > /dev/null
	fi
    fi

    if [ -d $CHRDIR/etc/spamassassin/sa-update-hooks.d ]; then
        $CHRBIN $CHRDIR run-parts --lsbsysinit /etc/spamassassin/sa-update-hooks.d
    fi
}

compiled_writable() {
	# @3G change to user (it was root)
	chown debian-spamd:debian-spamd $CHRDIR/var/lib/spamassassin/compiled -R
}

compiled_protect() {
	# @3G chown compiled dir back to root
	chown root.root $CHRDIR/var/lib/spamassassin/compiled -R
}

# Sleep for up to 3600 seconds if not running interactively
#if [ ! -t 0 ]; then
#    RANGE=3600
#    number=`od -vAn -N2 -tu4 < /dev/urandom`
#    number=`expr $number "%" $RANGE`
#    sleep $number
#fi
sleep 3

# Update
#umask 022
compiled_writable

env -i LANG="$LANG" PATH="$PATH" start-stop-daemon \
    --chroot $CHRDIR \
    --chuid debian-spamd:debian-spamd --start \
    --exec /usr/bin/sa-update -- \
    --gpghomedir /var/lib/spamassassin/sa-update-keys 2>&1

case $? in
    0)
	# got updates!
	env -i LANG="$LANG" PATH="$PATH" start-stop-daemon \
		--chroot $CHRDIR \
		--chuid debian-spamd:debian-spamd --start \
		--exec /usr/bin/spamassassin -- --lint 2>&1 || die_with_lint
	do_compile
	compiled_protect
	reload
	;;
    1)
	# no updates
	compiled_protect
	exit 0
	;;
    2)
	# lint failed!
	compiled_protect
	die_with_lint
	;;
    *)
	compiled_protect
	echo "sa-update failed for unknown reasons" 1>&2
	;;
esac

# Local variables:
# mode: shell-script
# tab-width: 4
# indent-tabs-mode: nil
# end:
