#!/bin/sh
# The file: "/home/iliyap/b/lfs10/bin32/generated/supervisor//kesl-supervisor" is generated at compile time 
#     from file: "/home/iliyap/scm/lfs/supervisor/supervisor.template" 
#     by replacing "%LFS_SUPERVISOR_FILENAME%" string 
#     with string: "kesl-supervisor". 
# The file: "/home/iliyap/b/lfs10/bin32/generated/supervisor//kesl-supervisor" is generated at compile time 
#     from file: "/home/iliyap/b/lfs10/bin32/generated/supervisor//kesl-supervisor" 
#     by replacing "%LFS_SHORT_DESCRIPTION%" string 
#     with content of file: "/home/iliyap/b/lfs10/bin32/generated/supervisor/../localization/en_title.utf8". 
# The file: "/home/iliyap/b/lfs10/bin32/generated/supervisor//kesl-supervisor" is generated at compile time 
#     from file: "/home/iliyap/b/lfs10/bin32/generated/supervisor//kesl-supervisor" 
#     by replacing "%LFS_PRODUCT_NAME%" string 
#     with string: "kesl". 
# The file: "/home/iliyap/b/lfs10/bin32/generated/supervisor//kesl-supervisor" is generated at compile time 
#     from file: "/home/iliyap/b/lfs10/bin32/generated/supervisor//kesl-supervisor" 
#     by replacing "%LFS_LAUNCHER_FILEPATH%" string 
#     with string: "/opt/kaspersky/kesl/libexec/kesl_launcher.sh". 
# The file: "/home/iliyap/b/lfs10/bin32/generated/supervisor//kesl-supervisor" is generated at compile time 
#     from file: "/home/iliyap/b/lfs10/bin32/generated/supervisor//kesl-supervisor" 
#     by replacing "%LFS_LAUNCHER_PID_FILEPATH%" string 
#     with string: "/var/run/kesl_launcher.pid". 
# The file: "/home/iliyap/b/lfs10/bin32/generated/supervisor//kesl-supervisor" is generated at compile time 
#     from file: "/home/iliyap/b/lfs10/bin32/generated/supervisor//kesl-supervisor" 
#     by replacing "%LFS_WDSERVER_FILEPATH%" string 
#     with string: "/opt/kaspersky/kesl/libexec/wdserver". 
# The file: "/home/iliyap/b/lfs10/bin32/generated/supervisor//kesl-supervisor" is generated at compile time 
#     from file: "/home/iliyap/b/lfs10/bin32/generated/supervisor//kesl-supervisor" 
#     by replacing "%LFS_WDSERVER_PID_FILEPATH%" string 
#     with string: "/var/run/wdserver.pid". 
# The file: "/home/iliyap/b/lfs10/bin32/generated/supervisor//kesl-supervisor" is generated at compile time 
#     from file: "/home/iliyap/b/lfs10/bin32/generated/supervisor//kesl-supervisor" 
#     by replacing "%LFS_AGREED_EULA_FILEPATH%" string 
#     with string: "/var/opt/kaspersky/kesl/private/agreed_eula.txt". 
# The file: "/home/iliyap/b/lfs10/bin32/generated/supervisor//kesl-supervisor" is generated at compile time 
#     from file: "/home/iliyap/b/lfs10/bin32/generated/supervisor//kesl-supervisor" 
#     by replacing "%LFS_SYSTEMD_SERVICE_FILENAME%" string 
#     with string: "kesl-supervisor.service". 
# The file: "/home/iliyap/b/lfs10/bin32/generated/supervisor//kesl-supervisor" is generated at compile time 
#     from file: "/home/iliyap/b/lfs10/bin32/generated/supervisor//kesl-supervisor" 
#     by replacing "%LFS_PRODUCT_RUNNING_STATUS%" string 
#     with string: "0". 
# The file: "/home/iliyap/b/lfs10/bin32/generated/supervisor//kesl-supervisor" is generated at compile time 
#     from file: "/home/iliyap/b/lfs10/bin32/generated/supervisor//kesl-supervisor" 
#     by replacing "%LFS_PRODUCT_DEAD_STATUS%" string 
#     with string: "1". 
# The file: "/home/iliyap/b/lfs10/bin32/generated/supervisor//kesl-supervisor" is generated at compile time 
#     from file: "/home/iliyap/b/lfs10/bin32/generated/supervisor//kesl-supervisor" 
#     by replacing "%LFS_PRODUCT_STOPPED_STATUS%" string 
#     with string: "3". 
# The file: "/home/iliyap/b/lfs10/bin32/generated/supervisor//kesl-supervisor" is generated at compile time 
#     from file: "/home/iliyap/b/lfs10/bin32/generated/supervisor//kesl-supervisor" 
#     by replacing "%LFS_BL_READY_MARKER_FILEPATH%" string 
#     with string: "/var/run/kesl.mrk". 

