#!/bin/bash
#
# zarafa-server Zarafa Collaboration Platform's Storage Server
#
# chkconfig: - 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

SERVERCONFIG=/etc/zarafa/server.cfg
SERVERPROGRAM=/usr/bin/zarafa-server

# 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

# Source function library.
. /etc/rc.d/init.d/functions
if [ -z "$ZARAFA_LOCALE" ]; then
	ZARAFA_LOCALE="C"
fi

RETVAL=0
server=`basename $SERVERPROGRAM`
lockfile=/var/lock/subsys/$server
pidfile=/var/run/$server.pid

start() {
	# Start in background, always succeeds
	echo -n $"Starting $server: "
	export LC_ALL=$ZARAFA_LOCALE
	export LANG=$ZARAFA_LOCALE
	daemon $SERVERPROGRAM $SERVERCONFIG_OPT
	RETVAL=$?
	unset LC_ALL LANG
	echo
	[ $RETVAL -eq 0 ] && touch $lockfile

	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() {
	echo -n $"Restarting $server: "
	killproc $SERVERPROGRAM -SIGHUP
	RETVAL=$?
	echo

	return $RETVAL
}

# See how we were called.
case "$1" in
    start)
		start
		;;
    stop)
		stop
		;;
    status)
		status $server
		RETVAL=$?
		;;
    restart|force-reload)
		restart
		;;
    condrestart|try-restart)
		if [ -f ${pidfile} ]; then
			stop
			start
		fi
		;;
    reload)
		reload
		;;
    *)
		echo $"Usage: $server {start|stop|status|reload|restart|condrestart|force-reload|try-restart}"
		RETVAL=1
		;;
esac

exit $RETVAL
