#!/bin/ksh93
#  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_standby.sh 1.8.3.1 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1990,2013 
# 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_standby.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM

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

#########################################################################
#
#   COMPONENT_NAME: EVENTS
#
#   FUNCTIONS: addback_route
#
#########################################################################


###############################################################################
#
# Name: addback_route
#
#       When two or more standbys are on the same subnet, only one of the
#       standbys is in the routing table as the route. If this standby is
#       used to takeover the remote address, the route also gets destroyed
#       in routing table. This routine is used to restore the route for
#       the remaining standbys on the subnet.
#
# Arguments: standby_IP_address
#
# Returns: None
#
###############################################################################
addback_route() {
    typeset PS4_FUNC="addback_route"
    [[ $VERBOSE_LOGGING == "high" ]] && set -x
    NETWORK=$(cllsif -cSn $1 | cut -d':' -f3 | uniq)

    standby=$1
    ADDR_FAMILY=""
    PREFIX_LEN=""

    ADDR_FAMILY=$(cllsif -J "$OP_SEP" -Sn $standby | cut -d"$OP_SEP" -f15)

    #
    # Make sure the standby is defined on local node
    #
    case "$ADDR_FAMILY" in
        AF_INET)
            clgetif -n $standby >/dev/null 2>&1
        ;;
        AF_INET6)
            clgetif -p $standby >/dev/null 2>&1
        ;;
        *)
            dspmsg scripts.cat 9503 "\n$PROGNAME: ERROR: Invalid address \
family for IP address \"$standby\".\n" $PROGNAME $standby
            exit 1
        ;;
    esac

    if (( $? != 0 ))
    then
        return
    fi

    if [[ "$ADDR_FAMILY" == "AF_INET" ]]; then
        NETMASK=$(LC_ALL=C clgetif -n $standby)
    elif [[ "$ADDR_FAMILY" == "AF_INET6" ]]; then
        PREFIX_LEN=$(LC_ALL=C clgetif -p $standby)
    fi

    INTERFACE=$(LC_ALL=C clgetif -a $standby)

    #
    # Make sure the standby is up on local node
    #
    addr=i"$standby"_"${LOCALNODENAME//-/$HA_DASH_CHAR}"
    if [[ "$ADDR_FAMILY" == "AF_INET" ]]; then
        addr=${addr//\./x}
    elif [[ "$ADDR_FAMILY" == "AF_INET6" ]]; then
        addr=${addr//:/y}
    fi

    VAR=\$"$addr"
    set +u
    VAL="$(eval echo $VAR)"
    set -u

    if [ "$VAL" != "UP" ]
    then
        return
    fi

    # ifconfig should re-establish the route to the standby
    # subnet (if it was lost)

    if [[ "$ADDR_FAMILY" == "AF_INET" ]]; then
        ifconfig $INTERFACE $standby netmask $NETMASK up
    elif [[ "$ADDR_FAMILY" == "AF_INET6" ]]; then
        ifconfig $INTERFACE inet6 $standby prefixlen $PREFIX_LEN up
    fi
} # End of "addback_route()"


#########################################################################
#                                                                       #
#    Name:           join_standby                                       #
#                                                                       #
#    Description:    This event script is called when a previously      #
#                    failed standby adapter is made available.          #
#                                                                       #
#    Called by:      cluster manager                                    #
#                                                                       #
#    Calls to:       None                                               #
#                                                                       #
#    Arguments:      nodename (with the joining standby adapter)        #
#                    address (of the joining standby adapter)           #
#                                                                       #
#    Returns:        0       success                                    #
#                    1       failure                                    #
#                    2       bad argument                               #
#                                                                       #
#########################################################################

typeset PROGNAME=${0##*/}
export PATH="$(/usr/es/sbin/cluster/utilities/cl_get_path all)"
if [[ $VERBOSE_LOGGING == "high" ]]; then
    set -x
    version='%I%'
fi

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

OP_SEP="$(cl_get_path -S)"

NODENAME=$1
ADDR=$2

if (( $# != 2 ))
then
    cl_echo 1032 "Usage: $PROGNAME nodename joining_standby_address\n" $PROGNAME
    exit 2
fi

set -u

MSG=$(dspmsg scripts.cat 330 "Boot communication interface $ADDR is now available.\n" $ADDR)
echo $MSG >/dev/console

if [[ $NODENAME == $LOCALNODENAME ]]
then
    addback_route $ADDR
fi

exit 0
