#!/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.
master_file="/DBA/centreon-broker/2.11.3/etc/master.run"
config_dir="/DBA/centreon-broker/2.11.3/etc"
cbd="/DBA/centreon-broker/2.11.3/bin/cbd"
pid_path=/var/run
stop_timeout=10
user="centreon-broker"
debug=0

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

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

# Start cbd.
start() {
  global_retval=0
  cat ${master_file} | grep -v '^#'| while read line; do
    daemon_name="cbd_$(echo ${line} | awk '{ print $1 }')"
    daemon_config="${config_dir}/$(echo ${line} | awk '{ print $2 }')"
    running="$(echo ${line} | awk '{ print $3 }')"
    pidfile="${pid_path}/${daemon_name}.pid"
    if [ "${running}" = "y" ]; then
      if [ -f "${pidfile}" ] ; then
        is_running=$(ps -edf | grep "${cbd} ${daemon_config}" | grep -v grep | wc -l )
        if [ $is_running = 0 ] ; then
          rm -f "$pidfile"
        else
          echo -n "${daemon_name} already running"
          return 1
        fi
      fi

      # Start cbd.
      echo -n "Starting ${daemon_name}: "
      if [ $debug -eq 1 ]; then
	  echo "/tmp/%e.core.%t.%u.%p" > /proc/sys/kernel/core_pattern
	  PID=`su - "${user}" -c "ulimit -c unlimited; ${cbd} ${daemon_config} >/dev/null 2>&1 & echo \\$!"`
      else
	  PID=`su - "${user}" -c "${cbd} ${daemon_config} >/dev/null 2>&1 & echo \\$!"`
      fi
      RETVAL=$?
      if [ ${RETVAL} = 0 ]; then
        echo ${PID} > "${pidfile}"
        echo "cbd startup [success]"
      else
        ${global_retval} = ${RETVAL}
        echo "cbd startup [failed]"
      fi
      echo
    fi
  done
  return ${global_retval}
}

# Stop cbd.
stop() {
  cat ${master_file} | grep -v '^#'| while read line; do
    daemon_name="cbd_$(echo ${line} | awk '{ print $1 }')"
    daemon_config="${config_dir}/$(echo ${line} | awk '{ print $2 }')"
    running="$(echo ${line} | awk '{ print $3 }')"
    pidfile="${pid_path}/${daemon_name}.pid"
    if [ "${running}" = "y" ]; then
      echo -n "Stopping ${daemon_name}: "
      kill -TERM `cat "${pidfile}"` > /dev/null 2>&1
      sleep ${stop_timeout}
      kill -KILL `cat "${pidfile}"` > /dev/null 2>&1
      RETVAL=$(ps -edf | grep "${cbd} ${daemon_config}" | grep -v grep | wc -l)
      if [ ${RETVAL} = 0 ] ; then
        rm -f "${pidfile}"
        echo "cbd shutdown [success]"
      else
        echo "cbd shutdown [failure]"
      fi
      echo
    fi
  done
}

# Send kill HUP
reload() {
  cat ${master_file} | grep -v '^#'| while read line; do
    daemon_name="cbd_$(echo ${line} | awk '{ print $1 }')"
    daemon_config="${config_dir}/$(echo ${line} | awk '{ print $2 }')"
    running="$(echo ${line} | awk '{ print $3 }')"
    reload="$(echo ${line} | awk '{ print $4 }')"
    pidfile="${pid_path}/${daemon_name}.pid"
    if [ "${running}" = "y" -a "${reload}" = "y" ]; then
      echo -n "Reloading ${daemon_name}: "
      kill -HUP `cat "${pidfile}"`
      RETVAL=$?
      if [ ${RETVAL} = 0 ]; then
        echo "cbd reload [success]"
      else
        echo "cbd reload [failure]"
      fi
      echo
    fi
  done
}

# Status
status_all() {
  cat ${master_file} | grep -v '^#'| while read line; do
    daemon_name="cbd_$(echo ${line} | awk '{ print $1 }')"
    daemon_config="${config_dir}/$(echo ${line} | awk '{ print $2 }')"
    running="$(echo ${line} | awk '{ print $3 }')"
    pidfile="${pid_path}/${daemon_name}.pid"
    if [ "${running}" = "y" ]; then
      is_running=$(ps -edf | grep "${cbd} ${daemon_config}" | grep -v grep | wc -l)
      if [ $is_running = 0 ] ; then
        echo "${daemon_name} NOT running"
      else
        echo "${daemon_name} running"
      fi
    fi
  done
}

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

exit ${RETVAL}
