#!/bin/ksh
#  ALTRAN_PROLOG_BEGIN_TAG
#  This is an automatically generated prolog.
#
#  Copyright (C) Altran ACT S.A.S. 2017,2018,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/utils/cl_start_commlinks.sh 1.11 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2002,2004 
# 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/utils/cl_start_commlinks.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM

##
## Script: cl_start_comm_links
##
## Syntax: 
##   cl_start_comm_links commlink1 commlink2 commlink3 ...
##
## Called-by: node_up_local node_down_remote
##
## Parameters: commlinks = list of highly available communications links
##
## Return:      0 on success, 1 on failure, 2 on usage error
##
PROGNAME=$(basename ${0})
export PATH="$($(dirname ${0})/../../utilities/cl_get_path all)"
[[ "$VERBOSE_LOGGING" = "high" ]] && set -x
[[ "$VERBOSE_LOGGING" = "high" ]] && version='1.11'
HA_DIR="$(cl_get_path)"

echo "Starting execution of $0 with parameters $*"
COMMUNICATION_LINKS=$*

set -u

STATUS=0

# are we really trying to start any links?
if [[ -z "$COMMUNICATION_LINKS" ]]
then
        echo "$PROGNAME: usage"
        echo "           cl_start_commlinks commlink1 commlink2 commlink3 ..."
        exit 2
fi

ALLCOMMLINKS="All_commlinks"
cl_RMupdate resource_acquiring $ALLCOMMLINKS $PROGNAME

for LINK in $COMMUNICATION_LINKS
do

        LINKTYPE=$(cllscommlinks -C -t All | \
		awk "BEGIN { FS=\":\" } \$1 == \"${LINK}\" { print \$2 }")

        # start the X25 layer
        if expr "$LINKTYPE" : '.*x25'
        then
                #make sure the commlink monitor is running
                if [[ -z $(ps -eo 'args' | grep -w clcommlinkd | grep -vw grep) ]]
                then
                        clcommlinkd
                fi
                #tell the commlink monitor to start this group's commlinks
                clcompcommlinks acquire $LINK

                #wait 1 minute for acquisition to be completed
		MAXWAIT=18
		WAIT=0
		WAITSTATUS=0
                while [[ -f "/usr/$HA_DIR/sbin/cluster/etc/commlink_add.$LINK" ]]
		do
                        sleep 10
			if [[ $MAXWAIT -le $WAIT ]]
			then
				cl_log 7701 "Time out waiting for $LINK" $LINK
	                        cl_RMupdate resource_error $LINK $PROGNAME
	                        WAITSTATUS=1
				break
			fi
			WAIT=$(( WAIT + 1 ));
                done
		if [[ "$WAITSTATUS" -ne 0 ]]
		then
			STATUS=11
			break
		fi

		#output and check the results
		cat "/tmp/hacmp_commlink_log.start_$LINK"
                if [[ -s "/tmp/hacmp_commlink_err.start_$LINK" ]]
                then
                        cl_RMupdate resource_error $LINK $PROGNAME
                        STATUS=11
			break
                fi
		#junk the old log file and error file
		rm -f "/tmp/hacmp_commlink_log.start_$LINK"
		rm -f "/tmp/hacmp_commlink_err.start_$LINK"

                
                # now get the application service
                APPL=""
                case "$LINKTYPE" in
                "snax25")
                        APPL=$(cllscommlinks -C -t snax25 | \
                                awk "BEGIN { FS=\":\" } \$1 == \"${LINK}\" { \
                                print \$11 }")
                        ;;
                "x25")
                        APPL=$(cllscommlinks -C -t x25 | \
                                awk "BEGIN { FS=\":\" } \$1 == \"${LINK}\" { \
                                print \$8 }")
                        ;;
                esac
                
                #start the application
                if [ -n "$APPL" ]
                then
                        cl_SNAapp -start $APPL "X25"
                        if [ $? -ne 0 ]
                        then
                                cl_log 4166 \
                                        "Error processing script ${APPL}." $APPL
                                cl_RMupdate resource_error $LINK $PROGNAME
                                STATUS=11
				break
                        fi
                fi
        fi

	#reconfigure the SNA dlc if necessary
	if [[ "$LINKTYPE" = "snax25" ]]
	then
        	X25PORT=$(cllscommlinks -C -t All | \
			awk "BEGIN { FS=\":\" } \$1 == \"${LINK}\" \
				{ print \$4 }")
		X25DRIVER=$(lsx25 | awk "\$3 == \"${X25PORT}\" { print \$2 }")
		cl_acquire_sna_dlc $X25DRIVER $LINK
		if [ $? -ne 0 ]
		then
			cl_RMupdate resource_error $LINK $PROGNAME
			STATUS=11
			break
		fi
	fi

        #now handle the SNA portion
        if expr "$LINKTYPE" : 'sna*'
        then
                cl_start_snalink $LINK
                if [ $? -ne 0 ]
                then
                        cl_RMupdate resource_error $LINK $PROGNAME
                        STATUS=11
			break
                fi
        fi

        #if we get all the way down here
        #then the link has been acquired successfully
done

ALLNOERRLINKS="All_nonerror_commlinks"
cl_RMupdate resource_up $ALLNOERRLINKS $PROGNAME

exit $STATUS 
