#!/bin/sh -u

### BEGIN INIT INFO
# Provides:          kesl
# Required-Start:    $local_fs
# Required-Stop:     $remote_fs
# Should-Start:
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Description:       See kesl(5) man page
### END INIT INFO

export LC_ALL=C

DieRc()
{
    _DieRc_res=1
    if [ "$1" -eq "$1" ] 2>/dev/null; then
        _DieRc_res=$1
        shift
    fi

    printf "\n%s: ERROR: %s\n\n" "$(basename "$0")" "$*" >&2

    exit "${_DieRc_res}"
}

Die()
{
    DieRc 1 "$@"
}

Echo()
{
    printf "%b\n" "$*"
}

EchoBold()
{
    printf '\033[1m'
    Echo "$@"
    printf '\033[0m'
}

Repeat()
{
    _Repeat_n="$1"
    _Repeat_ch="$2"
    printf "%${_Repeat_n}s\n" | tr " " "${_Repeat_ch}"
}

PsCommand()
{
    COLUMNS=4096 ps "$@"
}

Log()
{
    Echo "$*"
}


readonly PRODUCT_NAME='kesl'
readonly LAUNCHER='/opt/kaspersky/kesl/libexec/launcher'
readonly LAUNCHER_LOG='/var/log/kaspersky/kesl/kesl_launcher.log'
readonly SUBSYS_LOCK_DIRECTORY='/var/lock/subsys/'
readonly SUBSYS_LOCK_FILE="${SUBSYS_LOCK_DIRECTORY}kesl"
readonly SYSTEMD_SERVICE_FILENAME='kesl.service'
readonly PRODUCT_RUNNING_STATUS='0'
readonly PRODUCT_STOPPED_STATUS='3'
readonly PRODUCT_STOPPED='0'
readonly PRODUCT_ALREADY_STOPPED='2'
readonly PRODUCT_STARTED='0'
readonly PRODUCT_ALREADY_STARTED='2'
readonly MSG_APP_IS_RUNNING="${PRODUCT_NAME} is running"
readonly MSG_APP_IS_STOPPED="${PRODUCT_NAME} is stopped"
readonly MSG_APP_STARTED="${PRODUCT_NAME} started"
readonly MSG_APP_ALREADY_STARTED="${PRODUCT_NAME} already started"
readonly MSG_APP_STOPPED="${PRODUCT_NAME} stopped"
readonly MSG_APP_ALREADY_STOPPED="${PRODUCT_NAME} already stopped"
readonly MSG_APP_STOP_ERROR="${PRODUCT_NAME} stop failed. See ${LAUNCHER_LOG}"
readonly MSG_APP_STATUS_ERROR="${PRODUCT_NAME} status failed. See ${LAUNCHER_LOG}"
readonly MSG_APP_START_ERROR="${PRODUCT_NAME} start failed. See ${LAUNCHER_LOG}"

IsSystemd()
{
    if [ ! -d '/etc/systemd/system' ]; then
        return 1
    fi

    PsCommand -p 1 -o comm= | grep -F 'systemd' >/dev/null 2>&1 || return $?
}

CreateSubsysLockFile()
{
    if [ -d "${SUBSYS_LOCK_DIRECTORY}" ]; then
        touch "${SUBSYS_LOCK_FILE}" 2>/dev/null
    fi
}

RemoveSubsysLockFile()
{
    if [ -d "${SUBSYS_LOCK_DIRECTORY}" ]; then
        rm -f "${SUBSYS_LOCK_FILE}" 2>/dev/null
    fi
}

status()
{
    _status_res=0
    "${LAUNCHER}" "--status" 1>/dev/null 2>&1
    _status_res=$?
    if [ "${_status_res}" -eq "${PRODUCT_RUNNING_STATUS}" ]; then
        Echo "${MSG_APP_IS_RUNNING}"
    elif [ "${_status_res}" -eq "${PRODUCT_STOPPED_STATUS}" ]; then
        Echo "${MSG_APP_IS_STOPPED}"
    else
        Echo "${MSG_APP_STATUS_ERROR}"
    fi
    return ${_status_res}
}

start()
{
    if [ ${PPID} -ne 1 ] && IsSystemd; then
        systemctl start "${SYSTEMD_SERVICE_FILENAME}"
        _start_out=''
        _start_out="$(systemctl is-active "${SYSTEMD_SERVICE_FILENAME}")"
        if [ "${_start_out}" = 'active' ]; then
            Echo "${MSG_APP_STARTED}"
            return 0
        else
            Echo "${MSG_APP_START_ERROR}"
            return 255
        fi
    else
        "${LAUNCHER}" 1>/dev/null 2>&1
        _start_res=$?
        if [ "${_start_res}" -eq "${PRODUCT_STARTED}" ]; then
            CreateSubsysLockFile
            Echo "${MSG_APP_STARTED}"
        elif [ "${_start_res}" -eq "${PRODUCT_ALREADY_STARTED}" ]; then
            Echo "${MSG_APP_ALREADY_STARTED}"
        else
            Echo "${MSG_APP_START_ERROR}"
            return 255
        fi
        return 0
    fi
}

DoStop()
{
    _DoStop_res=0
    "${LAUNCHER}" "--stop" 1>/dev/null 2>&1
    _DoStop_res=$?
    RemoveSubsysLockFile
    if [ "${_DoStop_res}" -eq "${PRODUCT_STOPPED}" ]; then
        Echo "${MSG_APP_STOPPED}"
    elif [ "${_DoStop_res}" -eq "${PRODUCT_ALREADY_STOPPED}" ]; then
        Echo "${MSG_APP_ALREADY_STOPPED}"
        return 0
    else
        Echo "${MSG_APP_STOP_ERROR}"
    fi
    return ${_DoStop_res}
}

stop()
{
    if [ ${PPID} -ne 1 ] && IsSystemd; then
        systemctl stop "${SYSTEMD_SERVICE_FILENAME}"
        _stop_out=''
        _stop_out="$(systemctl is-active "${SYSTEMD_SERVICE_FILENAME}")"
        if [ "${_stop_out}" != 'active' ]; then
            Echo "${MSG_APP_STOPPED}"
        else
            Echo "${MSG_APP_STOP_ERROR}"
        fi
    else
        DoStop || return $?
    fi
}

restart()
{
    stop
    start
}

Main()
{
    case "${1:-}" in
        start|stop|status|restart)
            "${1}" || return $?
            ;;
        *)
            Echo "Usage: $(basename "$0") {start|stop|status|restart}" >&2
            return 1
            ;;
    esac
}

Main "$@"
