#!/bin/ksh93
# @(#)  7d4c34b 43haes/lib/ksh93/hacmp/KLIB_HACMP_delete_hmc.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
#  ALTRAN_PROLOG_BEGIN_TAG
#  This is an automatically generated prolog.
#
#  Copyright (C) Altran ACT S.A.S. 2017,2021.  All rights reserved.
#
#  ALTRAN_PROLOG_END_TAG
#

#================================================
# The following, commented line enforces coding
# standards when this file is edited via vim.
#================================================
# vim:tabstop=4:shiftwidth=4:expandtab:smarttab
#================================================

# Start of POD-formatted documentation. Viewing suggestions:
#      perldoc <FILENAME>
#      pod2text -c <FILENAME>
#      pod2text -c --code <FILENAME>
#      pod2html <FILENAME>
function devDoc {
    : <<'=cut' >/dev/null 2>&1

=head1 NAME

 KLIB_HACMP_delete_hmc

=head1 VERSION

 Version Number:  1.4.1.8
 Last Extracted:  8/9/16 14:12:29
 Last Changed:    7/28/16 10:20:29

 Path, Component, Release(, Level):
 src/43haes/lib/ksh93/hacmp/KLIB_HACMP_delete_hmc.sh, hacmp.assist, 61haes_r721, 1631A_hacmp721

=head1 SYNOPSIS

 clmgr delete hmc {<HMC> | ALL}

=head1 DESCRIPTION

 Attempts to delete the specified HMC, from its name, or All HMCs.

=head1 ARGUMENTS

 1. properties [REQUIRED] [hash ref]
    An associative array within which data about the
    created object can be returned to the caller.

 2. HMC [REQUIRED] [string]
    Either hostname of IP address for the HMC.
    IPv4 and IPv6 format address are supported.
    All HMCs can be deleted if 'All' is specified.

=head1 RETURN

 0: no errors were detected; the operation appears to have been successful
 1: a general error has occurred
 2: a specified resource does not exist, or could not be found
 3: some required input was missing
 4: some detected input was incorrect in some way
 5: a required dependency does not exist
 6: a specified search failed to match any data

=head1 COPYRIGHT

COPYRIGHT International Business Machines Corp. 2005,2015
All Rights Reserved

=cut
} # End of POD-formatted documentation.


