#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2020,2021.  All rights reserved.  
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                      
#                                                                              
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/lib/ksh93/hacmp/KLIB_HACMP_delete_interface.sh 1.3 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2006,2012 
# 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 43haes/lib/ksh93/hacmp/KLIB_HACMP_delete_interface.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM

# 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_interface

=head1 SYNOPSIS

clmgr delete interface {<interface>[,<if#2>,<if#n>,...] | ALL}

=head1 DESCRIPTION

Deletes one or more interfaces from the cluster configuration.

=head1 ARGUMENTS

 1. interfaces [REQUIRED] [string]
    One or more (comma or space separated) interfaces to
    attempt to delete.

=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

=cut
} # End of POD-formatted documentation.


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

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

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

    #===================================
    : Declare and initialize variables
    #===================================
    typeset -i rc=$RC_UNKNOWN
    typeset -i i=0
    typeset interface=

    #================================================================
    : 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 [[ -z $interfaces ]]; then
        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 uctext=$interfaces
        if [[ $uctext == "ALL" ]]; then
            interfaces=
            typeset available
            CL=$LINENO KLIB_HACMP_list_interfaces available
            for (( i=0; i<${#available[*]}; i++ )); do
                interfaces="$interfaces ${available[$i]}"
            done

            if [[ $interfaces == *([[:space:]]) ]]; then
                dspmsg -s $CLVT_SET $CLVT_MSGS 325 "Warning: there are no interfaces to delete.\n"
                rc=$RC_SUCCESS
            fi
        fi
    fi

    #=============================================================
    : Delete the interface if no input errors have been detected
    #=============================================================
    if (( $rc == RC_UNKNOWN )); then
        for interface in $interfaces; do

            #==================================================================
            : If the interface is in IP address format, convert to its label.
            : clrmnode will not accept an interface in address format. ~sigh~
            #==================================================================
            if [[ $interface == *@(\.|:)* ]]; then
                interface=$(/usr/bin/odmget -q identifier=$interface HACMPadapter | /usr/bin/grep -w ip_label)
                interface=${interface#*\"}
                interface=${interface%\"}
            fi

            CL=$LINENO KLIB_HACMP_is_known_interface "$interface"
            if (( $? != RC_SUCCESS )); then
                dspmsg -s $CLVT_SET $CLVT_MSGS 102 "\nERROR: \"%1\$s\" does not appear to exist!\n\n" "$interface" 1>&2
                dspmsg -s $CLVT_SET $CLVT_MSGS 146 "Available Interfaces:\n\n" 1>&2

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

            else
                print -- "$0()[$LINENO]($SECONDS): clodmget \"$interface\"" >>$CLMGR_TMPLOG  # Always log commands
                clodmget -q "ip_label=$interface" -nf network,nodename,function,interfacename -d' ' HACMPadapter 2>/dev/null| read network node function intrface
                #=======================================================================
                # If a network holding a service IP with only one network interface
                # then exiting with error message.
                #========================================================================
                service_function=$(cllsif -pcS | grep -w "service:$network" | wc -l | sed 's/ //g')
                if (( $service_function != 0 )); then
                    boot_function_value=$(cllsif -pcS | grep -w "$function:$network" | wc -l | sed 's/ //g')
                    if (( $boot_function_value == 1 )); then
                        rc=$RC_ERROR
                        dspmsg -s $CLVT_SET $CLVT_MSGS 1461 "\nERROR: Modifying or removing last network interface '%1\$s' not allowed\n\
as the network '%2\$s' holds a service IP.\n\
You can remove the service IP from the network, then try the operation again.\n" $interface $network
                    fi
                fi
                if [[ -n $function && $function == "boot" ]]; then
                    print -- "$0()[$LINENO]($SECONDS): cllsif" >>$CLMGR_TMPLOG  # Always log commands
                    persistent=$(cllsif -J "~" -Spi $node | egrep -w "persistent.*$network" | awk -F"~" '{print $7}')
                    if [[ -n $persistent ]]; then
                        print -- "$0()[$LINENO]($SECONDS): cl_rsh -n $node netstat -in" >>$CLMGR_TMPLOG  # Always log commands
                        pintrface=$(cl_rsh -n $node netstat -in | grep -w $persistent | awk '{print $1}')
                        if [[ -n $pintrface && $pintrface == $intrface ]]; then
                            dspmsg -s $CLVT_SET $CLVT_MSGS 1423 "\nManually remove aliased persistent IP:%1\$s on interface:%2\$s on node:%3\$s before removing the interface.\n" $persistent $intrface $node 1>&2
                            rc=$RC_ERROR
                        fi
                    fi
                fi

                if (( $rc == RC_UNKNOWN || $rc == RC_SUCCESS )); then
                    print -- "$0()[$LINENO]($SECONDS): $HAUTILS/clrmnode -a \"$interface\"" >>$CLMGR_TMPLOG  # Always log commands
                    $HAUTILS/clrmnode -a "$interface"
                    rc=$?
                    if (( $rc != RC_SUCCESS )); then
                        rc=$RC_ERROR
                        dspmsg -s $CLVT_SET $CLVT_MSGS 300 "\nERROR: failed to delete \"%1\$s\".\n\n" "$interface" 1>&2
                        break
                    fi
                fi
            fi
        done

        (( $rc != RC_SUCCESS )) && print -u2
    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 interface -h" "INTERFACE:" "$CLVT_PROGNAME" 1>&2
    fi

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