#! /bin/sh
#
# $Id: //NW/dev/nw_19_8/mcui/scripts/nwui_monitoring.sh#1 $ Copyright (c) 2020 Dell Inc. or its subsidiaries. All Rights Reserved.
#
#
# Copyright (c) 2020 Dell Inc. or its subsidiaries. All Rights Reserved.
#
# This software contains the intellectual property of Dell Inc.
# or is licensed to Dell Inc. from third parties. Use of this
# software and the intellectual property contained therein is
# expressly limited to the terms and conditions of the License
# Agreement under which it is provided by or on behalf of Dell
# Inc. or its subsidiaries.
#
cdir=`dirname ${0}`
nwuibase=`rpm -q --qf "%{INSTPREFIXES}\n" lgtonwui 2> /dev/null`
nwuiservbase=`rpm -q --qf "%{INSTPREFIXES}\n" lgtonwuiserv 2> /dev/null`
if [ "$nwuibase" == "package lgtonwui is not installed" ]; then
    base=$nwuiservbase
else
    base=$nwuibase
fi

MONITORING_JAR="monitoring-app.jar"
MONITORING_JAR_PATH="${base}/monitoring/app/${MONITORING_JAR}"
MONITORING_APP_ENV="app_env.sh"
MONITORING_USER="nsrnwui"
MONITORING_BASE="/nsr/nwui/monitoring"
MONITORING_LOGS_DIR="${MONITORING_BASE}/app/logs"
MONITORING_BIN_DIR="${MONITORING_BASE}/app/bin"
MONITORING_CONF_DIR="${MONITORING_BASE}/app/conf"
MONITORING_PROPERTIES_FILE="monitoring-app.properties"
MONITORING_LOG4J2_CONFIG_FILE="log4j2.xml"

if [ ! -f "${MONITORING_JAR_PATH}" ]; then
    base=/opt/nwui
    if [ ! -f "${MONITORING_JAR_PATH}" ]; then
	echo 1>&2 "ERROR: Unable to get nwui install directory."
	exit 1
    fi
fi

if [ -f "${MONITORING_CONF_DIR}/${MONITORING_APP_ENV}" ]
then
    . "${MONITORING_CONF_DIR}/${MONITORING_APP_ENV}"
else
    echo >&2 "Unable to source environment file '${MONITORING_CONF_DIR}/${MONITORING_APP_ENV}'."
fi

start_monitoring()
{
pgrep -U ${MONITORING_USER} -f ${MONITORING_JAR} >/dev/null 2>&1
if [ $? -eq 0 ]; then
    pid=`pgrep -U ${MONITORING_USER} -f ${MONITORING_JAR}`
    echo 1>&2 "Could not start the NWUI Monitoring application because another instance is still running ($pid)."
    exit 1
fi
echo >&2 "Starting NWUI Monitoring Application... "
cd $MONITORING_BIN_DIR && 
exec ${JAVA_HOME}/bin/java \
	-Dlogsdir="${MONITORING_LOGS_DIR}" \
	-Dpropertiesfile="${MONITORING_CONF_DIR}/${MONITORING_PROPERTIES_FILE}" \
	-Dlogging.config="${MONITORING_CONF_DIR}/${MONITORING_LOG4J2_CONFIG_FILE}" \
	-jar ${MONITORING_JAR} \
	--monitor.messaging.networker.server=${MANAGED_NW_SERVER}
retval=$?
return $retval
}

stop_monitoring()
{
echo >&2 "Stopping NWUI Monitoring Application... "
pgrep -U ${MONITORING_USER} -f ${MONITORING_JAR} >/dev/null 2>&1
if [ $? -eq 0 ]; then
    pid=`pgrep -U ${MONITORING_USER} -f ${MONITORING_JAR}`
    kill $pid >/dev/null 2>&1
    retval=$?
    if [ $retval -eq 0 ]; then
         echo >&2 "NWUI Monitoring application stopped successfully"
    else
	 echo >&2 "ERROR: NWUI Monitoring application stopped failed"
    fi
else
    echo >&2 "NWUI Monitoring application not running. Nothing to stop"
    retval=0
fi
return $retval
}

#
# main
#
retval=0
case $1 in
    start)
        start_monitoring
        retval=$?
        ;;
    stop)
        stop_monitoring
        retval=$?
        ;;
    *)
    echo "usage: `basename $0` {start|stop}"
    retval=1
    ;;

esac

exit $retval

