#!/bin/sh
#
# chkconfig: 345 55 45
# description: Starts the tinderbox build bot

### BEGIN INIT INFO
# Provides: tinderbox
# Required-Start: $network
# Default-Start: 345
# Short-Description: Starts the tinderbox build bot
# Description: This startup script launches the tinderbox build bot
### END INIT INFO

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

NAME=tinderbox
BINARY=/usr/share/tinderbox/bin/build_shellscript
LOCKFILE=/var/lock/subsys/$NAME
PIDFILE=/var/run/$NAME
USER=$NAME

# Source service configuration
[ -f /etc/sysconfig/tinderbox ] && . /etc/sysconfig/tinderbox

RETVAL=0

start() {
    if [ -z "$BUILD" ]; then
	gprintf "No build type defined, aborting\n"
	exit 1
    fi
    if [ -z "$TREE" ]; then
	gprintf "No source tree defined, aborting\n"
	exit 1
    fi
    # Check if it is already running
    if [ ! -f $LOCKFILE ]; then
	gprintf "Starting %s" "$NAME"
	touch $PIDFILE
	chown $USER $PIDFILE
	daemon --user $USER $BINARY --build $BUILD --tree $TREE --daemonize --pidfile $PIDFILE
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch $LOCKFILE
	echo
    fi
    return $RETVAL
}

stop() {
	gprintf "Stopping %s" "$NAME"
	killproc -p $PIDFILE $NAME
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f $LOCKFILE
	echo
        return $RETVAL
}

case "$1" in
start)
	start
	;;
stop)
	stop
	;;
restart|reload)
	stop
	start
	;;
condrestart)
	if [ -f $LOCKFILE ]; then
	    start
	    stop
	fi
	;;
status)
	status -p $PIDFILE $NAME
	;;
*)
	gprintf "Usage: %s {start|stop|restart|reload|condrestart|status}" "$0"
	exit 1
esac

exit $RETVAL
