#!/bin/sh
#
# centengine - Monitoring core.
#
# /etc/init.d/centengine
#
# chkconfig: - 70 30
# description: Centreon Engine
#
### BEGIN INIT INFO
# Provides: centengine
# Required-Start: $local_fs
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start and stop Centreon Engine
### END INIT INFO

prefix=@CMAKE_INSTALL_PREFIX@
var_dir=@ENGINE_VAR_LOG_DIR@
rw_dir=@ENGINE_VAR_LIB_DIR@/rw
binary=@CMAKE_INSTALL_FULL_SBINDIR@/centengine
config_file=@PREFIX_ENGINE_CONF@/centengine.cfg
status_file=$var_dir/status.dat
retention_file=$var_dir/retention.dat
command_file=$rw_dir/centengine.cmd
remove_command_file=1
lock_file=@LOCK_FILE@
pid_file=@PID_FILE@
user=@USER@
group=@GROUP@
timeout=300
debug=0

servicename=$(basename "$0")
command="$1"

usage="Usage: $servicename {start|stop|restart|try-restart|reload|force-reload|status}"
lsb_init_functions="/lib/lsb/init-functions"

# Check source function library
if [ ! -r $lsb_init_functions ]; then
    echo "$servicename: not able to read $lsb_init_functions: script cannot run" 1>&2
    exit 1
fi

# Load source function library
. $lsb_init_functions

# Check arguments (only one parameter)
if [ $# -ne 1 ]; then
    if [ $# -lt 1 -o "$command" = "" ]; then
	log_failure_msg "$servicename: action not specified"
    else
	log_failure_msg "$servicename: too many parameters"
    fi
    log_warning_msg "$usage"
    exit 2
fi

# Check if we can find the binary.
if [ ! -x $binary ]; then
    if [ $command = "stop" ] ; then
	log_warning_msg "$servicename: binary $binary not found: $command request ignored"
	exit 0
    else
	log_failure_msg "$servicename: binary $binary not found: $command request failed"
	exit 5
    fi
fi

check_configuration() {
# Check if we can find the configuration file.
    if [ ! -r $config_file ]; then
	log_failure_msg "$servicename: configuration file $config_file not found: $command request failed"
	exit 6
    fi

    su - $user -c "$binary -v $config_file > /dev/null 2>&1"
    if [ $? -ne 0 ]; then
    	log_failure_msg "$servicename $command: bad configuration."
    	exit 6
    fi
}

service_start() {
    check_configuration

    if [ -f "$pid_file" ] && [ ! -d "/proc/$(cat $pid_file)" ]; then
	rm -f "$pid_file" > /dev/null 2>&1
    fi

    if [ ! -f "$pid_file" ]; then
	if [ $remove_command_file -ne 0 ] ; then
		rm -f $command_file > /dev/null 2>&1
	fi
	su - $user -c "touch $var_dir/centengine.log $retention_file"

	if [ $debug -eq 1 ]; then
	    echo "/tmp/%e.core.%t.%u.%p" > /proc/sys/kernel/core_pattern
	    pid=`su - $user -c "ulimit -c unlimited; $binary $config_file >> $var_dir/init.debug 2>&1 & echo \\$!"`
	else
	    pid=`su - "$user" -c "$binary $config_file > /dev/null  2>&1 & echo \\$!"`
	fi

	sleep 2
	kill -s 0 $pid > /dev/null 2>&1
        if [ $? -ne 0 ]; then
	    log_failure_msg "$servicename $command: failed."
    	    exit 1
	fi
	echo "$pid" > "$pid_file"
        touch $lock_file;

	if [ $debug -eq 1 ]; then
	    su - $user -c "echo pid=$pid >> $var_dir/init.debug"
	    # strace -fF -p $pid 2>&1 >> "$var_dir/init.debug" &
	fi
    fi
}

service_stop() {
    if [ -f "$pid_file" ]; then
	pid=`cat "$pid_file"`
	kill -TERM $pid

	for i in $(seq 1 $timeout); do
	    kill -s 0 $pid > /dev/null 2>&1
 	    if [ $? -ne 0 ]; then
 		break
 	    fi
 	    echo -n '.'
 	    sleep 1
	done

	kill -s 0 $pid > /dev/null 2>&1
	if [ $? -eq 0 ]; then
	    kill -KILL $pid
	fi
    fi
    rm -f $lock_file $pid_file
    if [ $remove_command_file -ne 0 ] ; then
	rm -f $command_file
    fi
}

service_restart() {
    remove_command_file=0
    service_stop
    service_start
}

service_reload() {
    check_configuration
    if [ -f "$pid_file" ] ; then
	pid=`cat "$pid_file"`
	kill -HUP $pid
    else
	log_failure_msg "$servicename $command: $binary not running."
	exit 7
    fi
}

service_force_reload() {
    check_configuration
    if [ -f "$pid_file" ] ; then
	pid=`cat "$pid_file"`
	kill -HUP $pid
    else
	service_start
    fi
}

service_status() {
    if [ -f "$pid_file" ]; then
	kill -s 0 `cat "$pid_file"`
	if [ $? -eq 0 ]; then
    	    log_success_msg "$servicename $command: running"
    	    exit 0
	else
	    log_failure_msg "$servicename $command: $binary dead with existing pid file: $pid_file"
	    exit 2
	fi
    else
	log_failure_msg "$servicename $command: $binary not running"
	exit 3
    fi
}

# see how we were called.
case $command in
    start)
	service_start
	;;

    stop)
	service_stop
	;;

    restart)
	service_restart
	;;

    try-restart)
	pidofproc $binary > /dev/null 2>&1
	if [ $? -eq 0 ]; then
	    service_stop
	    service_start
	fi
	;;

    reload)
	service_reload
	;;

    force-reload)
	service_force_reload
	;;

    status)
	service_status
	;;

    *)
	log_failure_msg "$servicename: command \"$command\" not recognized"
	log_warning_msg "$usage"
	exit 3
	;;
esac

log_success_msg "$servicename $command: done"
exit 0
