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

#########################################################################
#
#   COMPONENT_NAME: EVENTS
#
#   FUNCTIONS: none
#
#########################################################################

#########################################################################
#                                                                       #
#       Name:           node_up_remote_complete                         #
#                                                                       #
#	Description:	This script is called when the node_up_remote 	#
#			script successfully completes. 			#
#			This script mounts network filesystems         	#
#			(NFS_mounts filesystems).			#
#									#
#	Called by:	node_up_complete, reconfig_resource_complete,   #
#                       rg_move_complete                                #
#									#
#	Calls to:	cl_activate_nfs					#
#									#
#       Arguments:      nodename	                                #
#                                                                       #
#       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
    eval export $(cllsparam -x)
    set -x
    version='%I%'
fi


NODENAME=$1
STATUS=0
HOMELESS=0

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

if [ ! -n "$EMULATE" ]
then
        EMULATE="REAL"
fi

set -u

for GROUP in $HOMELESS_GROUPS
do
    if [ "$GROUP" = "$GROUPNAME" ]; then
	# This group is homeless, so don't try to mount any NFS
	# filesystems below.
	HOMELESS=1
	break
    fi
done

# 
# Possible NFS re-mount
#
if [ -n "$MOUNT_FILESYSTEM" ] && [ "$HOMELESS" = 0 ]  && [ "$ASSOCIATE_ACTION" != "UMOUNT" ]
then
        NW_NFSMOUNT_LABEL=""

	echo $MOUNT_FILESYSTEM | grep -q "\;/"
	RC=$?
	if [ $RC = 0 ]
	then
	    if [ -z "$NFS_HOST" ]
	    then
		NFS_HOST=$LOCALNODENAME
	    fi

	    if [ -z "$NFSMOUNT_LABEL$TAKEOVER_LABEL$SERVICE_LABEL" ]
	    then
		NFSMOUNT_LABEL=$(odmget -q "group=$GROUPNAME AND name=SERVICE_LABEL" HACMPresource \
		    | grep value | awk '{print $3}' | sed 's/"//g')
	    fi
	fi
	NFSHOST=""

        # The following piece of code was added for CR990012 which      
        # allows the user to pick a preferred network to NFS mount over.        
        # This is similar to the NFSMOUNLABEL which is set by clsetenvres.
        # NW_NFSMOUNT_LABEL is ONLY set in the scripts.  clsetenvres was left out of the loop.
        if [ -n "$NFS_NETWORK" ]
        then
           ALL_NFSMOUNT_LABEL=$(odmget -q "group=$GROUPNAME AND name=SERVICE_LABEL" HACMPresource | grep value | awk '{print $3}' | sed 's/"//g')
           for label in $ALL_NFSMOUNT_LABEL; do
               IN_NETWORK=$(cllsif -cS 2> /dev/null | grep :$NFS_NETWORK: | cut -d: -f1 | grep -x ${label} )
               if [ -n "$IN_NETWORK" ]
               then
                   NW_NFSMOUNT_LABEL="$NW_NFSMOUNT_LABEL $label"
               fi
           done
        fi

	#
	# Select active service label
	#
	# First, make sure to flush the NFSMOUNT_LABEL if it exists.
	# Only one of these nonzero.
	#
	if [ -n "$NW_NFSMOUNT_LABEL$NFSMOUNT_LABEL$TAKEOVER_LABEL$SERVICE_LABEL" ]
	then
	    if [ "$EMULATE" = "EMUL" ]
	    then
		cl_echo 3020 "NOTICE >>>> The following command was not executed <<<< \n"
		echo arp -d $NFSMOUNT_LABEL
	    else
		# make sure to flush the arp cache for this label.
		for label in $NW_NFSMOUNT_LABEL $NFSMOUNT_LABEL $TAKEOVER_LABEL $SERVICE_LABEL
		do
		    arp -d $label
                    : exit status of arp -d $label is: $?
		done 
	    fi
	fi

	if [ -n "$NFS_HOST" ]
	then
	    for host in $NW_NFSMOUNT_LABEL $NFSMOUNT_LABEL $TAKEOVER_LABEL $SERVICE_LABEL
	    do
		ping $host 1024 1 >/dev/null
                RC=$?
                : exit status of ping $host is: $RC
		if [ $RC -eq 0 ]
		then
			NFSHOST=$host
			break
		fi
	    done

	    if [ -n "$NFSHOST" ]
	    then
		cl_activate_nfs 1 $NFSHOST "$MOUNT_FILESYSTEM"
                RC=$?
                : exit status of cl_activate_nfs 1 $NFSHOST $MOUNT_FILESYSTEM is: $RC
		if [ $RC -ne 0 ]
		then
			STATUS=1
		fi
	    else
		cl_log 653 "$PROGNAME: NFS Mounting failed. No reachable service interfaces found on node $NFS_HOST\n"  $PROGNAME $NFS_HOST
	    fi
	fi
fi

#
# Resource Manager Updates
#
if [ $STATUS -ne 0 ]
then
    if [ "$PRINCIPAL_ACTION" = "RELEASE" ]
    then    
        cl_RMupdate rg_error $GROUPNAME $PROGNAME
        # don't mess with STATUS here because it is already set
    elif [ "$AUXILLIARY_ACTION" = "ACQUIRE_SECONDARY" ]
    then
        cl_RMupdate rg_error_secondary $GROUPNAME $PROGNAME
        # don't mess with STATUS here because it is already set
    elif [ "$AUXILLIARY_ACTION" = "RELEASE_SECONDARY" ]
    then
        cl_RMupdate rg_error_secondary $GROUPNAME $PROGNAME
        # don't mess with STATUS here because it is already set
    else 
        cl_RMupdate rg_error $GROUPNAME $PROGNAME
        # don't mess with STATUS here because it is already set
    fi
else
    if [ "$PRINCIPAL_ACTION" = "SUSTAIN" ]
    then
        cl_RMupdate rg_up $GROUPNAME $PROGNAME
        [ $? -ne 0 ] && STATUS=1
    elif [ "$AUXILLIARY_ACTION" = "ACQUIRE_SECONDARY" ]
    then
        cl_RMupdate rg_up_secondary $GROUPNAME $PROGNAME
        [ $? -ne 0 ] && STATUS=1
    elif [ "$AUXILLIARY_ACTION" = "RELEASE_SECONDARY" ]
    then
        cl_RMupdate rg_down $GROUPNAME $PROGNAME
        [ $? -ne 0 ] && STATUS=1
    elif [ "$PRINCIPAL_ACTION" = "RELEASE" ]
    then
        cl_RMupdate rg_down $GROUPNAME $PROGNAME
        [ $? -ne 0 ] && STATUS=1
    fi
fi

exit $STATUS
