#!/bin/sh
# Tripwire Axon Agent
#
# update-rc.d: 2345 90 1
# description: Agent software for the Tripwire family of compliance and security products.
# processname: twagent
# pidfile: /var/lock/tripwire/twagent.pid
#
# Source function library: start_daemon(), killproc() and status_of_proc()


### BEGIN INIT INFO
# Provides:          tripwire-axon-agent
# Required-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

set_Ubuntu_locale_environment()
{
    # /etc/default/locale contains all locale settings on Debian and Ubuntu
    if [ -s "/etc/default/locale" ]; then
        . /etc/default/locale
        export LANG LANGUAGE
    fi
}

. /lib/lsb/init-functions

if grep -Rqs "Ubuntu" /etc/*release; then
    set_Ubuntu_locale_environment
fi

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}"
RETVAL=0
PID_FILE="/var/lock/tripwire/${DAEMON_NAME}.pid"


if [ ! -x "${DAEMON}" ]; then
    log_failure_msg "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}"
        RETVAL=$?
        rm -f "/var/lock/tripwire/${DAEMON_NAME}"*
        echo
        ;;
    status)
        status_of_proc -p "${PID_FILE}" "${SERVICE_NAME}" "${SERVICE_NAME}"
        RETVAL=$?
        ;;
    restart)
        "$0" stop
        "$0" start
        RETVAL=$?
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart|force-reload}"
        exit 1
        ;;
esac

exit $RETVAL

