#!/bin/bash
#
# chkconfig: 345 99 01
#
### BEGIN INIT INFO
# Provides: icinga_command_proxy
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Should-Start: icinga
# Should-Stop: icinga
### END INIT INFO

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

PROG="icinga_command_proxy"
BIN="/usr/local/bin/icinga_command_proxy"

if [[ -f /etc/sysconfig/$PROG ]]; then
    . /etc/sysconfig/$PROG
fi

ICINGA_CMD=${ICINGA_CMD:-"/usr/local/icinga/var/rw/icinga.cmd"}
ICINGA_MYSQL_CMD=${ICINGA_MYSQL_CMD:-"/usr/local/icinga-mysql/var/rw/icinga.cmd"}
ICINGA_PGSQL_CMD=${ICINGA_PGSQL_CMD:-"/usr/local/icinga-pgsql/var/rw/icinga.cmd"}

LOCKFILE=${LOCKFILE:-/var/lock/subsys/$PROG}
PIDFILE=${PIDFILE:-/var/lock/subsys/$PROG/$PROD.pid}

RETVAL=0

start() {
    echo -n $"Starting $PROG: "
    daemon --pidfile="$PIDFILE" "nohup \"$BIN\" \"$ICINGA_CMD\" \"$ICINGA_MYSQL_CMD\" \"$ICINGA_PGSQL_CMD\" >/dev/null 2>&1 &"
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && touch "$LOCKFILE"
    return $RETVAL
}

stop() {
    echo -n $"Stopping $PROG: "
    killproc -p "$PIDFILE" "$BIN"
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f "$LOCKFILE" "$PIDFILE"
}

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

exit $RETVAL

