#!/bin/bash
#
# zarafa-ical Zarafa Collaboration Platoform's iCal Gateway
#
# chkconfig: 345 86 24
# description: The Zarafa iCal Gateway allows users \
#              to access their calender using the iCal or CalDAV protocol.
# processname: /usr/bin/zarafa-ical
# config: /etc/zarafa/ical.cfg
# pidfile: /var/run/zarafa-ical.pid

### BEGIN INIT INFO
# Provides: zarafa-ical
# 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 Platoform's iCal Gateway
# Description: The Zarafa iCal Gateway allows users
#              to access their calender using the iCal or CalDAV protocol.
### END INIT INFO

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

ICALCONFIG=/etc/zarafa/ical.cfg
ICALPROGRAM=/usr/bin/zarafa-ical
ical=`basename $ICALPROGRAM`
LOCKFILE=/var/lock/subsys/$ical
PIDFILE=/var/run/$ical.pid
RETVAL=0

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

ICALCONFIG_OPT=""
[ ! -z $ICALCONFIG -a -f $ICALCONFIG ] && ICALCONFIG_OPT="-c $ICALCONFIG"

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

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

	return $RETVAL
}

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

	return $RETVAL
}

restart() {
	stop
	start
}

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

exit $RETVAL
