#!/bin/sh

# chkconfig: 345 90 90
# description: whatap-otel is a agent for OTLP.
# processname: whatap_agent

# Source function library.
. /etc/rc.d/init.d/functions
AGENT_APP_TYPE=10

export WHATAP_HOME=/usr/whatap/otel
export WHATAP_CONFIG_HOME=
export WHATAP_CONFIG=whatap.conf
export WHATAP_APP_TYPE=${AGENT_APP_TYPE}

if [ -x /usr/whatap/otel/whatap_otel ]; then
    exec=/usr/whatap/otel/whatap_otel
else
    exit 5
fi

prog=${exec##*/}

lockfile=/var/lock/subsys/whatap_otel

start()
{
    echo "WhaTap Agen whatap-otel" "Init try start" >&2
    echo -n $"Starting WhaTap Agent whatap-otel: "
    daemon $exec -t=${AGENT_APP_TYPE} -d=1
    rv=$?
    echo >&2
    [ $rv -eq 0 ] && touch $lockfile
    echo "WhaTap Agent whatap-otel" "Init start end" >&2
    return $rv
}

stop()
{
    echo "Whatap Agent whatap-otel" "Init try stop" >&2
    echo -n $"Shutting down Whatap Agent whatap-otel: "
    killproc $prog
    rv=$?
    echo
    [ $rv -eq 0 ] && rm -f $lockfile
    echo "WhaTap Agent whatap-otel " "Init stop end" >&2
    return $rv
}

restart()
{
    stop
    start
}

version()
{
    #echo "${WHATAP_VERSION}"
    ${exec} version
}

case "$1" in
    start|stop|restart)
        $1
        ;;
    force-reload)
        restart
        ;;
    status)
        status $prog
        ;;
    try-restart|condrestart)
        if status $prog >/dev/null ; then
            restart
        fi
        ;;
    reload)
        action $"Service ${0##*/} does not support the reload action: " /bin/false
        exit 3
        ;;
    version)
        version
        ;;
    *)
    echo $"Usage: $0 {start|stop|status|version|restart|try-restart|force-reload}"
    exit 2
    ;;
esac