function KLIB_HACMP_delete_hmc {
    LINENO=2 . $HALIBROOT/log_entry "$0()" "$CL"
    : version=@(#)  7d4c34b 43haes/lib/ksh93/hacmp/KLIB_HACMP_delete_hmc.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
    : INPUTS: $*

    typeset hmcs=${1//\"}
    hmcs=${hmcs//,/ }

    [[ $CLMGR_LOGGING == 'med' ]] && set +x  # Only trace param values

    #===================================
    : Declare and initialize variables
    #===================================
    typeset -i rc=$RC_UNKNOWN
    typeset hmc=
    typeset -u uc_val=$hmcs

    #================================================================
    : Check for a defined cluster. No need to continue without one.
    #================================================================
    CL=$LINENO isClusterDefined
    if (( $? != RC_SUCCESS )); then
        rc=$RC_MISSING_DEPENDENCY

    #=================
    : Validate input
    #=================
    elif [[ $hmcs == *([[:space:]]) ]]; then
        CL=$LINENO dspmsg -s $CLVT_SET $CLVT_MSGS 100 "\nERROR: a name/label must be provided.\n\n" 1>&2
        rc=$RC_MISSING_INPUT
    else
        typeset -u ALL=$uc_val
        if [[ $ALL == "ALL" ]]; then
            hmcs=""
            typeset available
            CL=$LINENO KLIB_HACMP_list_hmcs available
            for (( i=0; i<${#available[*]}; i++ )); do
                        hmcs=${hmcs:+$hmcs }${available[$i]}
            done
            # If no HMC to delete, consider it is ok
            if [[ -z $hmcs ]]; then
                rc=$RC_SUCCESS
            fi
        else
            typeset HMCS=""
            for hmc in $hmcs; do
                CL=$LINENO KLIB_HACMP_is_known_hmc hmc >/dev/null
                if (( $? != RC_SUCCESS )); then
                    dspmsg -s $CLMGR_SET $CLMGR_MSGS 102 "\nERROR: \"%1\$s\" does not appear to exist!\n\n" "$hmc" 1>&2
                    dspmsg -s $CLMGR_SET $CLMGR_MSGS 810 "Available HMCs:\n\n" 1>&2

                    typeset available
                    CL=$LINENO KLIB_HACMP_list_hmcs available
                    for (( i=0; i<${#available[*]}; i++ )); do
                                if [[ ${available[$i]} != *([[:space:]]) ]]; then
                                    print -u2 "\t${available[$i]}"
                                fi
                    done
                    print -u2 ""
                    rc=$RC_NOT_FOUND
                else
                    HMCS="${HMCS:+$HMCS }$hmc"
                fi
            done
            hmcs=$HMCS
        fi
    fi

    if (( $rc == RC_UNKNOWN )) ; then
        for hmc in $hmcs; do
            print -- "$0()[$LINENO]($SECONDS): clrmhmc -n \"$hmc\"" >>$CLMGR_TMPLOG
            clrmhmc -n "$hmc"
            rc=$?
            print -- "$0()[$LINENO]($SECONDS): clrmhmc -n \"$hmc\" rc=$rc" >>$CLMGR_TMPLOG

            if (( $rc != RC_SUCCESS )); then
                rc=$RC_ERROR
                dspmsg -s $CLMGR_SET $CLMGR_MSGS 812 "\nERROR: failed to remove the \"%1\$s\" HMC.\n\n" "$hmc" 1>&2
                break
            else
                #===================================
                : Update all Nodes HMC_IP attributes
                #===================================
                typeset NODES_OF_HMC=
                print -- "$0()[$LINENO]($SECONDS): cllshmcparam -m $hmc -o" >>$CLMGR_TMPLOG  # Always log commands
                NODES_OF_HMC=$(cllshmcparam -m $hmc -o)
                rc=$rc
                print -- "$0()[$LINENO]($SECONDS): cllshmcparam -m $hmc -o rc=$rc" >>$CLMGR_TMPLOG  # Always log commands result
                if (( $rc == RC_SUCCESS )); then
                    if [[ -n $NODES_OF_HMC ]]; then
                        for NODE_OF_HMC in $NODES_OF_HMC; do
                            #===================================
                            : We search all nodes using this hmc
                            #===================================
                            print -- "$0()[$LINENO]($SECONDS): cllshmcparam -n $NODE_OF_HMC -o" >>$CLMGR_TMPLOG  # Always log commands
                            typeset UPDATED_HMCS=
                            typeset HMCS_OF_NODE=
                            HMCS_OF_NODE=$(cllshmcparam -n $NODE_OF_HMC -o)
                            rc=$?
                            print -- "$0()[$LINENO]($SECONDS): cllshmcparam -n $NODE_OF_HMC -o rc=$rc" >>$CLMGR_TMPLOG  # Always log commands result
                            if (( $rc == RC_SUCCESS)); then
                                for HMC_OF_NODE in $HMCS_OF_NODE; do
                                    #===================================
                                    : We search for all hmcs of the node, and we remove from the list the hmc being removed.
                                    #===================================
                                    if [[ $HMC_OF_NODE != $hmc ]]; then
                                        #=========================
                                        : If different, we keep it
                                        #=========================
                                        UPDATED_HMCS=${UPDATED_HMCS:+$UPDATED_HMCS,}$HMC_OF_NODE
                                    fi
                                done
                                if [[ -z $UPDATED_HMCS ]]; then
                                    UPDATED_HMCS=""
                                fi
                                print -- "$0()[$LINENO]($SECONDS): clchhmcparam -n $NODE_OF_HMC -c HMC_IP=\"$UPDATED_HMCS\"" >>$CLMGR_TMPLOG  # Always log commands
                                clchhmcparam -n "$NODE_OF_HMC" -c HMC_IP=$UPDATED_HMCS
                                rc=$?
                                print -- "clchhmcparam -n $NODE_OF_HMC -c HMC_IP=\"$UPDATED_HMCS\" rc=$rc" >>$CLMGR_TMPLOG  # Always log command result
                                if (( $rc != RC_SUCCESS )); then
                                    rc=$RC_ERROR
                                    dspmsg -s $CLMGR_SET $CLMGR_MSGS 812 "\nERROR: failed to remove the \"%1\$s\" HMC.\n\n" "$hmc" 1>&2
                                fi
                            fi
                        done
                    fi
                fi

                #======================================
                : Update all Site HMCS_LIST attributes
                #======================================
                print -- "cllssite -l" >>$CLMGR_TMPLOG  # Always log commands
                typeset SITENAMES=
                SITENAMES=$(cllssite -l)
                rc=$?
                print -- "cllssite -l rc=$rc" >>$CLMGR_TMPLOG  # Always log commands result
                if (( rc == RC_SUCCESS )); then
                    for SITENAME in $SITENAMES; do
                        typeset UPDATED_HMCS=
                        print -- "cllssite -S $SITENAME" >>$CLMGR_TMPLOG  # Always log commands
                        typeset HMCS_OF_SITE=
                        HMCS_OF_SITE=$(cllssite -S $SITENAME)
                        rc=$?
                        print -- "cllssite -S $SITENAME rc=$rc" >>$CLMGR_TMPLOG  # Always log commands result
                        for HMC_OF_SITE in $HMCS_OF_SITE; do
                            if [[ $HMC_OF_SITE != $hmc ]]; then
                                UPDATED_HMCS=${UPDATED_HMCS:+$UPDATED_HMCS,}$HMC_OF_SITE
                            fi
                        done
                        print -- "clchgsite -p $SITENAME -H \"$UPDATED_HMCS\" ">>$CLMGR_TMPLOG  # Always log commands
                        clchgsite -p $SITENAME -H "$UPDATED_HMCS"
                        rc=$?
                        print -- "clchgsite -p $SITENAME -H \"$UPDATED_HMCS\" rc=$rc" >> $CLMGR_TMPLOG  # Always log command result
                        if (( $rc != RC_SUCCESS )); then
                            rc=$RC_ERROR
                            dspmsg -s $CLMGR_SET $CLMGR_MSGS 812 "\nERROR: failed to remove the \"%1\$s\" HMC.\n\n" "$hmc" 1>&2
                        fi
                    done
                fi

                #==========================
                : Update HACMPhmcparam
                #==========================
                print -- "$0()[$LINENO]($SECONDS): clean HACMPhmcparam from $hmc" >>$CLMGR_TMPLOG
                #===============================
                : Get current default_hmcs_list
                #===============================
                typeset default_hmcs_list=
                print -- "clodmget -n -f default_hmcs_list HACMPhmcparam" >>$CLMGR_TMPLOG  # Always log commands
                default_hmcs_list=$(clodmget -n -f default_hmcs_list HACMPhmcparam)
                rc=$?
                print -- "clodmget -n -f default_hmcs_list HACMPhmcparam rc=$rc" >> $CLMGR_TMPLOG  # Always log command result
                print -- "default_hmcs_list=$default_hmcs_list" >> $CLMGR_TMPLOG  # Always log command result
                if (( $rc != RC_SUCCESS )); then
                    rc=$RC_ERROR
                    dspmsg -s $CLMGR_SET $CLMGR_MSGS 812 "\nERROR: failed to remove the \"%1\$s\" HMC.\n\n" "$hmc" 1>&2
                else
                    typeset NEW_DEFAULT_HMCS_LIST=""
                    for default_hmc in $default_hmcs_list; do
                        if [[ $default_hmc != $hmc ]]; then
                            NEW_DEFAULT_HMCS_LIST=${NEW_DEFAULT_HMCS_LIST:+$NEW_DEFAULT_HMCS_LIST }$default_hmc
                        fi
                    done
                    #===================================
                    : Persist default_hmcs_list=$NEW_DEFAULT_HMCS_LIST
                    #===================================
                    print -- "Updating HACMPhmcparam with default_hmcs_list=$NEW_DEFAULT_HMCS_LIST" >> $CLMGR_TMPLOG  # Always log command result
                    print "HACMPhmcparam:
default_hmcs_list=$NEW_DEFAULT_HMCS_LIST" | odmchange -o HACMPhmcparam
                    rc=$?
                    if (( $rc != RC_SUCCESS )); then
                        dspmsg -s $CLMGR_SET $CLMGR_MSGS 820 "\nERROR: failed to remove the \"%1\$s\" HMC from HACMPhmcparam.\n\n" "$hmc" 1>&2
                        rc=$RC_ERROR
                    else
                        typeset CHECK_VAL=$(clodmget -n -f default_hmcs_list HACMPhmcparam)

                        [[ $CHECK_VAL == $NEW_DEFAULT_HMCS_LIST ]] && rc=$RC_SUCCESS || rc=$RC_ERROR
                    fi
                fi
            fi
        done
    fi


    #=======================================================================
    : If a user input error was detected, provide some helpful suggestions
    #=======================================================================
    if (( $rc == RC_MISSING_INPUT || $rc == RC_INCORRECT_INPUT )) && \
        [[ $CLVT_GUI == *([[:space:]]) ]]; then
        CL=$LINENO cl_dspmsg -s $CLVT_SET $CLVT_MSGS 104 "For more information about available options and syntax, try\n\"$HAUTILS/clmgr %1\$s\". As an\nalternative, if the PowerHA SystemMirror man pages have been installed, invoke\n\"$HAUTILS/clmgr -hv\" (or \"/usr/bin/man clmgr\"),\nsearching for \"%2\$s\" in the displayed text.\n\n" \
            "delete hmc -h" "HMC:" "$CLVT_PROGNAME" 1>&2
    fi

    log_return_msg "$rc" "$0()" "$LINENO"
    return $?
} # End of "KLIB_HACMP_delete_hmc()"
