#! /bin/bash
### BEGIN INIT INFO
# Provides:          open-xchange-groupware
# Required-Start:    $local_fs $remote_fs $network $syslog mysql
# Required-Stop:     $local_fs $remote_fs $network $syslog mysql
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/stop open-xchange-groupware
### END INIT INFO

# Open-Xchange Groupware Initscript

STARTSCRIPT=/opt/open-xchange/sbin/open-xchange-groupware
NAME=open-xchange-groupware
DESC="Open-Xchange Groupware"
DEFAULTS=/etc/default/open-xchange-groupware
OXFUNCTIONS=/opt/open-xchange/etc/oxfunctions.sh
OXCONFIG=/opt/open-xchange/etc/groupware/ox-scriptconf.sh

# Defaults - don't touch, edit /etc/default/open-xchange-groupware
ENABLED=0

test -f $DEFAULTS && . $DEFAULTS
if [ ! -f $OXFUNCTIONS ]; then
	echo "$DESC: $OXFUNCTIONS missing, exiting"
	exit 0
fi
if [ ! -f $OXCONFIG ]; then
	echo "$DESC: $OXCONFIG missing, exiting"
	exit 0
fi

. $OXFUNCTIONS
. $OXCONFIG

if [ "$ENABLED" = "0" ]; then
    echo "$DESC: disabled, see $DEFAULTS"
    exit 0
fi

ulimit -n $NRFILES


case "$1" in
  start)
	echo -n "Starting $DESC: "
	ox_start_daemon $STARTSCRIPT $NAME open-xchange open-xchange
	echo "$NAME."
	;;

  stop)
	echo -n "Stopping $DESC: "
	ox_stop_daemon $STARTSCRIPT $NAME
	echo "$NAME."
	;;

  restart|force-reload)
	echo -n "Restarting $DESC: "
	ox_restart_daemon $STARTSCRIPT $NAME open-xchange open-xchange
	echo "$NAME."
	;;

  status)
	echo -n "Checking for $DESC: "
	if ox_daemon_status /var/run/open-xchange-groupware.pid ;then
	    echo "running."
	else
	    echo "not running"
	fi
	;;

  *)
	N=/etc/init.d/$SNAME
	echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
	exit 1
	;;
esac

exit 0
