#!/bin/sh
#
# webcit Citadel Groupware Webclient
#
# chkconfig:	2345 20 80
# description:	control webcit 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# /etc/init.d/webcit

### BEGIN INIT INFO
# Provides:          webcit
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: control webcit start at boot time
# Description:       Reads the options from /etc/syslog/webcit and
#                    starts webcit the Citadel webclient.
### 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 Webserver"
NAME=webcit
RUNDIR=/var/run/$NAME
DAEMON=/usr/sbin/$NAME
PIDFILE=$RUNDIR/$NAME.pid
LOCKFILE=/var/lock/subsys/$NAME
DROP_TO_UID=`id -u nobody`

# 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/$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

start() {
   [ -x $DAEMON ] || exit 5
   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"
     daemon $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
     retval=$?
     [ $retval -eq 0 ] && touch "$PIDFILE.$WEBCIT_HTTP_PORT"
     echo
   fi
   if test "$WEBCIT_HTTPS_PORT" -ge "0"; then
     echo -n "Starting secure $DESC"
     daemon $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
     retval=$?
     [ $retval -eq 0 ] && touch $PIDFILE.$WEBCIT_HTTPS_PORT
     echo
   fi

   [ $retval -eq 0 ] && touch $LOCKFILE
   return $?
}

stop() {
  echo -n "Shutting down $DESC"
  killproc $DAEMON
  retval=$?  
  [ $retval -eq 0 ] && rm -f $LOCKFILE && rm -f $PIDFILE*
  echo
  return $retval
}

restart() {
    stop
    start
}

reload() {
    restart
}

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
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
exit $?

