#!/usr/bin/ksh
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2017,2018,2021.  All rights reserved.  
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                      
#                                                                              
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61pkgha_r721 src/packages/cluster/es/ui/agent/root/cluster.es.smui.agent.unconfig.sh 1.3 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2016 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG
# @(#)  7d4c34b packages/cluster/es/ui/agent/root/cluster.es.smui.agent.unconfig.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM

####################################################
#
# This script will perform the necessary prep work
# and cleanup for the SMUI agent's deinstallation.
#
####################################################

#
# Add tracing support through a variety of mechanisms, including the standard
# installp "INST_DEBUG" variable, and the SMIT trace flag (smitty -v). And for
# those times where we might just want to debug the GUI alone, and not see all
# the other information that those methods provide, we support our own variable,
# SMUI_TRACE (which can be set to any non-null value).
#
if [[ -n $SMUI_TRACE                     || \
      $_SMIT_TRACE_FLAG == 0             || \
      $SM_INST_DEBUG    == @(Y|y|DEBUG)* || \
      $INST_DEBUG       == @(Y|y|DEBUG)* ]]
then
    typeset TAB=$(printf '\t')
    typeset PROGNAME="${0##*/}(root-part)"
    export PS4="$PROGNAME:"'main()[$LINENO]($SECONDS'"):$TAB "
    set -x
fi

export PATH="/usr/bin:/usr/sbin:/sbin:$PATH"
export LC_ALL=C  # Required, per "The AIX LPP Owner's Guide"

#
: If the old subsytem exists, stop the agent then remove the subsystem
#
if lssrc -s uiagent >/dev/null 2>&1
then
    typeset AGENT_PID=$(ps -eo 'pid,args' | grep "node .*/index.js agent" | grep -vw phauiagent | grep -vw grep | cut -f1 -d' ')
    if [[ $AGENT_PID == +([0-9]) ]]
    then
        print "    The GUI agent is currently active. Shutting it down  prior to the uninstall..."
        if [[ $_SMIT_VERBOSE_FLAG == 0 ]]
        then
            print -n "    "
            $(ps -eo 'pid,args' | grep "node .*/index.js agent" | grep -vw phauiagent | grep -vw grep)
            print "    stopsrc -cs uiagent"
        fi
        stopsrc -cs uiagent
        TRIES=11
        while (( $TRIES > 0 ))
        do
            sleep 1
            AGENT_PID=$(ps -eo 'pid,args' | grep "node .*/index.js agent" | grep -vw phauiagent | grep -vw grep | cut -f1 -d' ')
            [[ $AGENT_PID != +([0-9]) ]] && break
            (( TRIES = $TRIES - 1 ))
        done

        if [[ $AGENT_PID == +([0-9]) ]]
        then
            print "    The GUI agent is still active. Killing it prior to the uninstall..."
            if [[ $_SMIT_VERBOSE_FLAG == 0 ]]
            then
                print -n "    "
                $(ps -eo 'pid,args' | grep "node .*/index.js agent" | grep -vw phauiagent | grep -vw grep)
                print "    kill -9 $AGENT_PID"
            fi
            kill -9 $AGENT_PID
        fi
    fi

    print "Removing the \"uiagent\" subsystem"
    rmssys -s uiagent >/dev/null 2>&1
fi

if lsitab uiagent >/dev/null 2>&1
then
    print "Removing the \"uiagent\" inittab entry"
    rmitab "uiagent" >/dev/null 2>&1
fi

#
: If the new subsytem exists, stop the agent then remove the subsystem
#
if lssrc -s phauiagent >/dev/null 2>&1
then
    typeset AGENT_PID=$(ps -eo 'pid,args' | grep "node .*/index.js agent phauiagent" | grep -vw grep | cut -f1 -d' ')
    if [[ $AGENT_PID == +([0-9]) ]]
    then
        print "    The GUI agent is currently active. Shutting it down  prior to the uninstall..."
        if [[ $_SMIT_VERBOSE_FLAG == 0 ]]
        then
            print -n "    "
            $(ps -eo 'pid,args' | grep "node .*/index.js agent phauiagent" | grep -vw grep)
            print "    stopsrc -cs phauiagent"
        fi
        stopsrc -cs phauiagent
        TRIES=11
        while (( $TRIES > 0 ))
        do
            sleep 1
            AGENT_PID=$(ps -eo 'pid,args' | grep "node .*/index.js agent phauiagent" | grep -vw grep | cut -f1 -d' ')
            [[ $AGENT_PID != +([0-9]) ]] && break
            (( TRIES = $TRIES - 1 ))
        done

        if [[ $AGENT_PID == +([0-9]) ]]
        then
            print "    The GUI agent is still active. Killing it prior to the uninstall..."
            if [[ $_SMIT_VERBOSE_FLAG == 0 ]]
            then
                print -n "    "
                $(ps -eo 'pid,args' | grep "node .*/index.js agent phauiagent" | grep -vw grep)
                print "    kill -9 $AGENT_PID"
            fi
            kill -9 $AGENT_PID
        fi
    fi

    print "Removing the \"phauiagent\" subsystem"
    rmssys -s phauiagent >/dev/null 2>&1
fi

if lsitab phauiagent >/dev/null 2>&1
then
    print "Removing the \"phauiagent\" inittab entry"
    rmitab "phauiagent" >/dev/null 2>&1
fi

exit 0
