#!/bin/sh
#
# citadel Citadel Groupware Core
#
# chkconfig:	2345 20 80
# description:	control citadel server start at boot time

# Copyright (c) 2011 Stefan Jakobs
# Author: Stefan Jakobs
#
#    This program 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 of the License, or
#    (at your option) any later version.
#
#    This program 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; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#    MA 02110-1301 USA.
#
# /etc/init.d/citadel

### BEGIN INIT INFO
# Provides:          citadel
# Required-Start:    $local_fs $remote_fs $syslog
# Required-Stop:     $local_fs $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: control citadel server start at boot time
# Description:       control citadel server start at boot time
### END INIT INFO

# uncomment this to create coredumps as described in
# http://www.citadel.org/doku.php/faq:mastering_your_os:gdb#how.do.i.make.my.system.produce.core-files
# ulimit -c unlimited

DESC="Citadel Groupware "
NAME=citserver
RUNDIR=/var/run/citadel
DAEMON=/usr/sbin/$NAME
PIDFILE=$RUNDIR/citadel.pid
SENDCOMMAND=/usr/sbin/sendcommand
LOCKFILE=/var/lock/subsys/$NAME
SETUP=/usr/sbin/citadel-setup
TIMEOUT=60

# Exit if the package is not installed
[ -x "$DAEMON" ] || { echo "$DAEMON not installed"; exit 1; }

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

# Read configuration variable file if it is present
[ -r /etc/sysconfig/citadel ] || { echo "citadel sysconfig file is not installed";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 1; fi; }
. /etc/sysconfig/citadel

if [[ -n $CITADEL_HOME_DIR ]]; then
    CITADEL_DAEMON_ARGS="$CITADEL_DAEMON_ARGS $CITADEL_HOME_DIR"
fi

start() {
    [ -x $DAEMON ] || exit 5
    echo -n $"Starting $DESC: "
    pidofproc -p $PIDFILE $DAEMON &> /dev/null && exit 0
    daemon $DAEMON $CITADEL_DAEMON_ARGS \
        -l$CITADEL_LOG_FACILITY $CITADEL_DEBUG_AREA
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $LOCKFILE
    return $retval
}

stop() {
    echo -n "Stopping $DESC: "
    killproc -d $TIMEOUT $DAEMON 
    retval=$?
    if [ $retval -eq 0 ] ; then
        rm -f $LOCKFILE
        rm -f $PIDFILE
    else
        echo -en "\t\t\t\t   [ FAIL ]"
    fi
    echo
    return $retval
}

restart() {
    stop
    start
}

reload() {
    echo -n "Reloading $DESC "
    $SENDCOMMAND "DOWN 1" 2>&1|grep '^200 '>/dev/null 
    retval=$?
    if [ $retval -eq 0 ] ; then
        echo -e "\t\t\t\t   [  OK  ]"
    else
        echo -e "\t\t\t\t   [ FAIL ]"
    fi
    return $retval
}

setup() {
    if ! rh_status_q ; then
       restart
    fi	    
    $SETUP
}

force_reload() {
    restart
}

rh_status() {
    # run checks to determine if the service is running or use generic status
    status $DAEMON
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    setup)
        setup
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|setup}"
        exit 2
esac
exit $?

