#! /bin/sh
#
# skeleton	Written by Miquel van Smoorenburg <miquels@cistron.nl>.
#		Modified for Debian 
#		by Ian Murdock <imurdock@gnu.ai.mit.edu>.
#
# Version:	@(#)skeleton  1.9  26-Feb-2001  miquels@cistron.nl
#
### BEGIN INIT INFO
# Provides:          root-system-xrootd
# Required-Start:    $local_fs
# Required-Stop:     $local_fs 
# Should-Start:	     $network, $remote_fs, $named
# Should-Stop:	     $network, $remote_fs, $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Extended ROOT file server
# Description:       Extended Server of ROOT files via special ROOT 
#	             protocol, and Open Load Balancer server.
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
XDAEMON=@prefix@/bin/xrootd
ODAEMON=@prefix@/bin/olbd
CDAEMON=@prefix@/bin/cmsd
XNAME=xrootd
ONAME=olbd
CNAME=cmsd
DESC="Extended ROOT file server"
XDESC="Extended ROOT file server" 
ODESC="Open Load Balancer server"
CDESC="Cluster Management System server"
USER=root
test -x $DAEMON || exit 0

# Include root defaults if available
if [ -f @sysconfdir@/default/root-system-xrootd ] ; then
	. @sysconfdir@/default/root-system-xrootd
fi

# Load the VERBOSE setting and other rcS variables
[ -f /etc/default/rcS ] && . /etc/default/rcS

# Define LSB log_* functions.
. /lib/lsb/init-functions

# Check enables 
enable_xrootd=`echo $ENABLE_XROOTD | tr '[:upper:]' '[:lower:]'`
enable_olbd=`echo $ENABLE_OLBD| tr '[:upper:]' '[:lower:]'`
enable_cmsd=`echo $ENABLE_CMSD| tr '[:upper:]' '[:lower:]'`

# xrood options 
# Configuration file
if test "x$XROOTD_CONFIG_FILE" != "x" ; then 
    XDAEMON_OPTS="$XDAEMON_OPTS -c $XROOTD_CONFIG_FILE" 
fi
# Log file 
if test "x$XROOTD_LOG_FILE" != "x" ; then 
    XDAEMON_OPTS="$XDAEMON_OPTS -l $XROOTD_LOG_FILE" 
fi
# port to listen to
if test "x$XROOTD_PORT" != "x" ; then 
    XDAEMON_OPTS="$XDAEMON_OPTS -p $XROOTD_PORT" 
fi
# Redirection 
if test "x$XROOTD_REDIRECTOR" != "xyes" ; then 
    XDAEMON_OPTS="$XDAEMON_OPTS -r" 
fi
# Base directory
if test "x$XROOTD_BASEDIR" != "xyes" ; then 
    XDAEMON_OPTS="$XDAEMON_OPTS $XROOTD_BASEDIR" 
fi

# olbd options 
# Confguration file 
if test "x$OLBD_CONFIG_FILE" != "x" ; then 
    ODAEMON_OPTS="$ODAEMON_OPTS -c $OLBD_CONFIG_FILE" 
fi
# Log file 
if test "x$OLBD_LOG_FILE" != "x" ; then 
    ODAEMON_OPTS="$ODAEMON_OPTS -l $OLBD_LOG_FILE" 
fi
# Port to listen to
if test "x$OLBD_PORT" != "x" ; then 
    ODAEMON_OPTS="$ODAEMON_OPTS -p $OLBD_PORT" 
fi
# Redirection. 
if test "x$OLBD_REDIRECTOR" != "xyes" ; then 
    ODAEMON_OPTS="$ODAEMON_OPTS -r" 
fi

# cmsd options 
# Confguration file 
if test "x$CMSD_CONFIG_FILE" != "x" ; then 
    CDAEMON_OPTS="$CDAEMON_OPTS -c $CMSD_CONFIG_FILE" 
fi
# Log file 
if test "x$CMSD_LOG_FILE" != "x" ; then 
    CDAEMON_OPTS="$CDAEMON_OPTS -l $CMSD_LOG_FILE" 
