#! /bin/sh
#
### BEGIN INIT INFO
# Provides:          zarafa-gateway
# Required-Start:    $syslog $network $remote_fs
# Required-Stop:     $syslog $network $remote_fs
# Should-Start:      zarafa-server
# Should-Stop:       zarafa-server
# Default-Start:     2 3 5
# Default-Stop:      0 1 6
# X-UnitedLinux-Default-Enabled:   yes
# 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

. /etc/rc.status
rc_reset

GATEWAYCONFIG=/etc/zarafa/gateway.cfg
GATEWAYPROGRAM=/usr/sbin/zarafa-gateway

# Sanity checks.
#[ -f $GATEWAYCONFIG ] || exit 0
[ -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

gateway=`basename $GATEWAYPROGRAM`

case "$1" in
    start)
		echo -n "Starting Zarafa gateway "
		install -dm0775 -o zarafa -g zarafa /var/run/zarafad
		export LC_ALL=$ZARAFA_LOCALE
		export LANG=$ZARAFA_LOCALE
		$GATEWAYPROGRAM $GATEWAYCONFIG_OPT
		rc_status -v
		unset LANG LC_ALL
		;;

    stop)
		echo -n "Stopping Zarafa gateway "
		killproc $gateway
		rc_status -v
		;;

    restart)
		$0 stop
		$0 start
		;;

	reload)
		echo -n "Restarting Zarafa gateway: "
		killproc $GATEWAYPROGRAM -SIGHUP
		rc_status -v
		;;

    status)
		echo -n "Checking for Zarafa gateway "
		checkproc $gateway
		rc_status -v
		;;

    *)
		;;
esac

rc_exit
