#!/bin/sh

# amavisd-new cronjob helper
#
# Run it as root or as the amavis user
#
# First parameter specifies which cronjob routine to run:
# 		sa-sync:	spamassassin fast sync
# 		sa-clean:	spamassassin cleanup

test -e /usr/bin/sa-learn || exit 0
test -e /usr/sbin/amavisd-new || exit 0

SUUSER="amavis"

set -e
umask 022

# WATCH OUT FOR PROPER QUOTING LEVEL WHEN CALLING THIS!
do_amavis_cmd() {
	if [ "$(id -u -n)" != "${SUUSER}" ]; then
		exec /bin/su -s /bin/sh - "${SUUSER}" -c "$*" >/dev/null
	else
		# to get the same quoting level as the su path
		CMD="$*"
		exec ${CMD} >/dev/null 
	fi
}

case $1 in
	sa-sync)
		do_amavis_cmd "/usr/bin/sa-learn --sync"
		;;
	sa-clean)
		do_amavis_cmd "/usr/bin/sa-learn --sync --force-expire"
		;;
	*)
		echo "$0: unknown cron routine $1" >&2
		exit 1
		;;
esac

exit 0
