#!/bin/bash
#
# autotestd       Start up the autotest scheduler daemon
#
# Copyright 2009 Red Hat, Inc.
#
# This software may be freely redistributed under the terms of the GNU
# general public license.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# chkconfig: - 65 25
# description: Autotest is a framework for fully automated testing.
# processname: monitor_db.py
# pidfile: /var/run/autotest/autotest-monitor-watcher.pid
#
### BEGIN INIT INFO
# Provides: autotest
# Required-Start: $syslog $local_fs
# Required-Stop: $syslog $local_fs
# Default-Stop: 0 1 6
# Short-Description: Start the autotest scheduler daemon
# Description: Autotest is a framework for fully automated testing.
### END INIT INFO

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

# pull in sysconfig settings
[ -f /etc/sysconfig/autotest ] && . /etc/sysconfig/autotest

PROG="autotest"
BECOME_USER=$PROG
LOCKFILE=/var/lock/subsys/$PROG

# Autotest paths
AUTOTEST_DIR="/usr/local/$PROG"
WATCHER="$AUTOTEST_DIR/scheduler/autotest-scheduler-watcher"
SCHEDULER="$AUTOTEST_DIR/scheduler/autotest-scheduler"

# Scheduler options
OPTIONS="--background"

# Where to locate PID files
PID_PATH="$AUTOTEST_DIR"
WATCHER_PIDFILE="$PID_PATH/autotest-scheduler-watcher.pid"
SCHEDULER_PIDFILE="$PID_PATH/autotest-scheduler.pid"

# Assume pass
RETVAL=0

start()
{
    [ -f $WATCHER ] || exit 5

    echo -n $"Starting $PROG: "
    daemon --user $BECOME_USER --check $PROG $WATCHER $OPTIONS
    RETVAL=$?
    echo
    [ "$RETVAL" = 0 ] && touch $LOCKFILE
    return $RETVAL
}

stop()
{
    echo -n $"Stopping $PROG: "

    killproc $WATCHER

    RETVAL=$?
    echo
    if [ "$RETVAL" = 0 ]; then
         rm -f $LOCKFILE
         rm -f $WATCHER_PIDFILE
         rm -f $SCHEDULER_PIDFILE
    fi
    return $RETVAL
}

reload()
{
    echo -n $"Reloading $PROG: "
    killproc -p $WATCHER_PIDFILE $PROG -HUP
    RETVAL=$?
    echo
    return $RETVAL
}

restart() {
    stop
    start
}

force_reload() {
    restart
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    reload)
        reload
        ;;
    condrestart|try-restart)
        if [ -f $LOCKFILE ] ; then
            if [ "$RETVAL" = 0 ] ; then
                stop
                # avoid race
                sleep 3
                start
            else
                RETVAL=6
            fi
        fi
        ;;
    status)
        # status -p $PIDFILE $PROG
        status $WATCHER
        status $SCHEDULER
        RETVAL=$?
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|reload|condrestart|try-restart|status}"
        RETVAL=2
esac
exit $RETVAL
