#!/bin/bash

### BEGIN INIT INFO
# Provides:          plesk-milter
# Required-Start:    $syslog $local_fs $network postfix
# Required-Stop:     $syslog $local_fs $network
# Default-Start:     3 5
# Default-Stop:      0 1 6
# Short-Description: start and stop Plesk milter filter
# Description:       Plesk milter filter handle mail processing
### END INIT INFO

# Check for missing binaries (stale symlinks should not happen)
# Note: Special treatment of stop for LSB conformance
exec="/usr/lib/plesk-9.0/psa-pc-remote"
user="postfix"
group="popuser"
prog="psa-pc-remote"

test -x $exec || { echo "$exec not installed"; 
	if [ "$1" = "stop" ]; then exit 0;
	else exit 5; fi; }

# Read config
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

. /etc/rc.status

# Reset status of this service
rc_reset

case "$1" in
    start)
	echo -n "Starting $prog "
	/sbin/startproc -u "$user" -g "$group" -e $exec -p inet:${pc_milter_port:-12768}@localhost ${pc_milter_timeout+ -t $pc_milter_timeout} ${pc_milter_pop_before_smtp+ -b}
	rc_status -v
	;;
    stop)
	echo -n "Shutting down $prog "
	/sbin/killproc -t 30 $exec
	rc_status -v
	;;
    try-restart|condrestart)
	## Do a restart only if the service was active before.
	## Note: try-restart is now part of LSB (as of 1.9).
	## RH has a similar command named condrestart.
	if test "$1" = "condrestart"; then
		echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
	fi
	$0 status
	if test $? = 0; then
		$0 restart
	else
		rc_reset	# Not running is not a failure.
	fi
	# Remember status and be quiet
	rc_status
	;;
    restart)
	## Stop the service and regardless of whether it was
	## running or not, start it again.
	$0 stop
	$0 start

	# Remember status and be quiet
	rc_status
	;;
    force-reload)
	echo -n "Reload service $prog "
	$0 try-restart
	rc_status
	;;
    reload)
	rc_failed 3
	rc_status -v
	;;
    status)
	echo -n "Checking for service $prog "
	/sbin/checkproc $exec
	rc_status -v
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload"
	exit 1
	;;
esac
rc_exit
