#!/bin/ksh
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2017,2019,2021.  All rights reserved.  
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                      
#                                                                              
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61pkgha_r720 src/packages/cluster/es/server/rte/root/cluster.es.server.rte.unconfig.sh 1.31.5.5 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1997,2015 
# 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/server/rte/root/cluster.es.server.rte.unconfig.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
###############################################################################
#
#  Name:  onintr
#
#  Rudimentary signal handler
#
#  Arguments:    none
#  Returns:      5 - exit with error
#  Environment:  interrupt handler
#
###############################################################################
onintr()
{
    echo "$0: Caught signal - exiting"
    exit 5
}

###############################################################################
#
#  Name:  if_failed
#
#  Common exit method
#
#  Arguments:    status - return code from some operation
#                errmsg - error message to display (if operation failed)
#
#  Returns:      if status is non 0 exit with error
#  Environment:  installp
#
###############################################################################
if_failed()
{
    status=$1
    errmsg=$2
    if [ $status -ne 0 ]
    then
        echo "Failed $errmsg"
        exit $status
    fi
}

###############################################################################
#
# Main
#
###############################################################################

#
# Setup a rudimentary PATH
#
PATH=/bin:/usr/bin:/usr/sbin:$PATH

#
# Establish a signal handler
#
trap onintr 1 2 3 15

SAVEODMDIR=${ODMDIR}

LPPNAME="PowerHA SystemMirror"
BFF="cluster.es.server"
LPPDIR="/usr/lpp/${BFF}"
typeset syslog_type=$(ODMDIR=/etc/objrepos odmget  -q "subsysname = 'syslogd'"  SRCsubsys | grep -w path | cut -d'=' -f2 | sed s/\"//g)
if [[ ${syslog_type} == " /usr/sbin/rsyslogd" ]]
then
    SYSLOG_CONF="/etc/rsyslog.conf"
else
    # setting default configuration file to syslog.conf
    SYSLOG_CONF="/etc/syslog.conf"
fi

############### Begin removal of HACMP entries in enabled syslog.conf ##############
# Check for previously added info in enabled syslog.conf, and remove;
# This first sed removes the "old" style entry, which should not exist
# at the time this script runs, but it does not hurt to leave the check
# in place. This can be removed in a later release.
if sed -e '/HACMP\/ES\ for\ AIX/{N
    d
    }' ${SYSLOG_CONF} > /tmp/$$tf1
then
        cp /tmp/$$tf1 ${SYSLOG_CONF}
        if_failed $? "removing ${LPPNAME} references from ${SYSLOG_CONF}"
        rm -f /tmp/$$tf1
fi
if sed -e '/PowerHA SystemMirror/{N
    d
    }' ${SYSLOG_CONF} > /tmp/$$tf1
then
        cp /tmp/$$tf1 ${SYSLOG_CONF}
        if_failed $? "removing ${LPPNAME} references from ${SYSLOG_CONF}"
        rm -f /tmp/$$tf1
fi
###############  End removal of HACMP entries in enabled syslog.conf  ##############

########## Begin removal of all services and inetd entries  ##################
# Most of these entries should not even exist at the time this script runs, 
# so ignore the return code
chservices -d -v cllockd -p udp -n 6100 > /dev/null 2>&1
chservices -d -v clsmuxpd -p tcp -n 6270 > /dev/null 2>&1
chservices -d -v clm_lkm -p tcp -n 6150 > /dev/null 2>&1
chservices -d -v clm_smux -p tcp -n 6175 > /dev/null 2>&1
chservices -d -v clm_mig_lkm -p tcp -n 6151 > /dev/null 2>&1

##############  End removal of services and inetd entries   ##################

#
# Remove previously added entries to the snmp configuration files
#
for file in snmpd.conf snmpd.peers snmpdv3.conf
do
        if [ -f /etc/$file ]
        then
            if grep "clsmuxpd_password" /etc/$file > /dev/null 2>&1
            then
                if grep -v "clsmuxpd_password" /etc/$file > /tmp/$$tf1
                then
                    cp /tmp/$$tf1 /etc/$file
                    if_failed $? "removing ${LPPNAME} entries in /etc/$file file."
                    rm -f /tmp/$$tf1
                fi
            fi
        fi
done

################## Begin removal of old tcp.clean entries  ###################
#
# Note that these entries are not made anymore, so this attempt at removal 
# is extraneous, but should cause no harm
# 
ed - /etc/tcp.clean << EOF >/dev/null 2>&1
g/HACMP\/ES\ for\ AIX/d
g/PowerHA SystemMirror/d
w
q
EOF
##################  End removal of old tcp.clean entries   ###################

################## Begin removal of shutdown_notify method  ###################
odmdelete -o PdAt -q "uniquetype=HACMP and attribute=shutdown_notify" >/dev/null 2>&1
##################  End removal of shutdown_notify method   ###################

