#!/bin/sh
#
# pbs_mom       This script will start and stop the PBS Mom
#
# chkconfig: 345 98 10
# description: TORQUE/PBS is a versatile batch system for SMPs and clusters

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

prog="pbs_mom"

# PBS_HOME, ULIMIT, SYSCTL
[ -f /etc/sysconfig/pbs ] && . /etc/sysconfig/pbs

test X"$PBS_HOME" = "X" && PBS_HOME=/var/spool/pbs
PBS_DAEMON=/usr/sbin/pbs_mom
export PBS_DAEMON PBS_HOME
[ -f $PBS_DAEMON ] || exit 0

RETVAL=0

set_limits() {
	test X"$SYSCTLS" != "X" && sysctl -w $SYSCTLS > /dev/null
	test X"$ULIMITS" != "X" && ulimit $ULIMITS
}

start() {
	echo -n $"Starting $prog: "
# Check for Dubna-Grid environment
	if test -e /var/lock/subsys/wulfd ; then
	  if  test ! -e /var/lock/subsys/dgr-final ; then
	    failure $"DGR final is not done yet"
	    echo
	    exit 1
	  fi
	fi
# Check for mounted AFS and /scr
	cat /proc/mounts | grep -q "^AFS /afs afs"
	if test $? -ne 0 ; then
	    failure $"AFS is not mounted"
	    echo
	    exit 1
	fi
	cat /proc/mounts | grep -qE " /scr .*(,| )rw(,| )"
	if test $? -ne 0 ; then
	    failure $"/scr  is not mounted"
	    echo
	    exit 1
	fi
	set_limits
# force to clean cvmfs cache
	test -e /etc/init.d/cvmfs && \
		/etc/init.d/cvmfs restartclean
	daemon $PBS_DAEMON $PBS_MOM_FLAGS
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/pbs_mom
	return $RETVAL
}

stop() {
	echo -n $"Stopping $prog: "
	killproc pbs_mom
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/pbs_mom
	return $RETVAL
}

restart() {
	stop
	start
}

reconfig() {
	echo -n $"Reloading configuration: "
	killproc pbs_mom -HUP
	RETVAL=$?
	echo
	return $RETVAL
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  purge)
	[ -f /var/lock/subsys/pbs_mom ] && stop
	PBS_MOM_FLAGS="-r" start
	;;
  stop)
	stop
	;;
  status)
	status pbs_mom
	;;
  restart)
	restart
	;;
  condrestart)
	test -f /var/lock/subsys/pbs_mom && restart || exit 0
	;;
  reconfig|reload)
	reconfig
	;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|purge|condrestart|reconfig}"
	exit 1
esac

exit $?