### BEGIN INIT INFO
# Provides:          kesl-supervisor
# Required-Start:    $local_fs
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Kaspersky Endpoint Security 10 for Linux
# Description:       See kesl(5) man page
### END INIT INFO

readonly PRODUCT_NAME='kesl'
readonly LAUNCHER_PID_FILE='/var/run/kesl_launcher.pid'
readonly LAUNCHER='/opt/kaspersky/kesl/libexec/kesl_launcher.sh'
readonly WDSERVER_BIN='/opt/kaspersky/kesl/libexec/wdserver'
readonly WDSERVER_PID='/var/run/wdserver.pid'
readonly LFS_AGREED_EULA_FILEPATH='/var/opt/kaspersky/kesl/private/agreed_eula.txt'
readonly LFS_READY_MARKER_FILEPATH='/var/run/kesl.mrk'
readonly LFS_SYSTEMD_SERVICE_FILENAME='kesl-supervisor.service'
readonly LFS_PRODUCT_RUNNING_STATUS='0'
readonly LFS_PRODUCT_DEAD_STATUS='1'
readonly LFS_PRODUCT_STOPPED_STATUS='3'

readonly PARENT_PID=$PPID
RC=0

RcMsg()
{
    local err
    case ${RC} in
        0)
            ;;
        ### Additional error codes
        120)
            err="pid file not found"
            ;;
        121)
            err="probably is dead but pid file not deleted"
            ;;
        122)
            err="pid file is empty"
            ;;
        201)
            err="signal senging error"
            ;;
        203)
            err="pid file still exists"
            ;;
        *)
            err="unknown code ${RC}"
    esac

    if [ -n "${err}" ]; then
        if [ -n "$1" ]; then
            echo "$1: ${err}"
        else
            echo "${err}"
        fi
    fi
}

KlGetPid()
{
    readonly local currPid="$1"
    readonly local currBin="$2"
    if [ -n "${currPid}" -a -f "${currPid}" ]; then
        local pid
        pid="$(head -n 1 "${currPid}" 2> /dev/null)"
        if [ -z "${pid}" ]; then
            # echo "pid file is empty"
            return 122
        fi
        ps -p "${pid}" --context | grep "${currBin}" >/dev/null 2>&1
        if [ $? -ne 0 ]; then
            return 121
        fi
        echo "${pid}"
        return 0
    else
        # echo "pid file not found"
        return 120
    fi
}

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

    ps -p 1 -o comm= | grep systemd >/dev/null 2>&1 || return 1
}

status()
{
    local statusTxt
    local pid
    pid=$(KlGetPid "${LAUNCHER_PID_FILE}" "${LAUNCHER}")
    RC=$?
    if [ ${RC} -eq 0 ]; then
        kill -0 "${pid}" 2>/dev/null
        if [ $? -eq 0 ]; then
            statusTxt="running"
            RC=${LFS_PRODUCT_RUNNING_STATUS}
        else
            statusTxt="dead"
            RC=${LFS_PRODUCT_DEAD_STATUS}
        fi
    else
        statusTxt="stopped"
        RC=${LFS_PRODUCT_STOPPED_STATUS}
    fi
    echo "${PRODUCT_NAME} is ${statusTxt}"
}

