#!/bin/sh
# $Id: template,v 1.3 2003/05/21 17:47:00 ldv Exp $
#
# Startup script for open-xchange.
#
# chkconfig: 345 80 20
# description:	open-xchange - The Collaboration and Integration Environment

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

OX_USER=ox

S_PIDFILE=/var/run/open-xchange/sessiond.pid
S_LOCKFILE=/var/lock/subsys/open-xchange-sessiond

G_PIDFILE=/var/run/open-xchange/groupware.pid
G_LOCKFILE=/var/lock/subsys/open-xchange-groupware

W_PIDFILE=/var/run/open-xchange/webmail.pid
W_LOCKFILE=/var/lock/subsys/open-xchange-webmail

RETVAL=0

start()
{

    local STARTUP_OPTS

    msg_starting open-xchange-sessiond
    start_daemon --pidfile "$S_PIDFILE" --lockfile "$S_LOCKFILE" --set-user "$OX_USER" --no-announce \
        -- openexchange-sessiond

    msg_starting open-xchange-groupware
    start_daemon --pidfile "$G_PIDFILE" --lockfile "$G_LOCKFILE" --set-user "$OX_USER" --no-announce \
        -- openexchange-groupware

    msg_starting open-xchange-webmail
    start_daemon --pidfile "$W_PIDFILE" --lockfile "$W_LOCKFILE" --set-user "$OX_USER" --no-announce \
        -- openexchange-webmail

    RETVAL=$?
    return $RETVAL
}

stop()
{
    msg_stopping open-xchange-webmail
    if [ -f "$W_PIDFILE" ]; then
	if [ -n "$(ps --no-headers p $(cat $W_PIDFILE))" ]; then
		kill $(cat $W_PIDFILE)
		echo_success
		echo -e "\r"
	else
		echo "open-xchange-webmail is not running, but pid-file exist"
		echo_passed
		echo -e "\r"
	fi
	rm -f "$W_PIDFILE"
	rm -f "$W_LOCKFILE"
    else
	echo "open-xchange-webmail is not running"
	echo_failure
	echo -e "\r"
    fi

    msg_stopping open-xchange-groupware
    if [ -f "$G_PIDFILE" ]; then
	if [ -n "$(ps --no-headers p $(cat $G_PIDFILE))" ]; then
		kill $(cat $G_PIDFILE)
		echo_success
		echo -e "\r"
	else
		echo "open-xchange-groupware is not running, but pid-file exists"
		echo_passed
		echo -e "\r"
	fi
	rm -f "$G_PIDFILE"
	rm -f "$G_LOCKFILE"
    else
	echo "open-xchange-groupware is not running"
	echo_failure
	echo -e "\r"
    fi

    msg_stopping open-xchange-sessiond
    if [ -f "$S_PIDFILE" ]; then
	if [ -n "$(ps --no-headers p $(cat $S_PIDFILE))" ]; then
		kill $(cat $S_PIDFILE)
		echo_success
		echo -e "\r"
	else
		echo "open-xchange-sessiond is not running, but pid-file exists"
		echo_passed
		echo -e "\r"
	fi
	rm -f "$S_PIDFILE"
	rm -f "$S_LOCKFILE"
    else
	echo "open-xchange-sessiond is not running"
	echo_failure
	echo -e "\r"
    fi

    RETVAL=$?
    return $RETVAL
}

status()
{
    if [ -f "$S_PIDFILE" ]; then
        if [ -n "$(ps --no-headers p $(cat $S_PIDFILE))" ]; then
            echo "open-xchange-sessiond is running"
        else
            echo "open-xchange-sessiond is not running, but pid-file exists"
        fi
    else
            echo "open-xchange-sessiond is not running"
    fi

    if [ -f "$G_PIDFILE" ]; then
        if [ -n "$(ps --no-headers p $(cat $G_PIDFILE))" ]; then
            echo "open-xchange-groupware is running"
        else
            echo "open-xchange-groupware is not running, but pid-file exists"
        fi
    else
            echo "open-xchange-groupware is not running"
    fi


    if [ -f "$W_PIDFILE" ]; then
        if [ -n "$(ps --no-headers p $(cat $W_PIDFILE))" ]; then
            echo "open-xchange-webmail is running"
        else
            echo "open-xchange-webmail is not running, but pid-file exists"
        fi
    else
            echo "open-xchange-webmail is not running"
    fi

    RETVAL=$?
    return $RETVAL
}

# See how we were called.
case "$1" in
    start)
	start
        ;;
    stop)
	stop
        ;;
    restart|reload)
	stop
	start
	;;
    condstop)
	# Stop the servce if it is already running, for example:
	if [ -e "$S_LOCKFILE" ]; then
	    stop
	fi
	;;
    condrestart)
	# Restart the servce if it is already running, for example:
	if [ -e "$S_LOCKFILE" ]; then
	    stop
	    start
	fi
	;;
    status)
	status
	;;
    *)
	echo "Usage: ${0##*/} {start|stop|reload|restart|condstop|condrestart|status}"
	RETVAL=1
esac

exit $RETVAL
