#! /bin/bash
#
# 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-rootd
# Required-Start:    $remote_fs
# Required-Stop:     $remote_fs
# Should-Start:	     $network
# Should-Stop:	     $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: ROOT file server
# Description:       Server of ROOT files via special ROOT protocol
### END INIT INFO
#
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/rootd
NAME=rootd
DESC="ROOT file server"
USER=rootd
test -x $DAEMON || exit 0

# Include root defaults if available
if [ -f /etc/default/root-system-rootd ] ; then
	. /etc/default/root-system-rootd
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 options from system settings
# User to execute rootd as 
test ! "x$ROOTD_USER" = "x" && USER=$ROOTD_USER
# Port to listen to
test ! "x$ROOTD_PORT" = "x" && DAEMON_OPTS="$DAEMON_OPTS -p $ROOTD_PORT"
# SSH port 
test ! "x$SSH_PORT" = "x"   && DAEMON_OPTS="$DAEMON_OPTS -s $SSH_PORT"
# TCP window size 
test ! "x$ROOTD_WINDOW_SIZE" = "x" && DAEMON_OPTS="$DAEMON_OPTS -b $ROOTD_WINDOW_SIZE"
# Debug level 
test ! "x$ROOTD_DEBUG" = "x" && DAEMON_OPTS="$DAEMON_OPTS -d $ROOTD_DEBUG"
# Access rules file
test ! "x$ROOTD_ACCESS_RULES" = "x" && DAEMON_OPTS="$DAEMON_OPTS -D $ROOTD_ACCESS_RULES"
# Run in foreground (shouldn't be done in init script)
test "x$ROOTD_FOREGROUND" = "xyes" && DAEMON_OPTS="$DAEMON_OPTS -f"
# No client authentication (security issue)
test "x$ROOTD_NO_AUTH" = "xyes" && DAEMON_OPTS="$DAEMON_OPTS -noauth"
# files can only be opened read-only
test "x$ROOTD_READ_ONLY" = "xyes" && DAEMON_OPTS="$DAEMON_OPTS -r"
# Temporary directory 
test ! "x$ROOTD_TMP" = "x" && DAEMON_OPTS="$DAEMON_OPTS -T $ROOTD_TMP"
# Require passwords. 
test "x$ROOTD_REQUIRE_PASSWD" = "xyes" && DAEMON_OPTS="$DAEMON_OPTS -w"


# Check if rootd is under inetds control
if test -f /var/run/inetd.pid ; then 
    # Inetd is running
    if grep -q ^$NAME /etc/inetd.conf > /dev/null 2>&1 ; then 
	# ROOTD is controlled by inetd. 
	ENABLE_ROOTD=no
    fi
fi
# Check if rootd is under xinetds control
if test -f /var/run/xinetd.pid ; then 
    # Inetd is running
    if grep -q ^$NAME /etc/inetd.conf > /dev/null 2>&1 ; then 
	# ROOTD is controlled by xinetd. 
	ENABLE_ROOTD=no
    fi
    # old name 
    for i in rootd root-rootd root-system-rootd ; do 
	if test ! -f /etc/xinetd.d/$i ; then continue ; fi 
	if test grep "disable[[:space:]]*= *[Nn][Oo]" /etc/xinetd.d/$i \
	    > /dev/null 2>&1  ; then
	    # ROOTD is controlled by xinetd. 
	    ENABLE_ROOTD=no
	    break
	fi
    done
fi

# Check if we're enabled at all 
case `echo $ENABLE_ROOTD | tr '[:upper:]' '[:lower:]'` in 
    yes) ;;
    *) exit 0;;
esac

	
# Function to start the daemon
startit() 
{
    o=`start-stop-daemon --chuid $USER --start --quiet --pidfile \
	/var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS`
    ret=$?
    test $ret -ne 0 && return 1
    eval $o
    # echo " (pid: $ROOTD_PID) "
    echo $ROOTD_PID > /var/run/$NAME.pid
    return $ret
}

# Function to stop the daemon
stopit() 
{
    start-stop-daemon --user $USER --stop --oknodo --quiet --pidfile \
	/var/run/$NAME.pid --exec $DAEMON
    ret=$?
    test $ret -ne 0 && return 1
    rm -f /var/run/$NAME.pid
    return $ret
}

# Main code 
case "$1" in
  start)
	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
	startit 
	ret=$?
	[ "$VERBOSE" != no ] && log_end_msg $ret
	;;
  stop)
	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
	stopit
	ret=$?
	[ "$VERBOSE" != no ] && log_end_msg $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".
	#
	log_daemon_msg "Restarting $DESC" "$NAME"
	stopit
	sleep 1
	startit
	ret=$?
	log_end_msg $ret
	;;
  *)
	N=/etc/init.d/root-system-$NAME
	# 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
#