start2()
{
    if "$0" "status" >/dev/null 2>&1 ; then
        echo "${PRODUCT_NAME} is already started"
    else
        local failCnt=0
        "${LAUNCHER}" > /dev/null 2>&1 &
        while true; do
            sleep 1
            if [ -f "${LFS_READY_MARKER_FILEPATH}" ]; then
                RC=0;
                break;
            elif [ ${failCnt} -gt 180 ]; then
                local pid
                pid="$(KlGetPid "${LAUNCHER_PID_FILE}" "${LAUNCHER}")"
                RC=$?
                if [ ${RC} -eq 0 ]; then
                    kill "${pid}" > /dev/null 2>&1
                    wait "${pid}"
                    rm -f "${LAUNCHER_PID_FILE}"
                fi
                echo "${PRODUCT_NAME} could not be started"
                RC=255
                return;
            fi
            failCnt=$((failCnt+1))
        done
        if [ ${RC} -eq 0 ]; then
            echo "${PRODUCT_NAME} started"
        else
            RcMsg "${PRODUCT_NAME} not started"
        fi
    fi
}

start()
{
    if [ ! -x "${LAUNCHER}" ]; then
        echo "${LAUNCHER} was not found"
        RC=255
        return
    fi

    if [ ! -f "${LFS_AGREED_EULA_FILEPATH}" ]; then
        echo "Please accept license first." >&2
        echo "${PRODUCT_NAME} not started" >&2
        exit 1
    fi

    IsSystemd
    RC=$?

    if [ ${RC} -eq 0 ] && [ ${PARENT_PID} -ne 1 ]; then
        systemctl start "${LFS_SYSTEMD_SERVICE_FILENAME}"
        local out
        out="$(systemctl is-active "${LFS_SYSTEMD_SERVICE_FILENAME}")"
        if [ "${out}" = "active" ]; then
            echo "${PRODUCT_NAME} started"
	        RC=${LFS_PRODUCT_RUNNING_STATUS};
        else
            echo "${PRODUCT_NAME} not started"
	        RC=255;
        fi
    else
       start2
    fi
}

stop2()
{
    while true ; do
        local pid
        pid="$(KlGetPid "${LAUNCHER_PID_FILE}" ${LAUNCHER})"
        RC=$?
        if [ ${RC} -ne 0 ]; then
            break
        fi

        kill -TERM "${pid}" > /dev/null 2>&1
        sleep 1

        local wdPid
        wdPid="$(pidof "${WDSERVER_BIN}")";
        if [ ! -z "${wdPid}" ]; then
            continue
        fi

        while kill -0 "${pid}" 2>/dev/null; do
            sleep 1
        done

        rm -f "${LAUNCHER_PID_FILE}"
        break
    done

    if [ ${RC} -eq 0 ]; then
        echo "${PRODUCT_NAME} stopped"
    elif [ ${RC} -eq 120 ]; then
        echo "${PRODUCT_NAME} already stopped"
        RC=0
    else
        RcMsg "${PRODUCT_NAME} could not be stopped"
    fi

    return ${RC}
}

stop()
{
    IsSystemd
    RC=$?

    if [ ${RC} -eq 0 ] && [ ${PARENT_PID} -ne 1 ]; then
        systemctl stop "${LFS_SYSTEMD_SERVICE_FILENAME}"
        local out
        out="$(systemctl is-active "${LFS_SYSTEMD_SERVICE_FILENAME}")"
        if [ "${out}" != "active" ]; then
            echo "${PRODUCT_NAME} stopped"
        else
            echo "${PRODUCT_NAME} could not be stopped"
        fi
    else
       stop2
    fi
}

restart()
{
    stop
    start
}

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

    exit ${RC}
}

Main "$@"
