#!/bin/bash
#
# keriomailserver  Script to start and stop Kerio MailServer
#
# chkconfig: 2345 79 30
# description: Kerio MailServer
# processname: mailserver
# pidfile: /var/run/kms.pid
#
### BEGIN INIT INFO
# Provides: keriomailserver
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: Start the Kerio MailServer
### END INIT INFO

if [ -f /etc/rc.d/init.d/functions ] ; then

. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/keriomailserver ] ; then
	. /etc/sysconfig/keriomailserver
else
	MAINDIR=/opt/kerio/mailserver
fi

[ -f $MAINDIR/mailserver ] || exit 0

RETVAL=0

start() {
	echo -n "Starting Kerio MailServer: "
	if [ -f /usr/local/bin/.uniprodloc ] ; then
		if [ -d `cat /usr/local/bin/.uniprodloc` ] ; then
			export LD_LIBRARY_PATH=`cat /usr/local/bin/.uniprodloc`/ino/config:`cat /usr/local/bin/.uniprodloc`/ino/lib
			. `cat /usr/local/bin/.uniprodloc`/scripts/envset
		fi
	elif [ -f /opt/CA/.uniprodloc ] ; then
		if [ -d `cat /opt/CA/.uniprodloc` ] ; then
			export LD_LIBRARY_PATH=`cat /opt/CA/.uniprodloc`/ino/config:`cat /opt/CA/.uniprodloc`/ino/lib
			. `cat /opt/CA/.uniprodloc`/scripts/envset
		fi
	fi
	daemon $MAINDIR/mailserver $MAINDIR
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/keriomail
	return $RETVAL
}

stop() {
	echo -n "Shutting down Kerio MailServer: "
	killproc mailserver -TERM
	RETVAL=$?
	echo
	# Wait up to 15 seconds for KMS to actually stop
	for x in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
		if [ -a /var/run/kms.pid ]; then
			sleep 1;
		else
			break;
		fi
	done
	# Kill KMS if it hasn't stopped yet
	[ -a /var/run/kms.pid ] && kill -9 `cat /var/run/kms.pid`

	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/keriomail
	return $RETVAL
}

case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|reload)
	stop
	start
	RETVAL=$?
	;;
  condrestart)
	if [ -f /var/lock/subsys/keriomail ]; then
	    stop
	    start
	    RETVAL=$?
	fi
	;;
  status)
	status mailserver
	RETVAL=$?
	;;
  *)
	echo "Usage: keriomailserver {start|stop|restart|condrestart|status}"
	exit 1
esac

exit $RETVAL

else

if [ -f /etc/sysconfig/keriomailserver ] ; then
	. /etc/sysconfig/keriomailserver
else
	MAINDIR=/opt/kerio/mailserver
fi

[ -f $MAINDIR/mailserver ] || exit 5

. /etc/rc.status

rc_reset

case "$1" in
    start)
	echo -n "Starting Kerio MailServer: "
	startproc $MAINDIR/mailserver $MAINDIR
	rc_status -v
	;;

    stop)
	echo -n "Shutting down Kerio MailServer: "
	killproc mailserver
	rc_status -v
	;;

    try-restart)
        $0 status > /dev/null && $0 restart
	rc_status
	;;

    restart)
        $0 stop
	$0 start
	rc_status
	;;

    force-reload)
        echo -n "Reloading Kerio MailServer: "
	$0 restart
	rc_status -v
	;;

    reload)
        echo -n "Reloading Kerio MailServer: "
	rc_failed 3
	rc_status -v
	;;

    status)
	echo -n "Status of Kerio MailServer: "
        checkproc $MAINDIR/mailserver
	rc_status -v
	;;

    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload}"
	exit 1
esac
rc_exit

fi
