#!/bin/sh
#
# pbs_server	This script will start and stop the PBS Server
#
# chkconfig: 345 85 85
# description: TORQUE/PBS is a versatile batch system for SMPs and clusters
#
# processname: pbs_server
#
### BEGIN INIT INFO
# Provides: pbs_server
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 3 4 5
# Short-Description: The Torque server
# Description: The Tera-scale Open-source Resource and QUEue manager
#              provides control over batch jobs and distributed
#              computing resources. It is an advanced open-source
#              product based on the original PBS project and
#              incorporates the best of both community and
#              professional development.
### END INIT INFO

# Source the library functions
. /etc/rc.d/init.d/functions

create() {
    gprintf "Creating initial TORQUE configuration: "
    if [ -r $PBS_SERVERDB ]; then
        gprintf "Configuration already exists.  Please remove %s to create a new one.\n" "$PBS_SERVERDB"
        exit 1
    fi

    $PBS_DAEMON -d $PBS_HOME -t create &
    sleep 5
    killproc pbs_server
    RET=$?
}

start() {
    status pbs_server >/dev/null 2>&1
    if [ $? -eq 0 ]; then
        gprintf "pbs_server is already running.\n"
        exit 0
    fi
    echo $PBS_SERVERDB
    if [ ! -r $PBS_SERVERDB ]; then
        create
    fi
    gprintf "Starting TORQUE Server: "
    daemon $PBS_DAEMON -d $PBS_HOME $PBS_ARGS
    RET=$?
    [ $RET -eq 0 ] && touch /var/lock/subsys/pbs_server
    echo
}

stop() {
    status pbs_server >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        gprintf "pbs_server is not running.\n"
        exit 0
    fi
    gprintf "Shutting down TORQUE Server: "
    killproc pbs_server
    RET=$?
    rm -f /var/lock/subsys/pbs_server
    echo
}

reload() {
    gprintf "Reloading pbs_server: "
    killproc pbs_server -HUP
    RET=$?
    echo
}

PBS_DAEMON=/usr/sbin/pbs_server
PBS_HOME=/var/spool/torque
PBS_ARGS=""
PBS_SERVERDB="$PBS_HOME/server_priv/serverdb"
export PBS_DAEMON PBS_HOME PBS_ARGS PBS_SERVERDB

if [ -f /etc/sysconfig/pbs_server ]; then
   . /etc/sysconfig/pbs_server
fi

# let see how we were called
case "$1" in
	start) 
		start
		;;
	stop)
		stop
		;;
	status)
		status pbs_server
		RET=$?
		;;
	restart)
		stop
		start
		;;
	reload)
		reload
		;;
	create)
		create
		;;
	*)
		gprintf "Usage: pbs_server {start|stop|restart|status|reload|create}\n"
		exit 1
esac
exit $RET