fi
# Port to listen to
if test "x$CMSD_PORT" != "x" ; then 
    CDAEMON_OPTS="$CDAEMON_OPTS -p $CMSD_PORT" 
fi

# User 
if test "x$XROOTD_USER" = "xroot" ; then 
    :
elif test "x$XROOTD_USER" != "x" ; then 
    USER=$XROOTD_USER
fi

# Check if we're enabled
case $enable_rootd:$enable_olbd:$enable_cmsd in 
    yes:*:*) ;;
    *:yes:*) ;;
    *:*:yes) ;;
    *) exit 0;;
esac

# function to start the main server 
startitx ()
{
    [ "$VERBOSE" != no ] && log_progress_msg "$XNAME" 
    start-stop-daemon --make-pidfile --chuid $USER --start --quiet \
	--pidfile /var/run/$XNAME.pid --exec $XDAEMON \
	--background -- $XDAEMON_OPTS
}
startito ()
{
    [ "$VERBOSE" != no ] && log_progress_msg "$ONAME" 
    start-stop-daemon --make-pidfile --chuid $USER --start --quiet \
	--pidfile /var/run/$ONAME.pid --exec $ODAEMON \
	--background -- $ODAEMON_OPTS
}
startitc ()
{
    [ "$VERBOSE" != no ] && log_progress_msg "$CNAME" 
    start-stop-daemon --make-pidfile --chuid $USER --start --quiet \
	--pidfile /var/run/$CNAME.pid --exec $CDAEMON \
	--background -- $CDAEMON_OPTS
}

stopitx() 
{
    start-stop-daemon --user $USER --stop --oknodo --quiet --pidfile \
	/var/run/$XNAME.pid --exec $XDAEMON 
}
stopito()
{
    start-stop-daemon --user $USER --stop --oknodo --quiet --pidfile \
	/var/run/$ONAME.pid --exec $ODAEMON 
}
stopitc()
{
    start-stop-daemon --user $USER --stop --oknodo --quiet --pidfile \
	/var/run/$CNAME.pid --exec $CDAEMON 
}

# executable code 
case "$1" in
  start)
	ret=0
	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" 
	if test "x$enable_xrootd" = "xyes" ; then 
	    startitx
	    ret=$? 
	fi
	if test $ret -eq 0 && test "x$enable_olbd" = "xyes" ; then 
	    startito
	    ret=$? 
	fi
	[ "$VERBOSE" != no ] && log_end_msg $ret
	[ $ret -ne 0 ] && exit $ret
	;;
  stop)
	ret=0
	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" 
	if test "x$enable_xrootd" = "xyes" ; then 
	    [ "$VERBOSE" != no ] && log_progress_msg "$XNAME" 
	    stopitx
	    ret=$?
	fi
	if test "x$enable_olbd" = "xyes" ; then 
	    [ "$VERBOSE" != no ] && log_progress_msg "$ONAME" 
	    stopito
	    ret=$?
	fi
	[ "$VERBOSE" != no ] && log_end_msg $ret
	[ $ret -ne 0 ] && exit $ret
	;;
  restart|force-reload)
	#
	#	If the "reload" option is implemented, move the "force-reload"
	#	option to the "reload" entry above. If not, "force-reload" is
	#	just the same as "restart".
	#
	ret=0
	[ "$VERBOSE" != no ] && log_daemon_msg "Restarting $DESC" 
	if test "x$enable_xrootd" = "xyes" ; then 
	    stopitx
	    startitx
	    ret=$?
	fi
	if test $ret -eq 0 && test "x$enable_olbd" = "xyes" ; then 
	    stopito
	    startito
	    ret=$?
	fi
	[ "$VERBOSE" != no ] && log_end_msg $ret
	[ $ret -ne 0 ] && exit $ret
	;;
  *)
	N=@sysconfdir@/init.d/root-system-$XNAME
	# echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
#
# EOF
#
