#!/bin/sh
##
## Copyright 2009-2012,2016 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
# Provides:             cbd
# Required-Start:       $local_fs $network
# Required-Stop:        $local_fs $network
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Start daemon cbd at boot
# Description:          Enable service provided Centreon Broker
### END INIT INFO


PKGNAME=cbd
DESC="Centreon Broker"
DAEMON="@PREFIX_BIN@/cbwd"
PID_PATH="/var/run"
WATCHDOG_FILE="@PREFIX_CONF@/watchdog.json"
CONFIG_DIR="@PREFIX_CONF@"
pidfile="${PID_PATH}/cbwd.pid"

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

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

# Include the default user configuration if exists
[ -r /etc/default/${PKGNAME} ] && . /etc/default/${PKGNAME}

# Load the VERBOSE setting and other rcS variables
[ -f /etc/default/rcS ] && . /etc/default/rcS

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

if [ -z "${RUN_AT_STARTUP}" -o "${RUN_AT_STARTUP}" != "YES" ]; then
  log_warning_msg "Not starting $PKGNAME, edit /etc/default/$PKGNAME to start it."
  exit 0
fi

if [ -z "${USER}" ]; then
  log_warning_msg "Not starting $PKGNAME, USER not set in /etc/default/$PKGNAME."
  exit 0
fi

# Start cbd.
do_start()
{
  global_retval=0
  [ "${VERBOSE}" != "no" ] && log_daemon_msg "Starting Centreon Broker watchdog" "${PKGNAME}"
  start-stop-daemon --start --background --quiet --pidfile ${pidfile} --exec ${DAEMON} \
    --chuid ${USER} --user ${USER} --test
  [ "$?" = "0" ] || global_retval=1
  start-stop-daemon --start --background --quiet --pidfile ${pidfile} --exec ${DAEMON} \
    --make-pidfile --chuid ${USER} --user ${USER} -- ${WATCHDOG_FILE}
  [ "$?" = "0" ] || global_retval=2
  case "${global_retval}" in
    0|1) [ "${VERBOSE}" != "no" ] && log_end_msg 0 ;;
    2) [ "${VERBOSE}" != "no" ] && log_end_msg 1 ;;
  esac
  return ${global_retval}
}

# Stop cbd.
do_stop()
{
  [ "${VERBOSE}" != no ] && log_daemon_msg "Restarting Centreon Broker watchdog" "${PKGNAME}"
  start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --user ${USER} --pidfile ${pidfile}
  [ "$?" = "2" ] && global_retval=2
  rm -f ${pidfile}
  [ "$?" = "0" ] || global_retval=1
  case "${global_retval}" in
    0|1) [ "${VERBOSE}" != "no" ] && log_end_msg 0 ;;
    2) [ "${VERBOSE}" != "no" ] && log_end_msg 1 ;;
  esac
}

# Send kill HUP
do_reload()
{
  [ "${VERBOSE}" != no ] && log_daemon_msg "Reloading Centreon Broker watchdog" "${PKGNAME}"
  start-stop-daemon --stop --quiet --signal=HUP --user ${USER} --pidfile ${pidfile}
  case "$?" in
    0|1) [ "${VERBOSE}" != "no" ] && log_end_msg 0 ;;
    2) [ "${VERBOSE}" != "no" ] && log_end_msg 1 ;;
  esac
}

# Status
status_all()
{
  status_of_proc ${DAEMON} "Centreon Broker watchdog" -p ${pidfile}
}

# Switch case.
case "$1" in
  start)
    do_start
    ;;
  stop)
    do_stop
    ;;
  restart)
    do_stop
    do_start
    ;;
  reload)
    do_reload
    ;;
  status)
    status_all
    ;;
  *)
    echo "Usage: ${daemon_name} {start|stop|restart|status}"
    exit 3
esac