################## Begin removal of old HACMP subsystems   ###################
# Remove any subsystem stuff first. Port to null.
# stop clstrmgrES and clevmgrdES if running
stopsrc -s clstrmgrES > /dev/null 2>&1
rmssys -s clstrmgrES > /dev/null 2>&1

stopsrc -s clevmgrdES > /dev/null 2>&1
rmssys -s clevmgrdES > /dev/null 2>&1
##################  End removal of old HACMP subsystems    ###################

######################  Begin /etc/rc.net modifications  ####################
#### This will remove IPAT conditions from /etc/rc.net.                    ##
# Note that these entries are not made anymore, so this attempt at removal 
# is extraneous, but should cause no harm
if egrep -q "HACMP" /etc/rc.net 2>&1
then
        if egrep -v "HACMP" /etc/rc.net > /tmp/$$tf1
        then
            cp /tmp/$$tf1 /etc/rc.net
            if [ $? -ne 0 ]
            then
                echo "Unable to modify rc.net to remove ${LPPNAME} entry."
            fi
            [[ -f /tmp/$$tf1 ]] && rm /tmp/$$tf1 2>/dev/null
        fi
fi

######################  End /etc/rc.net modifications  ####################

##################### Begin old crontab information removal ##################
CRONTMP=/var/tmp/crontmp$$
CRONFILE=/var/spool/cron/crontabs/root
# First remove clcycle entry if it already exists.
/bin/ed - $CRONFILE << EOF >/dev/null 2>&1
g/clcycle/d
g/cllvm/d
w
q
EOF
if_failed $? "removing ${LPPNAME} information from crontab file"

#####################  End old crontab information removal  ##################

######################   Begin Refresh subsystem Daemons #######################
if [ -z "$INUCLIENTS" -a -z "$INUBOSTYPE" ]
then
    SUB=inetd
    LC_ALL=C lssrc -s $SUB | grep $SUB | grep active > /dev/null 2>&1
    if [ $? -eq 0 ]
    then
      refresh -s $SUB >/dev/null 2>&1
      if_failed $? "refreshing source subsystem $SUB"
    fi
    # snmpd does not support refresh, so bounce it
    SUB=snmpd
    LC_ALL=C lssrc -s $SUB | grep active > /dev/null 2>&1
    if [ $? -eq 0 ]
    then
      stopsrc -s $SUB >/dev/null 2>&1
      if_failed $? "stopping subsystem $SUB"
      startsrc -s $SUB >/dev/null 2>&1
      if_failed $? "restarting subsystem $SUB"
    fi
fi
######################   End Refresh subsystem Daemons #########################

############ Begin Removing OLD HACMP ODM object classes ##########
ODMDIR="/etc/es/objrepos"
if [[ -d ${ODMDIR} && -x ${ODMDIR} ]]
then
   cd $ODMDIR

   LIST=$(ls -d HACMP*OLD*  2>/dev/null)
   if [ -n "$LIST" ]    ## there must be a list to go on
   then
       for odmclass in $LIST
       do
         rm $odmclass
         if_failed $? "removing extraneous file $odmclass"
       done
   fi
fi
############  End Removing OLD HACMP ODM object classes ##########

############ Begin Removing Links ##########
cd /usr/sbin/cluster
find . -type l | xargs rm -f

cd /etc/objrepos
find . -name "HACMP*" -type l | xargs rm -f

#  Remove /etc/cluster links
rm -f /etc/cluster/hacmp.clean
rm -f /etc/cluster/hacmp.term

if [[ -d /etc/es/objrepos && -x /etc/es/objrepos ]]
then
   # nuke these no matter what
   cd /etc/es/objrepos
   find . -type l | xargs rm -f
fi
############  End Removing Links ##########

############  Start remove HAES ODMs   ##########
for HAES_UNODMADD in `ls ${LPPDIR}/inst_root/*.unodmadd.haes`
do
   ODMDIR=/etc/es/objrepos ${HAES_UNODMADD}
   if_failed $? "running ${HAES_UNODMADD}"
   rm -f ${HAES_UNODMADD}
done
############  End remove HAES ODMs   ##########

########## Begin removing default node priority policies ##########
ODMDIR=/etc/es/objrepos odmdelete -o HACMPnpp -q "name = cl_highest_free_mem" >/dev/null 2>&1
ODMDIR=/etc/es/objrepos odmdelete -o HACMPnpp -q "name = cl_lowest_disk_busy" >/dev/null 2>&1
ODMDIR=/etc/es/objrepos odmdelete -o HACMPnpp -q "name = cl_highest_idle_cpu" >/dev/null 2>&1
ODMDIR=/etc/es/objrepos odmdelete -o HACMPnpp -q "name = cl_highest_udscript_rc" >/dev/null 2>&1
ODMDIR=/etc/es/objrepos odmdelete -o HACMPnpp -q "name = cl_lowest_nonzero_udscript_rc" >/dev/null 2>&1

