#!/bin/sh
#
# Copyright (c) 2012 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# based on the template by SuSE Linux and Wilfried Goesgens
#
# /etc/init.d/citadel
#
### BEGIN INIT INFO
# Provides:          citadel
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 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
SETUP=/usr/sbin/citadel-setup
TIMEOUT=60

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

# 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

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num><num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
. /etc/rc.status

# Reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running
# 
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.

case "$1" in
  start)
      echo -n "Starting $DESC"
      /sbin/startproc -p $PIDFILE $DAEMON $CITADEL_DAEMON_ARGS \
         -l$CITADEL_LOG_FACILITY $CITADEL_DEBUG_AREA

      # Remember status and be verbose
      rc_status -v
      ;;
  stop)
      echo -n "Shutting down $DESC"
      /sbin/killproc -t $TIMEOUT -p $PIDFILE $DAEMON

      # Remember status and be quiet
      rc_status -v
      ;;
  restart)
      ## Stop the service and regardless of whether it was
      ## running or not, start it again.
      $0 stop
      $0 start

      # Remember status and be quiet
      rc_status
      ;;
  try-restart)
      $0 status
      if test $? = 0; then
         $0 restart
      else
         rc_reset        # Not running is not a failure.
      fi
      # Remember status and be quiet
      rc_status
      ;;
  reload|force-reload)
      ## Signal the daemon to restart
      echo -n "Reload $DESC "
      $SENDCOMMAND "DOWN 1" 2>&1|grep '^200 '>/dev/null

      # Remember status and be verbose
      rc_status -v
      ;;
  status)
      echo -n "Checking for $DESC "
      /sbin/checkproc -p $PIDFILE $DAEMON

      # Remember status and be verbose
      rc_status -v
        ;;
  setup)
      $0 status
      if test $? -gt 0; then
         $0 start 
      fi	   
      $SETUP
      
      # Remember status and be quiet
      rc_status
      ;;
  *)
      echo "Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload|setup}"
      exit 1
      ;;
esac
