#!/bin/sh
#
# plesk-spamassassin: starts and stops Plesk-supporting spamassassin daemon
#
# chkconfig: 345 81 29
#
# description: Integration of the Plesk Server Administrator and SpamAssassin \
#              service.
#
### BEGIN INIT INFO
# Provides:       psa-spamassassin
# Required-Start: psa
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
### END INIT INFO

# Source function library.
[ -f /etc/init.d/functions ] && . /etc/init.d/functions

# Source networking configuration.
if [ -f /etc/sysconfig/network ]; then
	. /etc/sysconfig/network

	# Check that networking is up.
	[ "X${NETWORKING}" = "Xno" ] && exit 0
fi

[ -f /usr/local/psa/admin/bin/spamd ] || exit 0

# this way is for SuSE Linux
test -s /etc/rc.status && . /etc/rc.status && rc_reset

report_action()
{
	# $1 - text to put
	# $2 - status of operation
	case `type -t action` in
		function)
			action $"$1" $2
			;;
		*)
			case `type -t rc_reset` in
				function)
					# suse linux reporting style
					rc_reset
					echo -n $"$1"
					$2
					rc_status -v
					;;
				*)
					# debian style, probably useful for other systems
					echo -n $"$1"
					$2
					if [ $? -eq 0 ]; then
						echo "done"
					else
						echo "failed"
					fi
					;;
			esac
	esac
}

SPAMD_PIDFILES_D=/var/run/spamd
mkdir -p $SPAMD_PIDFILES_D
chown popuser $SPAMD_PIDFILES_D
chmod 755 $SPAMD_PIDFILES_D

case "$1" in

	start)
		echo -n 
		/usr/local/psa/admin/bin/spamd --start > /dev/null
		RETVAL=$?
		if [ $RETVAL = 0 ]; then
			result="/bin/true"
		else
			result="/bin/false"
		fi
		report_action $"Starting psa-spamassassin service: " $result
		;;

	stop)
		/usr/local/psa/admin/bin/spamd --stop > /dev/null
		RETVAL=$?
		if [ $RETVAL = 0 ]; then
			result="/bin/true"
		else
			result="/bin/false"
		fi
		report_action $"Shutting down psa-spamassassin service: " $result
		;;

	restart)
		$0 stop
		$0 start
		;;

	status)
		/usr/local/psa/admin/bin/spamd --status
		;;

	*)
		echo "Usage: $0 {start|stop|restart|status}"
		exit 1

esac

exit 0
