#!/bin/sh
#
# beakerd:	Start the Beaker proxy daemon
#
# chkconfig: - 99 99
# description:  This service proxies unauthenticated requests from the lab
#               network to the Beaker Scheduler.  Communication to the 
#               scheduler is done via authenticated https.
#	
#

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

[ -f /usr/bin/beaker-proxy ] || exit 0

prog="beaker-proxy"
PIDFILE=/var/run/beaker-lab-controller/${prog}.pid
LOCKFILE=/var/lock/subsys/$prog
RUN_AS=apache

check_pidfile() {
    status -p $PIDFILE $prog >& /dev/null
    ret=$?
    if [ "$ret" -eq 1 ]; then
        # remove stale pidfile
        rm -f -- "$PIDFILE"
    fi
}

start() {
    echo -n $"Starting $prog: "
    check_pidfile
    daemon --pidfile $PIDFILE --user $RUN_AS $prog
    RETVAL=$?
    echo
    if test $RETVAL = 0; then
        touch $LOCKFILE
    fi
    return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "
    killproc -p $PIDFILE $prog
    RETVAL=$?
    echo 
    rm -f $LOCKFILE
    return $RETVAL
}

restart() {
    stop
    start
}

condrestart(){
	[ -e $LOCKFILE ] && restart
	return 0
}

# See how we were called.
case "$1" in
    start)
	start
	;;
    stop)
	stop
	;;
    status)
	status -p $PIDFILE $prog
	RETVAL=$?
        ;;
    restart)
	restart
	;;
    condrestart)
	condrestart
	;;
    reload)
        ;;
    *)
	echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
	;;
esac
exit $RETVAL
