#!/bin/bash
#
# zarafa-gateway Zarafa Collaboration Platform's POP3/IMAP Gateway
#
# chkconfig: 345 86 24
# description: The Zarafa Gateway allows users \
#              to access their email using the POP3 or IMAP protocol.
# processname: /usr/bin/zarafa-gateway
# config: /etc/zarafa/gateway.cfg
# pidfile: /var/run/zarafa-gateway.pid

### BEGIN INIT INFO
# Provides: zarafa-gateway
# Required-Start: $local_fs $network $remote_fs $syslog
# Required-Stop: $local_fs $network $remote_fs $syslog
# Should-Start: zarafa-server
# Should-Stop: zarafa-server
# Short-Description: Zarafa Collaboration Platform's POP3/IMAP Gateway
# Description: The Zarafa Gateway allows users
#              to access their email using the POP3 or IMAP protocol.
### END INIT INFO

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

GATEWAYCONFIG=/etc/zarafa/gateway.cfg
GATEWAYPROGRAM=/usr/bin/zarafa-gateway
gateway=`basename $GATEWAYPROGRAM`
LOCKFILE=/var/lock/subsys/$gateway
PIDFILE=/var/run/$gateway.pid
RETVAL=0

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

GATEWAYCONFIG_OPT=""
[ ! -z $GATEWAYCONFIG -a -f $GATEWAYCONFIG ] && GATEWAYCONFIG_OPT="-c $GATEWAYCONFIG"

[ -f /etc/sysconfig/zarafa ] && . /etc/sysconfig/zarafa
if [ -z "$ZARAFA_LOCALE" ]; then
	ZARAFA_LOCALE="C"
fi

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

	return $RETVAL
}

stop() {
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" $gateway
	RETVAL=$?

	return $RETVAL
}

restart() {
	stop
	start
}

reload() {
	msg_reloading $gateway
	stop_daemon --pidfile "$PIDFILE" -HUP -- $gateway
	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" $gateway
		RETVAL=$?
		;;
    *)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL
