#! /bin/bash
# /etc/init.d/scilogd
#

### BEGIN INIT INFO
# Provides:          scilogd
# Required-Start:    $network
# Required-Stop:     $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/Stop ScienceLogic agent
### END INIT INFO

DAEMON=/usr/bin/scilogd
PIDFILE=/etc/scilog/scilogd.pid

. /lib/lsb/init-functions

ld_preload_chk () {
    if [ -f /etc/scilog/ld_preload ]; then
        export LD_PRELOAD=/usr/share/scilog/libscilog.so.1.0.1
        export LD_LIBRARY_PATH=/usr/share/scilog
        $DAEMON --test
        if [ $? -gt 0 ]; then
            unset LD_PRELOAD
            echo "Warning: overlay issue, preload has been disabled"
            log_warning_msg "preload has been disabled"
            exit 0
        fi
        unset LD_PRELOAD
    fi
}

start_agent ()
{
    log_daemon_msg "Starting ScienceLogic agent" "scilogd"

    if [ -e $PIDFILE ] && kill -0 $(cat $PIDFILE); then
        log_end_msg 0
        echo "scilogd is already running"
        exit 0
    fi

    export LD_LIBRARY_PATH=/usr/share/scilog

    # validate LD_PRELOAD
    ld_preload_chk

    $DAEMON
    RETVAL=$?
    unset LD_LIBRARY_PATH

    log_end_msg $?
    log_success_msg "ScienceLogic agent has started" "scilogd"
}

stop_agent ()
{
    log_daemon_msg "Stopping ScienceLogic agent" "scilogd"

    if [ -e /etc/scilog/scilogd.pid ]; then
        # kill the agent
        kill -USR1 `cat /etc/scilog/scilogd.pid`
        # wait for agent to exit
        for i in $(seq 1 60);
        do
            if [ ! -e /etc/scilog/scilogd.pid ]; then
                pkill -f 'java.*scilogjmxd'
                exit 0
            fi
        sleep 1
        done
        # safe shutdown failed, lets kill it
        kill -KILL `cat /etc/scilog/scilogd.pid`
    else
        kill -USR1 `pgrep $DAEMON` 2>/dev/null
        # wait for agent to exit
        for i in $(seq 1 60);
        do
            if [ -z "`pgrep $DAEMON`" ]; then
                exit 0
            fi
        sleep 1
        done
        # safe shutdown failed, lets kill it
        kill -KILL `pgrep $DAEMON` 2>/dev/null
        pkill -f 'java.*scilogjmxd'
    fi

    sleep 1
    RETVAL=$?

    log_end_msg $?
}

# start and stop below are the legacy, deprecated way to start and stop services.
# They are left here for continuity.
# exec_start and exec_stop are pointed to by scilog.service, defined in preinst.
# This is the modern way to start and stop services. They are defined in this
# file for convenience.
# The rc interface:
case "$1" in
  start)
    start_agent
    ;;
  exec_start)
    start_agent
    ;;
  stop)
    stop_agent
    ;;
  exec_stop)
    stop_agent
    ;;
  restart)
    stop_agent
    start_agent
    ;;
# upgrade is same as start except that it does not check if agent is already running 
  upgrade)
    log_daemon_msg "Starting ScienceLogic agent" "scilogd"

    # check selinux
    selinux_chk

    # validate ld.so.preload
    ld_preload_chk

    export LD_LIBRARY_PATH=/usr/share/scilog
    $DAEMON
    RETVAL=$?
    unset LD_LIBRARY_PATH
    log_success_msg "ScienceLogic agent has started" "scilogd"
    log_end_msg $?
    ;;
  collector_restart)
    start-stop-daemon --retry 30 --quiet --signal 10 --exec $DAEMON --stop $DAEMON
    wait
    export LD_LIBRARY_PATH=/usr/share/scilog
    $DAEMON
    unset LD_LIBRARY_PATH
    ;;
  collector_start)
    if /bin/pidof $DAEMON >> /dev/null
    then 
        echo "scilogd is already running"
        log_end_msg 0
        exit 0
    fi

    export LD_LIBRARY_PATH=/usr/share/scilogd
    $DAEMON
    unset LD_LIBRARY_PATH
    ;;
  collector_stop)
    start-stop-daemon --retry 30 --quiet --signal 10 --exec $DAEMON --stop $DAEMON
    wait
    ;;
  status)
    if [ -e $PIDFILE ] && kill -0 $(cat $PIDFILE); then
        echo "scilogd agent is running"
        exit 0
    else
        echo "scilogd agent is not running"
        exit 3
    fi
    ;;
  *)
    echo " * Usage: /etc/init.d/scilogd {start|stop|restart|status}"
    exit 1
    ;;
esac

exit 0