########## End removing default node priority policies ##########

########## Begin removing errnotify stanzas added at runtime ####

ODMDIR=/etc/objrepos odmdelete -q"en_name LIKE clSF*" -o errnotify > /dev/null 2>&1
if_failed $? "removing errnoty stanzas from /etc/objrepos/errnotify"

########## End removing errnotify stanzas added at runtime ####

export ODMDIR="${SAVEODMDIR}"


######## RMC CDB handling #####################################
## But ignore the exit code
/usr/sbin/rsct/install/bin/ctinstrm -t D -f cluster.es.server.rte -s IBM.HacmpRgRm -R >/dev/null 2>&1

################################################################

########## Begin unregistrations for AIX Live Kernel Update (LKU) support ##########

#
# Unregister PowerHA script for AIX Live Kernel Update (LKU)
#
RegScript_CMD=/usr/sbin/lvupdateRegScript
typeset -i comp_flag=0
typeset -i V R M F
lslpp -lcq "bos.rte.commands" | head -1 | cut -f3 -d':' | IFS=. read V R M F
#supporting fileset bos.rte.commands version 7.2.2 and above
if (( $V > 7 )) || (( $V == 7 && (( $R > 2 || (( $R == 2 && $M >= 2 )) )) ));then
    comp_flag=1
fi


if [[ -f $RegScript_CMD ]]
then
    $RegScript_CMD -r -n PowerHA_ORIG_CHECK -P LVUP_CHECK -p 1 -d orig >/dev/null 2>&1
    if_failed $? "unregistering script for AIX Live Update (PowerHA_ORIG_CHECK)"
    $RegScript_CMD -r -n PowerHA_ORIG_PRE -P LVUP_PRE -p 1 -d orig >/dev/null 2>&1
    if_failed $? "unregistering script for AIX Live Update (PowerHA_ORIG_PRE)"
    $RegScript_CMD -r -n PowerHA_ORIG_ERROR -P LVUP_ERROR -p 1 -d orig >/dev/null 2>&1
    if_failed $? "unregistering script for AIX Live Update (PowerHA_ORIG_ERROR)"
    $RegScript_CMD -r -n PowerHA_SURR_CHECK -P LVUP_CHECK -p 1 -d surr >/dev/null 2>&1
    if_failed $? "unregistering script for AIX Live Update (PowerHA_SURR_CHECK)"
    $RegScript_CMD -r -n PowerHA_SURR_POST -P LVUP_POST -p 1 -d surr >/dev/null 2>&1
    if_failed $? "unregistering script for AIX Live Update (PowerHA_SURR_POST)"
    if (( comp_flag )); then
        $RegScript_CMD -r -n PowerHA_SURR_COMPLETE -P LVUP_COMPLETE -p 1 -d surr >/dev/null 2>&1
        if_failed $? "unregistering script for AIX Live Update (PowerHA_SURR_COMPLETE)"
    fi
    $RegScript_CMD -r -n PowerHA_SURR_ERROR -P LVUP_ERROR -p 1 -d surr >/dev/null 2>&1
    if_failed $? "unregistering script for AIX Live Update (PowerHA_SURR_ERROR)"
fi

#
# Unregister PowerHA kernel extensions for AIX Live Kernel Update (LKU)
#
phakernmgr_KE=/usr/lib/drivers/phakernmgr
SafeKE_CMD=/usr/sbin/lvupdateSafeKE

if [[ -x $SafeKE_CMD ]]
then
    # Unregister the phakernmgr kernel extension as LKU Safe
    $SafeKE_CMD -l | grep -q "kext = ${phakernmgr_KE}$"
    if [[ $? == 0 ]]
    then
        $SafeKE_CMD -r ${phakernmgr_KE} >/dev/null 2>&1
        if_failed $? "unregistering phakernmgr kernel extension as safe for AIX Live Update"
    fi
fi

########## End unregistrations for AIX Live Kernel Update (LKU) support ##########

########## Begin unregistrations for automated Live Partition Mobility (LPM) support ##########

#
# Un-register DR scripts for Live Partition Mobility (LPM)
#
/usr/sbin/drmgr -u cl_dr >/dev/null 2>&1
if_failed $? "unregistering cl_dr script for Live Partition Mobility"
/usr/sbin/drmgr -u cl_2dr >/dev/null 2>&1
if_failed $? "unregistering cl_2dr script for Live Partition Mobility"

########## End unregistrations for automated Live Partition Mobility (LPM) support ##########

exit 0
