#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/events/utils/cl_stop_commlinks.sh 1.9 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2002,2003 
# 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 
# @(#)34  1.9  src/43haes/usr/sbin/cluster/events/utils/cl_stop_commlinks.sh, hacmp.hawan, 61haes_r714 2/28/03 16:04:41

##
## Script: cl_start_comm_links
##
## Syntax: 
##   cl_stop_comm_links commlink1 commlink2 commlink3 ...
##
## Called-by: node_down_local node_up_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.9'
HA_DIR="$(cl_get_path)"

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

COMMUNICATION_LINKS=$*

set -u

STATUS=0

# are we really trying to stop 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_releasing $ALLCOMMLINKS $PROGNAME

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

        #first handle the SNA portion
        if expr "$LINKTYPE" : 'sna*'
        then
                cl_stop_snalink $LINK
                if [ $? -ne 0 ]
                then
                        cl_RMupdate resource_error $LINK $PROGNAME
                        STATUS=1
			continue
                fi
        fi

        # next stop the X25 layer
        if expr "$LINKTYPE" : '.*x25'
        then
                # stop 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
                #stop the X25 application layer
                if [ -n "$APPL" ]
                then
                        cl_SNAapp -stop $APPL "X25"
                        if [[ $? -ne 0 ]]
                        then
                                cl_log 4166 \
                                        "Error processing script ${APPL}." $APPL
                                cl_RMupdate resource_error $LINK $PROGNAME
                                STATUS=1
				continue
                        fi
                fi
		
		#make sure the commlink monitor is running
                if [ ! -f "/usr/$HA_DIR/sbin/cluster/etc/comm_link.lck" ]
                then
                        #if it's not running, then nothing else to do
			continue
                fi
                #tell the commlink monitor to start this group's commlinks
                clcompcommlinks release $LINK

                #wait 1 minute for release to be completed
		MAXWAIT=18
		WAIT=0
		WAITSTATUS=0
                while [[ -f "/usr/$HA_DIR/sbin/cluster/etc/commlink_del.$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=1
			continue
		fi

		#output and check the results
		cat "/tmp/hacmp_commlink_log.stop_$LINK"
                if [[ -s "/tmp/hacmp_commlink_err.stop_$LINK" ]]
                then
                        cl_RMupdate resource_error $LINK $PROGNAME
                        STATUS=1
			continue
                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"
        fi

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

ALLNOERRLINKS="All_nonerror_commlinks"
cl_RMupdate resource_down $ALLNOERRLINKS $PROGNAME

exit $STATUS
