#!/bin/sh
##
## Copyright 2009-2013 Centreon
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
##     http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
## For more information : contact@centreon.com
##

#
# Script init
#
### BEGIN INIT INFO Suse
# Provides:       cbd
# Required-Start:
# Required-Stop:
# Default-Start:  3 5
# Default-Stop: 0 1 6
# Description: Centreon Broker
### END INIT INFO

### BEGIN INIT INFO Redhat
# chkconfig: - 71 31
# description: Centreon Broker
# processname: cbd
# config: TO_CHANGE
# pidfile: TO_CHANGE
### END INIT INFO

# Configuration parameters.
watchdog_file="@PREFIX_CONF@/master.run"
config_dir="@PREFIX_CONF@"
cbwd="@PREFIX_BIN@/cbwd"
pid_path="@PREFIX_VAR@/run"
stop_timeout=10
user="@USER@"
debug=0
pidfile="${pid_path}/cbwd.pid"

# Check that cbwd is executable.
if [ ! -x "${cbwd}" ] ; then
  echo "The cbwd binary can't be run."
  exit 1
fi

# Check that configuration file exists.
if [ ! -e "${watchdog_file}" ] ; then
  echo "The watchdog configuration file isn't found."
  exit 1
fi

# Start cbd.
start_watchdog() {
  global_retval=0
  if [ -f "${pidfile}" ] ; then
    echo -n "Centreon Broker Watchdog is already running."
    echo "cbd startup [failed]"
    return 1
  fi
  # Start centreon broker watchdog.
  echo -n "Starting Centreon Broker watchdog: "
  if [ $debug -eq 1 ]; then
    echo "/tmp/%e.core.%t.%u.%p" > /proc/sys/kernel/core_pattern
    PID=`su - "${user}" -c "ulimit -c unlimited; ${cbwd} ${watchdog_file} >/dev/null 2>&1 & echo \\$!"`
    RETVAL=$?
  else
    PID=`su - "${user}" -c "${cbwd} ${watchdog_file} >/dev/null 2>&1 & echo \\$!"`
    RETVAL=$?
  fi

  if [ ${RETVAL} = 0 ]; then
    echo ${PID} > "${pidfile}"
    echo "cbd startup [success]"
  else
    ${global_retval} = ${RETVAL}
    echo "cbd startup [failed]"
  fi
  echo
  return ${global_retval}
}

# Stop cbd.
stop_watchdog() {
  if [ -f "${pidfile}" ] ; then
    echo -n "Stopping Centreon Broker watchdog: "
    kill -TERM `cat "${pidfile}"` > /dev/null 2>&1
    RETVAL=$?
    if [ ${RETVAL} = 0 ] ; then
      rm -f "${pidfile}"
      echo "cbd shutdown [success]"
    else
      echo "cbd shutdown [failure]"
    fi
    echo
  else
    echo "Centreon Broker watchdog is not running."
    echo "cbd shutdown [failure]"
  fi
}

# Send kill HUP
reload_watchdog() {
  if [ -f "${pidfile}" ] ; then
    echo -n "Reloading Centreon Broker watchdog: "
    kill -HUP `cat "${pidfile}"`
    RETVAL=$?
    if [ ${RETVAL} = 0 ]; then
      echo "cbd reload [success]"
    else
      echo "cbd reload [failure]"
    fi
  else
    echo "Centreon Broker watchdog is not running."
    echo "cbd reload [failure]"
  fi
}

# Status
status_watchdog() {
  if [ -f "${pidfile}" ] ; then
    echo "Centreon Broker watchdog is running."
  else
    echo "Centreon Broker watchdog NOT running."
  fi
}

# Switch case.
case "$1" in
  start)
    start_watchdog
    RETVAL=$?
    ;;
  stop)
    stop_watchdog
    RETVAL=$?
    ;;
  restart)
    stop_watchdog
    start_watchdog
    RETVAL=$?
    ;;
  reload)
    reload_watchdog
    ;;
  status)
    status_watchdog
    RETVAL=$?
    ;;
  *)
    echo "Usage: ${daemon_name} {start|stop|restart|status}"
    exit 1
esac

exit ${RETVAL}
