#!/bin/ksh
#  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
#
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/events/join_interface.sh 1.13 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2002,2011 
# 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/events/join_interface.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
#########################################################################
#
#   COMPONENT_NAME: EVENTS
#
#   FUNCTIONS: none
#
#########################################################################


#########################################################################
#                                                                       #
#       Name:           join_adapter                                    #
#                                                                       #
#	Description:	This event script is called when an		#
#			adapter comes up.                               #
#									#
#       Called by:      cluster manager                                 #
#                                                                       #
#       Calls to:       none                           			#
#                                                                       #
#       Arguments:      nodename                                 	#
#			address (of the adapter)         		#
#                                                                       #
#       Returns:        0       success                                 #
#                       1       failure                                 #
#                       2       bad argument                            #
#                                                                       #
#########################################################################

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

set -a
eval $(cllsparam -n $LOCALNODENAME)
set +a

if [[ $VERBOSE_LOGGING == "high" ]]; then
    set -x
    version='%I%'
fi

OP_SEP="$(cl_get_path -S)"

if [ $# -ne 2 ]
then
        cl_echo 8063 "Usage: $PROGNAME nodename address\n" $PROGNAME
        exit 2
fi

NODENAME=$1
ADDR=$2
BOOTIF=""
ADDR_FAMILY=""
PREFIX_LEN=""

set -u
if [[ "$NODENAME" = "$LOCALNODENAME" ]]
then
    # If boot joining and alias network ifconfig boot so route 
    # which was deleted in cl_swap_IP_address will be restored.
    BOOTIF=$(cllsif -J "$OP_SEP" -Si $NODENAME | \
            awk -F"$OP_SEP" -v bootif=$ADDR '($2=="boot" && $7==bootif) \
            {printf("%s\n",$9)}')
    if [[ -n $BOOTIF ]]
    then
        cllsif -J "$OP_SEP" -Sn $ADDR | cut -d"$OP_SEP" -f12 | \
            tr "$OP_SEP" " " | read IF_ALIAS
        if [[ -n "$IF_ALIAS" ]]
        then
	    netstat -in
	    netstat -rnC
		ADDR_FAMILY=$(cllsif -J "$OP_SEP" -Sn $ADDR | cut -d"$OP_SEP" -f15)
		case "$ADDR_FAMILY" in
			AF_INET)
				NETMASK=$(LC_ALL=C clgetif -n $ADDR)
				ifconfig $BOOTIF alias $ADDR netmask $NETMASK
				;;
			AF_INET6)
				PREFIX_LEN=$(LC_ALL=C clgetif -p $ADDR)
				ifconfig $BOOTIF inet6 $ADDR alias prefixlen $PREFIX_LEN
				;;
			*)
				dspmsg scripts.cat 9503 "\n$PROGNAME: ERROR: Invalid address \
family for IP address \"$ADDR\".\n" $PROGNAME $ADDR
				exit 1
				;;
		esac
		
	    netstat -in
	    netstat -rnC
       fi
    fi
fi


MSG=$(dspmsg scripts.cat 8064 "Interface $ADDR is now available on node $NODENAME.\n" $ADDR $NODENAME)
echo $MSG >/dev/console

exit 0

