#!/bin/sh
#
### BEGIN INIT INFO
# Provides:          Savapi
# Required-Start:    $syslog $remote_fs
# Required-Stop:     $syslog $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Avira Savapi
# Description:       Start Avira Sapavi
#	needed for amavisd
### END INIT INFO
# 
SAVAPI_BIN=/usr/lib/iku-av/bin/savapi
test -x $SAVAPI_BIN || { echo "$SAVAPI_BIN not installed"; 
	if [ "$1" = "stop" ]; then exit 0;
	else exit 5; fi; }

# Check for existence of needed config file and read it
SAVAPI_CONFIG=/usr/lib/iku-av/bin/savapi3.conf
test -r $SAVAPI_CONFIG || { echo "$SAVAPI_CONFIG not existing";
	if [ "$1" = "stop" ]; then exit 0;
	else exit 6; fi; }

[ -e /etc/rc.status ] && . /etc/rc.status

# Reset status of this service
#rc_reset

case "$1" in
    start)
	echo -n "Starting SAVAPI "
	/usr/lib/iku-av/bin/license-tool
	start-stop-daemon --start --exec $SAVAPI_BIN -- --config=$SAVAPI_CONFIG  --socket-permissions=777
	# Remember status and be verbose
	#rc_status -v
	;;
    stop)
	echo -n "Shutting down SAVAPI "
	## Stop daemon with killproc(8) and if this fails
	## killproc sets the return value according to LSB.

	start-stop-daemon --stop --exec $SAVAPI_BIN
	#/sbin/killproc -TERM $SAVAPI_BIN

	# Remember status and be verbose
	#rc_status -v
	;;
    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
	;;
    reload)
	## Like force-reload, but if daemon does not support
	## signaling, do nothing (!)

	# If it supports signaling:
	$SAVAPI_BIN --reload-engine --config=$SAVAPI_CONFIG
	#touch /var/run/SAVAPI.pid
	#rc_status -v
	
	## Otherwise if it does not support reload:
	#rc_failed 3
	#rc_status -v
	;;
    status)
	echo -n "Checking for service SAVAPI "
	## Check status with checkproc(8), if process is running
	## checkproc will return with exit status 0.

	# Return value is slightly different for the status command:
	# 0 - service up and running
	# 1 - service dead, but /var/run/  pid  file exists
	# 2 - service dead, but /var/lock/ lock file exists
	# 3 - service not running (unused)
	# 4 - service status unknown :-(
	# 5--199 reserved (5--99 LSB, 100--149 distro, 150--199 appl.)
	
	# NOTE: checkproc returns LSB compliant status values.
	$SAVAPI_BIN --status --config=$SAVAPI_CONFIG
	# NOTE: rc_status knows that we called this init script with
	# "status" option and adapts its messages accordingly.
	#rc_status -v
	;;
    *)
	echo "Usage: $0 {start|stop|status|restart|reload}"
	exit 1
	;;
esac
echo
#rc_exit
