#!/bin/sh
##
## Copyright 2009-2012,2015 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 Redhat
# chkconfig: - 71 31
# description: Centreon Broker
# processname: cbd
# config: TO_CHANGE
# pidfile: TO_CHANGE
### END INIT INFO

# Select distribution type.
. /etc/init.d/functions

# Configuration parameters.
watchdog_file="@PREFIX_CONF@/watchdog.json"
config_dir="@PREFIX_CONF@"
cbwd="@PREFIX_BIN@/cbwd"
pid_path=/var/run
lockfile=/var/lock/subsys/cbd
stop_timeout=10
user="@USER@"
pidfile="${pid_path}/cbwd.pid"
debug=0

# 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."
    failure "cbd startup"
    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}"
    touch "${lockfile}"
    success "cbd startup"
  else
    ${global_retval} = ${RETVAL}
    failure "cbd startup"
  fi
  echo
  return ${global_retval}
}

# Stop cbd.
stop_watchdog() {
  if [ -f "${pidfile}" ] ; then
    echo -n "Stopping Centreon Broker watchdog: "
    killproc -p "${pidfile}" -d ${stop_timeout} "${cbwd}"
    RETVAL=$?
    if [ ${RETVAL} = 0 ] ; then
      rm -f "${pidfile}"
      success "cbd shutdown"
    else
      failure "cbd shutdown"
    fi
    echo
  else
    echo "Centreon Broker watchdog is not running."
    failure "cbd shutdown"
  fi
  rm -f "${lockfile}"
}

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

# Status
status_watchdog() {
  if [ -f "${pidfile}" ] ; then
    status -p ${pidfile} ${cbwd}
  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}
