#!/bin/sh

# PROVIDE: condor_master
# REQUIRE: DAEMON
# BEFORE: LOGIN
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# condor_enable (bool):   Set to NO by default.
#               Set it to YES to enable condor.
#

. /etc/rc.subr

name="condor"
rcvar=condor_enable

pidfile=/var/run/${name}.pid

load_rc_config $name

: ${condor_enable="NO"}

start_cmd=condor_start
stop_cmd=condor_stop

condor_start() {
    checkyesno condor_enable && echo "Starting condor_master." && \
	/usr/local/sbin/condor_master ${condor_flags}
    while ! ps ax | egrep -v 'grep|rc\.d' | grep -q condor; do
        echo 'Waiting for daemons to start...'
        sleep 2
    done
}

condor_stop() {
    # FIXME: condor_off with no arguments should be the same as
    # -daemon master according to the man page, but it doesn't work
    checkyesno condor_enable && echo "Stopping condor_master." && \
	/usr/local/sbin/condor_off -daemon master
    # Prevent restart command from issuing start before daemons are down
    tries=1
    while ps ax | egrep -v 'grep|rc\.d' | grep -q condor; do
        echo 'Waiting for daemons to shut down...'
        sleep 2
        tries=$(($tries + 1))
        if [ $tries = 10 ]; then
            printf "condor_off has failed.  Force killing condor_master...\n"
            killall condor_master
            sleep 2
        fi
    done
}

run_rc_command "$1"
