#!/bin/bash
# chkconfig:	2345 85 15
# description:	SOGo is a groupware server
# processname:  sogod
# config:	/etc/sysconfig/sogo
# config:	/etc/httpd/conf.d/SOGo.conf
# pidfile:	/var/run/sogo/sogod.pid

# SOGo init script for RedHat
#
# Copyright (C) 2007-2009 Inverse inc.
#
# Authors: Wolfgang Sourdeau <wsourdeau@inverse.ca>
#          Francis Lachapelle <flachapelle@inverse.ca>
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
PATH=/sbin:/bin:/usr/sbin:/usr/bin

NAME=sogo
DAEMON=/usr/sbin/sogod
DESC="SOGo"

USER=$NAME
PREFORK=3

PIDFILE=/var/run/$NAME/$NAME.pid
LOGFILE=/var/log/$NAME/$NAME.log

if [ -f /etc/sysconfig/$NAME ]; then
    . /etc/sysconfig/$NAME
fi

if [ ! -x $DAEMON ]; then
 echo "$DAEMON is not executable."
 exit 1
fi

checkDir() {
    directory="$1"
    if [ ! -d "$directory" ]
    then
	echo "$directory does not exist."
	exit 1
    fi

    if [ `/usr/bin/stat "$directory" -c %U` != "$USER" ]
    then
	echo "$directory is not owned by the '$USER' user."
	exit 1
    fi
}

checkDir /var/run/$NAME
checkDir /var/spool/$NAME
checkDir /var/log/$NAME

. /etc/rc.d/init.d/functions
if [ -z "$GNUSTEP_SYSTEM_ROOT" ]
then
  . /etc/GNUstep/GNUstep.conf
  . ${GNUSTEP_MAKEFILES}/GNUstep.sh
fi

DAEMON_OPTS="-WOWorkersCount $PREFORK -WOPidFile $PIDFILE -WOLogFile $LOGFILE"

RETVAL=0

start() {
    echo $"Starting $DESC: "
    pid="`cat $PIDFILE 2> /dev/null`"
    if [ -n "$pid" ]
    then
        pid="`ps --pid ${pid} -o pid=`"
	if [ -n "$pid" ]
	then
	    echo "  $NAME already running. Skipped."
	    RETVAL=0
	else
	    rm -f $PIDFILE
	    daemon --user="$USER" --pidfile="$PIDFILE" "$DAEMON" $DAEMON_OPTS
	    RETVAL=$?
	    echo "  $NAME (stale pid file removed)"
	fi
    else
	daemon --user="$USER" --pidfile="$PIDFILE" "$DAEMON" $DAEMON_OPTS
	RETVAL=$?
        echo "  $NAME"
    fi
    return $RETVAL
}

stop() {
    echo $"Stopping $DESC: "
    pid="`cat $PIDFILE 2> /dev/null`"
    if [ -n "$pid" ]
    then
        pid="`ps --pid ${pid} -o pid=`"
	if [ -n "$pid" ]
	then
	    kill $pid >& /dev/null
	    RETVAL=$?
	    echo "  $NAME stopped"
	else
	    echo "  $NAME not running"
	    RETVAL=0
	fi
    else
	echo "  $NAME not running"
	RETVAL=0
    fi
    return $RETVAL
}

restart() {
    echo $"Restarting $DESC: "
    pid="`cat $PIDFILE 2> /dev/null`"
    if [ -n "$pid" ]
    then
	kill $pid >& /dev/null
    fi
    sleep 1
    daemon --user="$USER" --pidfile="$PIDFILE" "$DAEMON" $DAEMON_OPTS
    echo "  $NAME"
}

case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	restart
	;;
  condrestart|try-restart)
	if status -p "$PIDFILE" $DAEMON >&/dev/null; then
	  restart
	fi
	;;
  status)
  	status -p "$PIDFILE" $DAEMON
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|condrestart|status}" >&2
	exit 1
	;;
esac

exit $?
