#!/bin/sh
# Allows start and forces stop without restart of kerio-connect-console
#
### BEGIN INIT INFO
# Provides:          kerio-console
# Required-Start:    $remote_fs $syslog $time
# Required-Stop:     $remote_fs $syslog $time
# Should-Start:
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Script controlling the kerio-connect-console daemon
# Description:       kerio-connect-console provides administration tools for
#                    managing kerio-connect application.
### END INIT INFO

DAEMON=/opt/kerio/mailserver/console/kerio-console

# Make sure Kerio Console is started with system locale
if [ -r /etc/default/locale ]; then
	. /etc/default/locale
	export LANG
	export LC_ALL=$LANG
fi

if [ ! -x $DAEMON ]; then
	echo "No kerio-connect-console package installed"
	exit 0
fi

case "$1" in
	start)
		echo "Allowing start of kerio-connect-console"
		rm -f /tmp/kerio-console.stop
		;;
	stop)
		echo "Stopping kerio-connect-console"
		touch /tmp/kerio-console.stop
		killall -q $DAEMON || true
		# wait for exit
		for i in 1 2 3 4 5; do
				pgrep -fx $DAEMON || break
				sleep 1
		done
		;;
	status)
		if [ -f /tmp/kerio-console.stop ]; then
			echo "kerio-connect-console is not running"
			exit 3
		else
			echo "kerio-connect-console is running"
			exit 0
		fi
		;;
	restart)
		$0 stop
		$0 start
		;;
	force-reload)
		# kerio console do not reloads
		;;
	*)
		echo "Usage: $0 {start|stop|status|restart|force-reload}"
		exit 2
		;;
esac
exit 0
