#!/bin/sh
#
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# based on the template by SuSE Linux and Wilfried Goesgens
#
# /etc/init.d/webcit
#
### BEGIN INIT INFO
# Provides:          webcit
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 5
# Default-Stop:      0 1 6
# Short-Description: starts webcit the Citadel webclient
# Description:       Reads the options from /etc/syslog/webcit and
#                    starts webcit the Citadel webclient.
### END INIT INFO

DESC="Citadel Groupware Webserver "
NAME=webcit
RUNDIR=/var/run/$NAME
PIDFILE=$RUNDIR/$NAME.pid
DAEMON=/usr/sbin/$NAME
DROP_TO_UID=`id -u nobody`

# 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/$NAME ] || { echo "$NAME sysconfig file is not installed";
	if [ "$1" = "stop" ]; then exit 0;
	else exit 1; fi; }
. /etc/sysconfig/$NAME

# Check if $RUNDIR exists
[ -d $RUNDIR ] || mkdir -p $RUNDIR

unset LANG
unset LANGUAGE
unset LC_ALL
unset LOCALE

# 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)
    if test -n "$WEBCIT_LISTEN_IP"; then
      LISTEN="-i$WEBCIT_LISTEN_IP"
    fi
    if test -n "$WEBCIT_GUEST_LANDING_PAGE"; then
       LANDING_PAGE="-g $WEBCIT_GUEST_LANDING_PAGE"
    fi
    if test -n "$WEBCIT_HTTPS_CIPHERS"; then
       CIPHERS="-S $WEBCIT_HTTPS_CIPHERS"
    else
       CIPHERS="-s"
    fi
    if test "$WEBCIT_HTTP_PORT" -ge "0"; then
      echo -n "Starting $DESC"
      /sbin/startproc -p $PIDFILE.$WEBCIT_HTTP_PORT $DAEMON \
	-d -u$DROP_TO_UID \
	-D$PIDFILE.$WEBCIT_HTTP_PORT \
	-p$WEBCIT_HTTP_PORT $LANDING_PAGE \
	$WEBCIT_CITADEL_IP $WEBCIT_CITADEL_PORT \
	$LISTEN $WEBCIT_APACHEFLAG \
	$WEBCIT_DAEMON_ARGS

      # Remember status and be verbose
      rc_status -v
    fi
    if test "$WEBCIT_HTTPS_PORT" -ge "0"; then
      echo -n "Starting secure $DESC"
      /sbin/startproc -p $PIDFLE.$WEBCIT_HTTPS_PORT $DAEMON \
	-d -u$DROP_TO_UID \
	-D$PIDFILE.$WEBCIT_HTTPS_PORT \
	-p$WEBCIT_HTTPS_PORT $LANDING_PAGE \
	$WEBCIT_CITADEL_IP $WEBCIT_CITADEL_PORT \
	$CIPHERS $LISTEN $WEBCIT_APACHEFLAG \
	$WEBCIT_DAEMON_ARGS

      # Remember status and be verbose
      rc_status -v
    fi
    ;;

  stop)
    if test "$WEBCIT_HTTP_PORT" -ge "0"; then
	echo -n "Shutting down $DESC"
	/sbin/killproc -p $PIDFILE.$WEBCIT_HTTP_PORT $DAEMON

	# Remember status and be verbose
        rc_status -v
    fi

    if test "$WEBCIT_HTTPS_PORT" -ge "0"; then
	echo -n "Shutting down secure $DESC"
	/sbin/killproc -p $PIDFILE.$WEBCIT_HTTPS_PORT $DAEMON

	# Remember status and be verbose
        rc_status -v
    fi
    ;;
  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
    ;;
  restart|reload|force-reload)
    ## 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
    ;;
  status)
    if test "$WEBCIT_HTTP_PORT" -ge "0"; then
      echo -n "Checking for $DESC "
      /sbin/checkproc -p $PIDFILE.$WEBCIT_HTTP_PORT $DAEMON

      # Remember status and be quiet
      rc_status -v
    fi
    if test "$WEBCIT_HTTPS_PORT" -ge "0"; then
      echo -n "Checking for secure $DESC "
      /sbin/checkproc -p $PIDFILE.$WEBCIT_HTTPS_PORT $DAEMON

      # Remember status and be quiet
      rc_status -v
    fi
    ;;
  *)
     echo "Usage: $0 {start|stop|status|restart|reload|force-reload|try-restart}"
     exit 1
     ;;
esac

:
