#!/bin/sh
#
#       /etc/rc.d/init.d/tractor-blade
#
# Controls tractor-blade, the Pixar new remote execution server
#
# chkconfig: 345 95 05
# description: Handles remote execution and status monitoring services \
# for Pixar's Tractor and netrender distributed computing \
# applications.
# processname: tractor-blade

prog="tractor-blade"
options=/etc/sysconfig/pixar
pidfile=/var/run/$prog.pid
lockfile=/var/lock/subsys/$prog

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking enabled.
if [ "$NETWORKING" = "no" ]; then
    exit 0
fi

# Source function library.
. /etc/init.d/functions

# Source the options file, defines bladeOPTIONS
# also define bladeDIR, RATTREE, RMANTREE
if [ -f $options ];then
        . $options
fi

executable=$bladeDIR/$prog

# Sanity check, executable exists?
test -x $executable || exit 0

RETVAL=0

#
# Set up some functions
#
start() {
    local pid
    echo -n $"Starting $prog: "
    RETVAL=0
    # Check if the daemon is already running
    pid=`pidofproc $prog`
    if [ -f $pidfile ]; then
        RETVAL=1
        if [ "$pid" == "" ]; then
            RETVAL=0
            rm -f $pidfile
        fi
    fi
    # in case the blade is running as non-priviledged user
    # touch the pidfile and change ownership so blade can
    # write it.
    touch $pidfile
    chown $bladeOWNER $pidfile

    if [ $RETVAL -eq 0 ]; then
        daemon --user=$bladeOWNER "$bladeLAUNCH $@ --daemon --pidfile=$pidfile"
        RETVAL=$?
        if [ $RETVAL -ne 0 ]; then
            [ "$BOOTUP" != "verbose" ] && echo_failure
        else
            touch $lockfile
        fi
    fi
    echo ""
    return $RETVAL
}

reload() {
    local pid
    echo -n $"Reloading $prog: "
    RETVAL=0
    # run program without killing previous, add --supersede
    daemon --user=$bladeOWNER "$bladeLAUNCH $@ --daemon --pidfile=$pidfile --supersede"
    RETVAL=$?
    return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog
    RETVAL=$?
    [ $RETVAL -eq 0 ] && rm -f $pidfile $lockfile
    echo ""
    return $RETVAL
}



#
# Handle invocation options
#

case "$1" in
start)
        start ${@:2}
        ;;
stop)
        stop
        ;;
reload)
        reload ${@:2}
        ;;
restart)
        stop
        start ${@:2}
        ;;
condrestart)
        if [ -f $pidfile ]; then
            stop
        fi
        start ${@:2}
        ;;
status)
        status $executable
	exit $?
        ;;
*)
        echo $"Usage: $0 {start|stop|reload|restart|condrestart|status}"
        exit 1
esac

exit $RETVAL
