#!/bin/bash
#
# ovirt-ha-broker        Start the oVirt Hosted Engine HA Communications Broker
#
# chkconfig: 2345 96 4
# description: ovirt-ha-broker - communications broker for the oVirt \
#              Hosted Engine High Availability project.
#
# processname: ovirt-ha-broker

### BEGIN INIT INFO
# Provides: ovirt-ha-broker
# Required-Start: $local_fs $syslog
# Required-Stop: $local_fs $syslog
# Should-Start: $syslog
# Should-Stop: $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start the oVirt Hosted Engine High Availability Communications Broker
# Description: ovirt-ha-broker - communications broker for the oVirt
#              Hosted Engine High Availability project.
### END INIT INFO

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

RETVAL=0
prog="ovirt-ha-broker"
lockfile=/var/lock/subsys/$prog

BROKER=/usr/share/ovirt-hosted-engine-ha/ovirt-ha-broker
ENGINE_SETUP_CONF_FILE=/etc/ovirt-hosted-engine/hosted-engine.conf

[ -f /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

runlevel=$(set -- $(runlevel); eval "echo \$$#" )

start()
{
    [ -x $BROKER ] || exit 5
    echo -n $"Starting $prog: "
    $BROKER $OPTIONS && success || failure
    RETVAL=$?
    [ $RETVAL -eq 0 ] && touch $lockfile
    echo
    return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $BROKER
    RETVAL=$?
    [ $RETVAL -eq 0 ] && rm -f $lockfile
    echo
}

restart() {
    stop
    start
}

reload() {
    restart
}

force_reload() {
    restart
}

rh_status() {
    status $prog
    RETVAL=$?
    return $RETVAL
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        start
        ;;
    stop)
        if ! rh_status_q; then
            rm -f $lockfile
            exit 0
        fi
        stop
        ;;
    status)
        rh_status
        ;;
    restart)
        restart
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    reload)
        rh_status_q || exit 7
        reload
        ;;
    force-reload)
        force_reload
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        RETVAL=2
esac
exit $RETVAL
