#!/bin/bash
#
# chkconfig: 2345 94 06
# description: The Vesta runtool server
# config: /etc/vesta/initconfig
# config: /etc/vesta.cfg
#
### BEGIN INIT INFO
# Provides: vruntool
# Required-Start: $network vmount
# Should-Start: autofs ypbind
# X-UnitedLinux-Should-Start: autofs ypbind
# Required-Stop: $network vmount
# Should-Stop: autofs ypbind
# X-UnitedLinux-Should-Stop: autofs ypbind
# Default-Start:2 3 4 5
# Default-Stop: 0 1 6
# Description: The Vesta runtool server
### END INIT INFO

# Notes on ordering:
#
# - Start ypbind first in case the user to run the RunToolServer
# daemon comes from YP
#
# - Start autofs first in case the configuration files are in an
# auto-mounted alias for another local filesystem
#
# Note also that we use both "Should-Start" / "Should-Stop" (which LSB
# defines) and "X-UnitedLinux-Should-Start" /
# "X-UnitedLinux-Should-Stop" (which some vendors defined before LSB
# added "Should-Start"/"Should-Stop") to be compatible with more
# systems

if [ -f /etc/vesta/initconfig ]; then
    . /etc/vesta/initconfig
else
    echo $"Vesta services are not configured"
    exit 0;
fi

RETVAL=0

# On RedHat we put a file in /var/lock/subsys to indicate that the
# runtool server has been started.
if [ -d /var/lock/subsys ]; then
  lockfile=/var/lock/subsys/vruntool
fi

pidfile="/var/run/${RUNTOOL_NAME}.pid"
if [ ! -d /var/run ]; then
    mkdir -p /var/run
fi

if [ -f /etc/init.d/functions ]; then
    # Source RedHat function library.
    . /etc/init.d/functions
    start() {
	echo -n $"Starting the Vesta runtool server: "

	daemon --check ${RUNTOOL_NAME} \
	    "${VSRUN_BIN} --background \
	    --user ${RUNTOOL_USER} --pidfile $pidfile \
	    ${RUNTOOL_VSRUN_ARGS} \
	    -- ${RUNTOOL_BIN}"

	RETVAL=$?
	echo
	[ $RETVAL -eq 0 -a -n "$lockfile" ] && touch $lockfile
    }

    stop() {
	echo -n $"Shutting down the Vesta runtool server: "

	killproc ${RUNTOOL_NAME}

	RETVAL=$?
	echo
	[ $RETVAL -eq 0 -a -n "$lockfile" ] && rm -f $lockfile
    }

    status () {
	pid=`pidof -o $$ -o $PPID -o %PPID -x $1`
        if [ -n "$pid" ]; then
                echo $"$1 (pid $pid) is running..."
                return 0
        fi

        echo $"$1 is stopped"
        return 3
    }
elif [ -f /lib/lsb/init-functions ]; then
    # SuSE/LSB compliant version
    . /lib/lsb/init-functions

    server_pid() {
	if [ ! -e $pidfile ]; then
	    echo 0
	else
	    pid=`cat $pidfile`
	    if kill -0 $pid && ps -p $pid | grep -q ${RUNTOOL_NAME}; then
		echo $pid
	    else
		echo 0
	    fi
	fi
    }
    
    start() {
	pid=`server_pid`
 	if [ $pid -gt 0 ]; then
	    log_success_msg $"The Vesta repository is already running"
	else
	    echo -n $"Starting the Vesta runtool server: "

	    start_daemon -f ${VSRUN_BIN} --background \
		--user ${RUNTOOL_USER} --pidfile $pidfile \
		${RUNTOOL_VSRUN_ARGS} \
		-- ${RUNTOOL_BIN}

	    log_success_msg "${RUNTOOL_NAME}."
	fi
    }
    stop() {
        echo -n $"Shutting down the Vesta runtool server: "

        killproc -p $pidfile ${RUNTOOL_NAME}

	log_success_msg "${RUNTOOL_NAME}."
    }
    status () {
	pid=`server_pid`
	if [ $pid -gt 0 ]; then 
	    echo $"$1 (pid $pid) is running..."
            return 0
        fi

        echo $"$1 is stopped"
        return 3
    }
elif [ -x /sbin/start-stop-daemon ]; then
    # Debian versions of functions

    start() {
	echo -n $"Starting the Vesta runtool server: "

	start-stop-daemon --start \
	    --user ${RUNTOOL_USER} --name ${RUNTOOL_NAME} \
	    --background --startas ${VSRUN_BIN} -- \
	    --user ${RUNTOOL_USER} \
	    --pidfile $pidfile \
	    ${RUNTOOL_VSRUN_ARGS} \
	    -- ${RUNTOOL_BIN}

	RETVAL=$?
        echo "${RUNTOOL_NAME}."
    }

    stop() {
	echo -n $"Shutting down the Vesta runtool server: "

        start-stop-daemon --stop --quiet --oknodo \
	    --user ${RUNTOOL_USER} --name ${RUNTOOL_NAME} \

	RETVAL=$?
        echo "${RUNTOOL_NAME}."
    }

    status () {
	pid=`pidof -o $$ -o $PPID -o %PPID -x $1`
        if [ -n "$pid" ]; then
                echo $"$1 (pid $pid) is running..."
                return 0
        fi

        echo $"$1 is stopped"
        return 3
    }
else
  echo "Don't know how to start/stop daemons!"
  exit 1
fi

require_root()
{
    # Make sure we're running as root 
    if [ `id -u` -ne 0 ]; then 
	echo "$0 $1: Must be run as root"
        exit 1
    fi
}

case "$1" in
  start)
	require_root $1
	start
	;;
  stop)
	require_root $1
	stop
	;;
  restart|reload|force-reload)
	require_root $1
	stop
	start
	RETVAL=$?
	;;
  try-restart|condrestart)
	require_root $1
	# Note: this does nothing on Debian
	if [ -n "$lockfile" -a -f "$lockfile" ]; then
	    stop
	    start
	    RETVAL=$?
	fi
	;;
  status)
	status ${RUNTOOL_NAME}
	RETVAL=$?
	;;
  *)
	echo $"Usage: $0 {start|stop|restart|reload|force-reload|try-restart|condrestart|status}"
	exit 1
esac

exit $RETVAL

