#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/utilities/cl_hb_alias_network.sh 1.7 
#  
# 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 
# @(#)00      1.7  src/43haes/usr/sbin/cluster/utilities/cl_hb_alias_network.sh, hacmp.utils, 61haes_r714 11/28/11 15:08:32
###############################################################################
#
#   COMPONENT_NAME: EVENTUTILS
#
#   FUNCTIONS: 
#
###############################################################################

###############################################################################
#
#  Name:  cl_hb_alias_network
#
#  This script is used to manage all the alias addresses for heartbeat on
#  a particular network.
#  The parameters are:
# 	network name - name of the network
#
#	action - there are 2 basic actions:
#  		add - add the alias addresses to the adapters on this network
#  		delete - remove the alias addresses from this network
#
# Returns:      0 - success
#               2 - bad number of arguments
#               any non-0 return from cl_hb_alias
#
# Environment:  VERBOSE_LOGGING,PATH
#
###############################################################################

###############################################################################
#
# Main entry point
#
###############################################################################

PROGNAME=$(basename ${0})
export PATH="$($(dirname ${0})/cl_get_path all)"
[[ "$VERBOSE_LOGGING" = "high" ]] && set -x
[[ "$VERBOSE_LOGGING" = "high" ]] && version='1.7 $Source: 61haes_r711 43haes/usr/sbin/cluster/utilities/cl_hb_alias_network.sh 1$'
HA_DIR="$(cl_get_path)"
OP_SEP="$(cl_get_path -S)"

NETWORK=$1 		# this is the interface (adapter)
ACTION=$2 		# this is the action 

# check the inputs
if [[ $# != 2 ]]; then
    # bad arg count
    cl_echo 9107 "Usage : %s <network name> <add|delete|force>\n" $PROGNAME
    exit 2
fi

if [[ "$ACTION" != "add" && "$ACTION" != "delete" && "$ACTION" != "force" ]];then
    # bad args
    cl_echo 9107 "Usage : %s <network name> <add|delete|force>\n" $PROGNAME
    exit 2
fi

set -u

cl_echo 33 "Starting execution of $0 with parameters $*\n" $0 "$*"
date

LOCALNODENAME=$(get_local_nodename)
STATUS=0

# check that this network is using hb via aliasing
cllsnw -J "$OP_SEP" -Sn $NETWORK | cut -d"$OP_SEP" -f4 | grep -q hb_over_alias || exit 0

# get the list of adapters on this node and network
ADAPTER_LIST=$(cllsif -J "$OP_SEP" -Si $LOCALNODENAME | grep "$OP_SEP$NETWORK$OP_SEP" | 
	cut -d"$OP_SEP" -f 9)

if [[ "$ACTION" = "add" ]]; then	# convert action for cl_hb_alias
    ACTION="-e"
elif [[ "$ACTION" = "force" ]]; then	
    ACTION="-f"
else
    ACTION="-d"		# delete 
fi

# now call the util to do the actual ifconfig work
for ADAPTER in $ADAPTER_LIST
do
    # call the utility which does the actual ifconfig
    # dump stderr in case topsvcs is not up yet
    cl_hb_alias $ADAPTER $ACTION 2>/dev/null
    ((STATUS=STATUS+$?))
done

cl_echo 32 "Completed execution of $0 with parameters $*.  Exit status = $STATUS\n" $0 "$*" $STATUS

date
exit $STATUS
