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



DESC="Kerio Connect"         # this name is write to console
NAME=mailserver              # processname
PIDFILE=/var/run/kms.pid     # pidfile
DELAY=4                      # delay in active waiting (in start and stop)
STARTTIMEOUT=30              # max waiting on start (second)
STOPTIMEOUT=30               # max waiting on stop (second), we recommend min 30s



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

EXEC=$MAINDIR/$NAME


pidfileclean() {
	if [ -z "`pidof $NAME`" ] && [ -e $PIDFILE ]; then
		rm -f $PIDFILE
		echo
	fi
}

# Waiting to complete start
wait_until_start() {
	local delay

	sleep 1 # min waiting
	delay=$STOPTIMEOUT
	while [ $delay -gt 0 ] && [ -n "`pidof $NAME`" ] && [ ! -e $PIDFILE ]; do
		echo -n "."
		sleep $DELAY
		delay=$(( $delay - $DELAY ))
	done
	[ -z "`pidof $NAME`" ] && return 1 || return 0	
}

# kill with active waiting 30 second 
kill_process() {
	local delay pid
	
	delay=$STOPTIMEOUT

	if [ -r $PIDFILE ]; then
		pid=$( cat $PIDFILE )
	else
		pid=$( pidof $NAME )
		if  [ -z "$pid" ]; then
			# noting to kill
			return 1
		fi
	fi

	if checkpid $pid; then
		# TERM first, then KILL if not dead
		kill -TERM $pid >/dev/null 2>&1
		sleep 1
		while [ $delay -gt 0 ] && checkpid $pid; do
			sleep $DELAY;
			echo -n "."
			delay=$(( $delay - $DELAY ))
		done
		if checkpid $pid; then
			kill -KILL $pid >/dev/null 2>&1
		fi
		sleep 1
		checkpid $pid && return 1 || return 0
	fi
	return 1
}


if [ -f /etc/rc.d/init.d/functions ] ; then
#####################[ OS like REDHAT Linux init script ]#########

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

[ -x $EXEC ] || exit 0

RETVAL=0

start() {
	if [ -n "`pidof $NAME`" ]; then
		if [ -e $PIDFILE ]; then
			echo -n "$DESC is already running:"
			success
			echo
		else
			echo -n "$DESC is already starting or shutting down."
			echo
		fi
		return $RETVAL
	fi
	
	pidfileclean
	echo -n "Starting $DESC: "
	ulimit -c unlimited
	ulimit -s 2048
	ulimit -n 4096
	if ldd $EXEC 2>&1 >/dev/null | grep -sq libstdc++; then
	    export LD_LIBRARY_PATH=$(dirname $EXEC):$LD_LIBRARY_PATH
	fi
	$EXEC $MAINDIR
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
		# Waiting to complete start
		wait_until_start
		RETVAL=$?
	fi
	[ $RETVAL -eq 0 ] && success || failure
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/kerio-connect
	return $RETVAL
}

stop() {
	status mailserver> /dev/null
	RETVAL=$?
	if [ $RETVAL == 0 ]; then 
	    pidfileclean
	    echo -n "Shutting down $DESC: "
	    # We can't use 'killproc -p $PIDFILE -d 30 $NAME' therefore this function
	    # don't waiting active. Function simply try again after -d second.
	    kill_process
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] && success || failure
	    echo
	    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/kerio-connect
	    return $RETVAL
	fi
	
}

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

exit $RETVAL

else
#####################[ OS like SUSE Linux init script ]#########

[ -x $EXEC ] || exit 5

. /etc/rc.status

# Help for function from: /etc/rc.status:
#  rc_check         check and set local and overall rc status
#  rc_status        check and set local and overall rc status
#  rc_status -v     ditto but be verbose in local rc status
#  rc_status -v -r  ditto and clear the local rc status
#  rc_failed        set local and overall rc status to failed
#  rc_reset         clear local rc status (overall remains)
#  rc_exit          exit appropriate to overall rc status

# First reset status of this service
rc_reset

case "$1" in
    start)
	if [ -n "`pidof $NAME`" ]; then
		if [ -e $PIDFILE ]; then
			echo -n "$DESC is already running:"
		else
			echo -n "$DESC is already starting or shutting down."
			echo
			exit 0
		fi
		rc_status -v && rc_exit
	fi	

	pidfileclean
	echo -n "Starting $DESC: "

	ulimit -c unlimited
	ulimit -s 2048
	ulimit -n 4096
	startproc $EXEC $MAINDIR
	RETVAL=$?

	if [ $RETVAL -eq 0 ]; then
		# Waiting to complete start
		wait_until_start
		RETVAL=$?
	fi
	[ $RETVAL -eq 0 ] || rc_failed
	rc_status -v 
	;;

    stop)
	checkproc $EXEC 
	RETVAL=$?
	if [ $RETVAL == 0 ]; then 
	    pidfileclean
	    echo -n "Shutting down $DESC: "
	    killproc -t $STOPTIMEOUT -TERM $EXEC
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] || rc_failed
	    rc_status -v
	fi
	;;

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

    restart)
   	$0 stop
	$0 start
	rc_status
	;;

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

    reload)
   	echo -n "Reloading $DESC: "
	rc_failed 3
	rc_status -v
	;;

    status)
	echo -n "Status of $DESC: "
   	checkproc $EXEC
	rc_status -v
	;;

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

fi
