#!/bin/sh
#
# pbsserv: This script will start and stop the PBS server
#
# description: PBS is a batch versitle batch system for SMPs and clusters
#
### BEGIN INIT INFO
# Provides: pbsserv
# Required-Start: $network  $remote_fs
# Required-Stop: $network
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: Start PBS server
### END INIT INFO         

# Source the library functions
. /etc/rc.status

# First reset status of this service
rc_reset 
case "$1" in
  start) 
	# Check if database was created
	if [ ! -f "/var/spool/pbs/server_priv/serverdb" ]; then
		return=$rc_done
		echo -n "Creating PBS database"
		/opt/pbs/sbin/pbs_server -t create 
		rc_status -v 
		sleep 2
		killproc -TERM /opt/pbs/sbin/pbs_server 
	fi
	echo -n "Starting PBS Server"
	startproc /opt/pbs/sbin/pbs_server  -a 1 >/dev/null 2>&1
	rc_status -v 
  ;;
  stop)
	echo -n "Stopping PBS Server"
	#killproc -TERM /opt/pbs/sbin/pbs_server 
	checkproc /opt/pbs/sbin/pbs_server ||  echo -n " Warning: server not running "
	( checkproc /opt/pbs/sbin/pbs_server &&  /opt/pbs/bin/qterm -t quick) || killproc /opt/pbs/sbin/pbs_server -TERM 
	rc_status -v 
  ;;
  status)
        echo -n "Checking for PBS server: "
	checkproc /opt/pbs/sbin/pbs_server
	rc_status -v 
  ;;
  try-restart)
        ## Stop the service and if this succeeds (i.e. the
        ## service was running before), start it again.
        $0 status >/dev/null &&  $0 restart 
 
        # Remember status and be quiet
        rc_status
        ;;                 
  restart)
	echo "Restarting PBS server"
	$0 stop
	$0 start
	rc_status
  ;;
  *)
        echo "Usage: $0 {start|stop|status|restart|reload}"
        exit 1
esac

rc_exit
