#!/bin/bash
### BEGIN INIT INFO
# Provides: mqttcool
# Required-Start: $network $syslog $remote_fs
# Should-Start: $named $syslog $time
# Required-Stop: $network $syslog
# Should-Stop: $named $syslog $time
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Short-Description: MQTT.Cool Server
# Description:       MQTT.Cool Server - https://mqtt.cool
### END INIT INFO

PATH="/bin:/sbin:/usr/bin"

if [ -r /lib/lsb/init-functions ]; then
    . /lib/lsb/init-functions
else
    exit 1
fi

# MQTT.Cool specific settings
# MC_HOME=/opt/mqtt.cool
MC_HOME=%MC_HOME%
# MC_USER=mqttcool
MC_USER=%MC_USER%
# MC_GROUP=mqttcool
MC_GROUP=%MC_GROUP%

DAEMON="${MC_HOME}/bin/unix-like/mc.sh"
LS_LOG="${MC_HOME}/logs/LS.out"
PIDFILE="${MC_HOME}/bin/unix-like/daemon.pid"

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="MQTT.Cool Server"
NAME="MQTT.Cool"

# For SELinux we need to use 'runuser' not 'su'
if [ -x "/sbin/runuser" ]; then
    SU="/sbin/runuser"
else
    SU="/bin/su"
fi

RETVAL="0"

. /etc/rc.status
rc_reset

function getlspid() {
    local _pid
    _pid=$(cat "${PIDFILE}" 2> /dev/null)
    echo "${_pid}"
}

function checkpid() {
    local _pid=$(getlspid)
    if [ -n "${_pid}" ]; then
        kill -0 "${_pid}" 2> /dev/null
        return ${?}
    fi
    return 1
}

function start() {
    echo -n "Starting MQTT.Cool"
    if checkpid; then
        # already started
        echo -n " (already started)"
        rc_failed 0
    else
        $SU - "${MC_USER}" -c "WRITE_PID=\"${PIDFILE}\" ${DAEMON} run &" >> "${LS_LOG}" 2>&1
        RETVAL="$?"
        if [ "$RETVAL" -eq 0 ]; then
            rc_failed 0
            touch /var/run/rc${NAME}
        else
            rc_failed 7
        fi
    fi
    rc_status -v
}

# Status has a slightly different for the status command:
# 0 - service running
# 1 - service dead, but /var/run/  pid  file exists
# 2 - service dead, but /var/lock/ lock file exists
# 3 - service not running

# NOTE: checkproc returns LSB compliant status values.
function status() {
    echo -n "Checking for MQTT.Cool"
    if checkpid; then
        rc_failed 0
    else
        rc_failed 3
    fi
    rc_status -v
}

function stop() {
    echo -n "Shutting down MQTT.Cool"
    if [ -f "/var/run/rc${NAME}" ]; then
        if ! checkpid; then
            rc_failed 0
        else
            local _pid=$(getlspid)
            if [ -n "${_pid}" ]; then
                kill -TERM "${_pid}"
                rc_failed 0
            else
                rc_failed 7
            fi
        fi
    else
        rc_failed 1
    fi
    rc_status -v
}


# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
    *)
        echo "Usage: $0 {start|stop|status}"
        exit 1
        ;;
esac
rc_exit
