#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2019,2021.  All rights reserved.  
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                      
#                                                                              
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r721 src/43haes/usr/sbin/cluster/utilities/get_local_nodename.sh 1.2.1.28 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1990,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 43haes/usr/sbin/cluster/utilities/get_local_nodename.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM

#########################################################################
#                                                                       #
#       Name:           get_local_nodename                              #
#                                                                       #
#       Arguments:      none                                            #
#                                                                       #
#       Returns:        local-nodename     success                      #
#                       NULL               failure                      #
#                                                                       #
#########################################################################

typeset PROGNAME=${0##*/}
export PATH="$(/usr/es/sbin/cluster/utilities/cl_get_path all)"
eval export $(cllsparam -x)

typeset OP_SEP="$(cl_get_path -S)"
typeset AIXODMDIR="/etc/objrepos"
typeset HAODMDIR="/etc/es/objrepos"
typeset addr="" addr_v6=""

#turn on tracing after initializations
if [[ $VERBOSE_LOGGING == "high" ]]; then
    set -x
    version='1.2.1.28'
fi

#
: cllsclstr -N will return the local node if not configured in HACMPcluster
#
export ODMDIR=$HAODMDIR
typeset nodename=""
nodename=$(cllsclstr -N)
typeset -i rc=$?
(( $rc != 0 )) && exit $rc

#
: If the node name in HACMPcluster matches a configured node, we are done.
#
if [[ -n $(clnodename | grep -w $nodename) ]]
then
    print -- "$nodename"
    exit 0
fi

#
: No match - nodename must have changed or must be a new cluster.
: Try to identify this node by matching an adapter in the config
: to one configured in CuAt or ifconfig on this system.
#
for nodename in $(clnodename)
do
    for addr in $(cllsif -J"$OP_SEP" -Si $nodename |\
                  grep ${OP_SEP}boot${OP_SEP}      |\
                  cut -d"$OP_SEP" -f7)
    do
        #
        : Look for address $addr as a local IPv4/6 address
        #
        if [[ -n $(ODMDIR=$AIXODMDIR clodmget -q"value = $addr AND attribute LIKE netaddr*" CuAt) ]]
        then
            print -- "$nodename"
            exit 0
        fi

        #
        : An IPv6 address may not show up in the CuAt ODM if it was
        : added by autoconf, so also check for the address, $addr,
        : in ifconfig.
        #
        for addr_v6 in $(ODMDIR=$AIXODMDIR LC_ALL=C ifconfig -a |\
                         grep -w inet6 | cut -d' ' -f2)
        do
            addr_v6=${addr_v6%%/*}  # Eliminate zone number
            if [[ $addr == $addr_v6 ]]; then
                print -- "$nodename"
                exit 0
            fi
        done
    done
done

#
: Local node name not found
#
print
exit 1
