#!/bin/bash
#
# zarafa-server Zarafa Collaboration Platform's Storage Server
#
# chkconfig: 345 85 25
# description: The Zarafa Server takes MAPI calls in SOAP over HTTP(S) or \
#              the unix socket. It authenticates users using one of three \
#              authentication backends (unix/pam, db, ldap). It stores the data \
#              in a MySQL instance, and optionally stores the attachments directly \
#              on the filesystem.
# processname: /usr/bin/zarafa-server
# config: /etc/zarafa/server.cfg
# pidfile: /var/run/zarafa-server.pid

### BEGIN INIT INFO
# Provides: zarafa-server
# Required-Start: $local_fs $network $remote_fs $syslog
# Required-Stop: $local_fs $network $remote_fs $syslog
# Should-Start: mysqld
# Should-Stop: mysqld
# Short-Description: Zarafa Collaboration Platform's Storage Server
# Description: The Zarafa Server takes MAPI calls in SOAP over HTTP(S) or
#              the unix socket. It authenticates users using one of three
#              authentication backends (unix/pam, db, ldap). It stores the data
#              in a MySQL instance, and optionally stores the attachments directly
#              on the filesystem.
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

SERVERCONFIG=/etc/zarafa/server.cfg
SERVERPROGRAM=/usr/bin/zarafa-server
server=`basename $SERVERPROGRAM`
LOCKFILE=/var/lock/subsys/$server
PIDFILE=/var/run/$server.pid
RETVAL=0

# Sanity checks.
[ -x $SERVERPROGRAM ] || exit 0

SERVERCONFIG_OPT=""
[ ! -z $SERVERCONFIG -a -f $SERVERCONFIG ] && SERVERCONFIG_OPT="-c $SERVERCONFIG"

[ -f /etc/sysconfig/zarafa ] && . /etc/sysconfig/zarafa
export ZARAFA_USERSCRIPT_LOCALE

if [ -z "$ZARAFA_LOCALE" ]; then
	ZARAFA_LOCALE="C"
fi

start() {
	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" $server
	RETVAL=$?

	return $RETVAL
}

stop() {
	if [ -f /tmp/zarafa-upgrade-lock ]; then
		echo
		echo "Zarafa Server database upgrade is taking place."
		echo "Do not stop this process bacause it may render your database unusable."
		echo
		exit 1
	fi
	# Cannot use killproc, because it has no timeout feature;
	# zarafa-server may take up to 60 seconds to shutdown.
	ZARAFAPID=`cat /var/run/zarafa-server.pid 2>/dev/null`
	if [ -z "$ZARAFAPID" -o ! -n "$ZARAFAPID" ]; then
		echo -n "Program ID of $server not found, trying to stop anyway: "
		killall $SERVERPROGRAM >/dev/null 2>&1
		RETVAL=$?
		echo
		if [ $RETVAL -eq 0 ]; then
			failure $"Stopping $server: "
		else
			success $"Stopping $server: "
		fi
		RETVAL=0
	else
		echo -n $"Stopping $server: "
		TIMEOUT=65
		/bin/kill $ZARAFAPID >/dev/null 2>&1
		if [ $? -eq 0 ]; then
			while [ $TIMEOUT -gt 0 ]; do
				/bin/kill -0 $ZARAFAPID >/dev/null 2>&1 || break
				sleep 1
				TIMEOUT=$[${TIMEOUT}-1]
			done
			if [ $TIMEOUT -eq 0 ]; then
				failure $"Timeout on stopping $server"
				RETVAL=1
			else
				success $"Stopping $server: "
				RETVAL=0
			fi
		else
			failure $"Stopping $server: "
		fi
		echo
	fi
	[ $RETVAL -eq 0 ] && rm -f $lockfile $pidfile

	return $RETVAL
}

restart() {
	stop
	start
}

reload() {
	msg_reloading $server
	stop_daemon --pidfile "$PIDFILE" -HUP -- $server
	RETVAL=$?

	return $RETVAL
}

# See how we were called.
case "$1" in
    start)
		start
		;;
    stop)
		stop
		;;
    reload)
		reload
		;;
    restart|force-reload)
		restart
		;;
    condstop)
		if [ -e "$LOCKFILE" ]; then
		stop
		fi
		;;
    condrestart|try-restart)
		if [ -e "$LOCKFILE" ]; then
		restart
		fi
		;;
    condreload)
		if [ -e "$LOCKFILE" ]; then
		reload
		fi
		;;
    status)
		status --pidfile "$PIDFILE" $server
		RETVAL=$?
		;;
    *)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL
