#!/bin/sh
# Tripwire Axon Agent
#
# chkconfig: 2345 90 1
# description: Agent software for the Tripwire family of compliance and security products.
# processname: twagent
# pidfile: /var/lock/tripwire/twagent.pid

### BEGIN INIT INFO
# Provides: tripwire-axon-agent
# Required-Start:
# Should-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Tripwire Axon Agent
# Description: Agent software for the Tripwire family of compliance and security products.
### END INIT INFO

SERVICE_NAME=$(basename "$0")
SERVICE="Tripwire Axon Agent"
AGENT_DIR="/opt/tripwire/agent"
PLUGINS_DIR="${AGENT_DIR}/plugins"
DAEMON_NAME=twagent
DAEMON="${AGENT_DIR}/${DAEMON_NAME}"
PID_FILE="/var/lock/tripwire/twagent.pid"
RETVAL=0

set_SUSE_locale_environment()
{
    # /etc/sysconfig/language contains all locale settings on SUSE
    # all locale variables in the "language" have format "RC_..."
    # source the file and update the locale settings
    . /etc/sysconfig/language
    if [ "X$ROOT_USES_LANG" = "Xyes" ] ; then
        locale_vars="     \
        LANG              \
        LC_CTYPE          \
        LC_NUMERIC        \
        LC_TIME           \
        LC_COLLATE        \
        LC_MONETARY       \
        LC_MESSAGES       \
        LC_PAPER          \
        LC_NAME           \
        LC_ADDRESS        \
        LC_TELEPHONE      \
        LC_MEASUREMENT    \
        LC_IDENTIFICATION \
        LC_ALL"


        unset LC_ALL
        for lc in $locale_vars
        do
            eval val="\$RC_$lc"
            if test -n "$val"; then
                eval $lc="\$RC_$lc"
                export $lc
            fi
        done
        unset lc val
    fi
}

if [ -f /lib/lsb/init-functions ]; then


    . /lib/lsb/init-functions
    if grep -Rqs "SUSE" /etc/*release; then

        set_SUSE_locale_environment

    fi

    alias START_DAEMON=start_daemon
    alias STATUS=pidofproc_status
    alias LOG_SUCCESS=log_success_msg
    alias LOG_FAILURE=log_failure_msg
    alias LOG_WARNING=log_warning_msg
elif [ -f /etc/rc.d/init.d/functions ]; then
    . /etc/rc.d/init.d/functions
    alias START_DAEMON=daemon
    alias STATUS=status
    alias LOG_SUCCESS=success
    alias LOG_FAILURE=failure
    alias LOG_WARNING=passed
else
    echo "Error: Your platform is not supported by $0" > /dev/stderr
    exit 1
fi

pidofproc_status()
{
    pidofproc -p "${PID_FILE}" ${DAEMON_NAME} 2>&1 > /dev/null
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        LOG_SUCCESS "${SERVICE_NAME} is running"
    else
        LOG_SUCCESS "${SERVICE_NAME} is not running"
    fi
    return $RETVAL
}

if [ ! -x "${DAEMON}" ]; then
    LOG_FAILURE "Error: ${DAEMON} is not executable"
    exit 1
fi

case "$1" in
    start)
        echo -n "Starting $SERVICE ..."
        START_DAEMON ${DAEMON} --service.mode=true --service.type="SysV" --agent.dir="${AGENT_DIR}" --plugins.dir="${PLUGINS_DIR}"
        RETVAL=$?
        echo
        ;;
    stop)
        echo -n "Stopping $SERVICE daemon: "
        killproc -p "${PID_FILE}" ${DAEMON_NAME}
        RETVAL=$?
        rm -f "${PID_FILE}"
        echo
        ;;
    status)
        STATUS ${DAEMON_NAME}
        RETVAL=$?
        ;;
    restart|force-reload)
        $0 stop
        $0 start
        RETVAL=$?
        ;;
    *)
        printf "Usage: %q {start|stop|status|restart|force-reload}\n" "$0" >&2
        exit 1
        ;;
esac

exit ${RETVAL}

