#!/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/usr/cluster.es.smui.agent.unconfig.sh 1.7 
#  
# 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/usr/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##*/}(usr-part)"
    export PS4="$PROGNAME:"'main()[$LINENO]($SECONDS'"):$TAB "
    set -x
fi

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

#
: Global Declarations
#
typeset SMUI_HOME="/usr/es/sbin/cluster/ui"
typeset fromfile="/usr/es/sbin/cluster/ui/server/node_modules/smui-common/bin/uisnap"
typeset tofile="/usr/es/sbin/cluster/ui/bin/uisnap"

####################################################
# If the server package is still installed, change
# the link so that uisnap points to the server copy.
####################################################
if lslpp -l cluster.es.smui.server >/dev/null 2>&1 && \
   [[ -s $fromfile ]]
then
    ln -sf $fromfile $tofile
    if (( $? != 0 )) || [[ ! -L $tofile ]]
    then
        print "Warning: unable to create a softlink to $fromfile in ${tofile%/*}."
    fi
elif [[ -e $tofile ]]
then
    rm -f $tofile
fi

#
: Remove the non-installed files that are inserted into the agent
: directory structure by the cluster discovery process, so that the
: agent deinstall will be clean and complete.
#
if ! lslpp -lc cluster.es.smui.server >/dev/null 2>&1
then
    rm -f $SMUI_HOME/security/agent.crt
    rm -f $SMUI_HOME/security/agent.key
    rm -f $SMUI_HOME/security/ca.crt
    rm -f $SMUI_HOME/lib/libgcc*
    rm -f $SMUI_HOME/lib/libstdc*
    rmdir $SMUI_HOME/lib 2>/dev/null  # Only works if all files are gone
fi
rm -f $SMUI_HOME/agent/configuration-agent.json
rm -f $SMUI_HOME/agent/logs/*

#
: If there are still files/directories for the agent, go ahead and remove them
#
if [[ -d /usr/es/sbin/cluster/ui/agent ]]
then
    print "    Removing all agent files..."
    if [[ $_SMIT_VERBOSE_FLAG == 0 ]]
    then
        print "    rm -rf /usr/es/sbin/cluster/ui/agent"
    fi
    rm -rf /usr/es/sbin/cluster/ui/agent
fi

#
: If none of the other GUI filesets are installed any longer,
: get rid of the whole installation directory.
#
if [[ -d /usr/es/sbin/cluster/ui ]] && \
   ! lslpp -lc cluster.es.smui.common >/dev/null 2>&1 && \
   ! lslpp -lc cluster.es.smui.server >/dev/null 2>&1
then
    print "    Removing the GUI installation directory..."
    if [[ $_SMIT_VERBOSE_FLAG == 0 ]]
    then
        print "    rm -rf /usr/es/sbin/cluster/ui"
    fi
    rm -rf /usr/es/sbin/cluster/ui
fi

exit 0
