#!/bin/sh
#
# shinken-broker - Startup script for the Shinken Broker Daemon
#
# chkconfig: 2345 20 80
# Description:  Shinken is a monitoring tool and the Broker \
#               is one of its daemon. This one gets the configuration \
#               from the arbiter. His purpose is to get the broks from \
#               the schedulers specified in the configuration.

### BEGIN INIT INFO
# Provides:          shinken-broker
# Required-Start:    $all 
# Required-Stop:     $all
# Should-Start:    
# Should-Stop: 
# Default-Start:     2 3 4 5 
# Default-Stop:      0 1 6
# Short-Description: Shinken broker daemon
# Description:       Shinken is a monitoring tool and the Broker
#                    is one of its daemon. This one gets the configuration 
#                    from the arbiter. His purpose is to get the broks from 
#                    the schedulers specified in the configuration
### END INIT INFO



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

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

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

exec="/usr/sbin/shinken-broker"
prog=$(basename $exec)

opts="-d -c /etc/shinken/daemons/brokerd.ini"             # Arguments to run the daemon with

pidfile=/var/run/shinken/brokerd.pid
piddir=/var/run/shinken

user=nagios
group=root

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

lockfile=/var/lock/subsys/$prog

start() {

    [ ! -d $piddir ] && mkdir -m 0750 -p $piddir
    chown $user:$group $piddir

    echo -n $"Starting $prog: "
    daemon --pidfile=$pidfile $exec $opts
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

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

restart() {
    stop
    sleep 1
    start
}


case "$1" in
    start|stop|restart)
        $1
        ;;
    force-reload)
        restart
        ;;
    status)
        status -p $pidfile $prog
        ;;
    try-restart|condrestart)
        if status $prog >/dev/null ; then
            restart
        fi
	;;
    reload)
        # If config can be reloaded without restarting, implement it here,
        # remove the "exit", and add "reload" to the usage message below.
        # For example:
        # status $prog >/dev/null || exit 7
        # killproc $prog -HUP
        action $"Service ${0##*/} does not support the reload action: " /bin/false
        exit 3
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|try-restart|force-reload}"
        exit 2
esac
