#!/sbin/runscript
# Copyright 1999-2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/sys-cluster/openpbs/files/pbs.rc,v 1.4 2004/07/15 00:56:06 agriffis Exp $

depend() {
	need net
}

checkconfig() {

	HOSTNAME=`hostname`

	if [ ! -e /var/spool/PBS/server_name ] ; then
		eerror "Missing config file /var/spool/PBS/server_name"
		return 1
	else
		PBS_SERVER_NAME=`cat /var/spool/PBS/server_name`
		if [ ${HOSTNAME} == ${PBS_SERVER_NAME} ] ; then
			PBS_START_SERVER=1;
		else
			PBS_START_SERVER=0;
		fi
	fi

	if [ ! -e /var/spool/PBS/mom_priv/config ] ; then
		eerror "Missing config file /var/spool/PBS/mom_priv/config"
		return 1
	else
		PBS_START_MOM=1;
	fi

	if [ "${PBS_START_SERVER}" -eq 1 ] ; then
		if [ ! -e /var/spool/PBS/server_priv/nodes ] ; then
			eerror "The startup script has detected this node is a server"
			eerror "from the file /var/spool/PBS/server_name,"
			eerror "but the config file /var/spool/PBS/server_priv/nodes is missing"
			return 1
		fi
	fi

}

start() {
	checkconfig || return 1

	if [ "${PBS_START_MOM}" -gt 0 ] ; then
		ebegin "Starting pbs_mom"
		start-stop-daemon --start --pidfile /var/spool/PBS/mom_priv/mom.lock \
			--startas /usr/sbin/pbs_mom
	fi

	if [ "${PBS_START_SERVER}" -gt 0 ] ; then
		ebegin "Starting pbs_server"
		start-stop-daemon --start --quiet --pidfile /var/spool/PBS/server_priv/server.lock \
			--startas /usr/sbin/pbs_server
		ebegin "Starting pbs_sched"
		start-stop-daemon --start --quiet --pidfile /var/run/pbs_sched.pid \
			--startas /usr/sbin/pbs_sched
	fi

	eend $?
}

stop() {
	if [ -e /var/spool/PBS/mom_priv/mom.lock ] ; then
		ebegin "Stopping pbs_mom"
		start-stop-daemon --stop --pidfile /var/spool/PBS/mom_priv/mom.lock
		rm -f /var/spool/PBS/mom_priv/mom.lock
	fi

	if [ -e /var/spool/PBS/server_priv/server.lock ] ; then
		ebegin "Stopping pbs_server"
		start-stop-daemon --stop --pidfile /var/spool/PBS/server_priv/server.lock
		rm -f /var/spool/PBS/server_priv/server.lock
	fi

	if [ -e /var/spool/PBS/sched_priv/sched.lock ] ; then
		ebegin "Stopping pbs_sched"
		start-stop-daemon --stop --pidfile /var/spool/PBS/sched_priv/sched.lock
		rm -f /var/spool/PBS/sched_priv/sched.lock
	fi

	eend $?
}
