#! /bin/sh
# $Id$ Copyright (c) 2018-2024 EMC Corporation
#

#
# Copyright (c) 2018-2024 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.

# chkconfig: 35 94 03
# description: NMC Web UI

### BEGIN INIT INFO
# Provides:       nwui
# Required-Start: syslog network
# Required-Stop:
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description:    nwui
### END INIT INFO

ARCH=`uname`
DISTRO=`grep PRETTY_NAME /etc/os-release | cut -d'"' -f2 | cut -d' ' -f1 2> /dev/null`
if [ "$ARCH" = "Linux" ]; then
    if [ "$DISTRO" = "Ubuntu" ]; then
        MCUI_HOME=/opt/nwui
    else
        MCUI_HOME=`rpm -q --qf "%{INSTPREFIXES}\n" lgtonwui 2>/dev/null`
    fi
fi
nwuictld="${MCUI_HOME}/bin/nwuictld"
code=0
stop_code=0
arg0=`basename $0`

if [ "`whoami`" != "root" ]; then

    echo "$arg0: You must be root to execute this command."
    exit 1
fi

getpid()
{
	if [ -x /usr/bin/ps ] ; then
		/usr/bin/ps -ef | grep $1 | grep -v grep | tr -s " " | cut -d' ' -f2 | head -n 1
	else
		/bin/ps -ef | grep $1 | grep -v grep | tr -s " " | cut -d' ' -f2 | head -n 1
	fi
}

start_service()
{
        (echo 'starting the daemon "nwuictld":') > /dev/console
        if [ -f ${MCUI_HOME}/bin/nwuirc ]; then
                . ${MCUI_HOME}/bin/nwuirc || exit 1
        fi

        if [ -f ${MCUI_HOME}/bin/nwuictld ]; then
                "${MCUI_HOME}/bin/nwuictld" 2>&1 | /usr/bin/tee /dev/console
		code=$?
		echo "done."
                (echo ' nwuictld') > /dev/console
	else
            code=253
            echo "ERROR: $nwuictld not found!" >&2
        fi

        if [ -d /var/lock/subsys ]; then
                touch /var/lock/subsys/nwuictld
        fi
}

# Get PIDs of some important daemons & services
tomcat_pid=`getpid tomcat`
nwuictld_pid=`getpid nwuictld`

case "$1" in
    start)
	echo "Starting daemon, nwuictld"
	nwuictld_pid=`getpid nwuictld`
	if [ -n "$nwuictld_pid" ]; then
            echo "ERROR: MCUI Controller daemon is already running!" >&2
            exit 1;
        fi

	start_service
        ;;
    stop)
	EXITCODE=0
	echo "Stopping nwuictld: "
	if [ -n "$nwuictld_pid" ] ; then
	    kill -TERM $nwuictld_pid
	    if [ $? -ne 0 ]; then
		echo "ERROR: Could not kill -TERM $nwuictld_pid" >&2
		exit 1
	    fi

	    if [ -n "$tomcat_pid" ] ; then
		kill -TERM $tomcat_pid
		if [ $? -ne 0 ]; then
		    echo "ERROR: Could not kill -TERM $tomcat_pid" >&2
		    exit 1
		fi
	    fi

	    # wait a little bit for tomcat service to stop naturally
	    loop_delay=4
	    loop_count=5
	    loop_index=0

	    while [ -n "$tomcat_pid" ]; do
		sleep $loop_delay;
		loop_index=`expr $loop_index + 1`
		if [ $loop_index -ge $loop_count ]; then
		    break;
		fi
		tomcat_pid=`getpid tomcat`
	    done;
	    echo ""

	    # If tomcat isn't down yet, print out warning and kill it.
	    tomcat_pid=`getpid tomcat`
	    if [ -n "$tomcat_pid" ] ; then
		echo "WARNING: using kill -KILL to stop tomcat" >&2
		kill -KILL $tomcat_pid
	    fi
	    echo "done."
	    code=0
	else
	    code=254
	    echo "WARNING: nwuictld is not running!" >&2
	fi
	;;
    restart)
	echo "Restarting nwuictld: "
	$0 stop
	stop_code=$?
	if [ $stop_code -ne 1 ]; then
		$0 start
	else
		echo "Not restarting nwuictld:  nwuictld stop error"
	fi
	;;
    status)
	nwuictld_pid=`getpid nwuictld`
	if [ -n "$nwuictld_pid" ] ; then
		echo "nwuictld is running" >&2
	else
		echo "nwuictld is not running" >&2
	fi
	;;
    *)
	echo "usage: $arg0 { start | stop | restart | status }" >&2
	code=255
	;;
esac

exit $code
