#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/events/utils/cl_stop_snalink.sh 1.15 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1998,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 
# @(#)43  1.15  src/43haes/usr/sbin/cluster/events/utils/cl_stop_snalink.sh, hacmp.hawan, 61haes_r714 3/20/03 05:38:55

###############################################################################
#
#  Name:  cl_stop_snalink
#
#  This script is used to stop Communications Server resources.
#  It is called n number of times with a Link Name as a variable.
#  Each Link Name is a highly available resource and points to
#  an entry in the ODM object, HACMPsna, from which optional
#  SNA resources will be identified to stop. 
#
# Arguments:    LINKNAME - an SNA DLC 
#
# Returns:      0 - Success
#               1 - Failure
#		2 - Usage error
#
# Called by: node_up_remote, node_down_local 
# Calls to: snaversion	
#
# Environment:  VERBOSE_LOGGING
###############################################################################

###############################################################################
#
# Main Starts Here
#
###############################################################################

PROGNAME=$(basename ${0})
export PATH="$($(dirname ${0})/../../utilities/cl_get_path all)"
[[ "$VERBOSE_LOGGING" = "high" ]] && set -x
[[ "$VERBOSE_LOGGING" = "high" ]] && version='1.15'
HA_DIR="$(cl_get_path)"

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

set -u

if [ $# -ne 1 ] ; then
	cl_echo 4163 "Usage: $PROGNAME Link Name" $PROGNAME
	exit 2
fi

LINKNAME=$1

STATUS=0


#Determine SNA version
ISSNA5=$(snaversion)
if [ $ISSNA5 -eq 2 ] ; then
   cl_echo  4151 "Communications Server not installed or unsupported version."
   exit 1   
fi

#get link information
LINKTYPE=$(cllscommlinks -C -t All | awk "BEGIN { FS=\":\" } \$1 == \"${LINKNAME}\" { print \$2 }")
if [[ "${LINKTYPE}" = "snalan" ]]
then
	eval $(cllscommlinks -C -t snalan | \
			awk "BEGIN { FS=\":\" } \$1 == \"${LINKNAME}\" { \
			printf(\"DLCNAME='%s' LINKS='%s' APPL='%s'\", \
			\$3, \$5, \$6) }" )

elif [[ "${LINKTYPE}" = "snax25" ]]
then
	eval $(cllscommlinks -C -t snax25 | \
			awk "BEGIN { FS=\":\" } \$1 == \"${LINKNAME}\" { \
			printf(\"DLCNAME='%s' LINKS='%s' APPL='%s'\", \
			\$8, \$10, \$11) }" )

else
	#not an sna link, nothing to do
	exit 0
fi



if [[ "$STATUS" = 0 ]] 
then
  if (( $ISSNA5 ))
  then
    #If SNA is stopped then exit now
    #
    snaadmin query_node
    if (($? != 0))
    then
        cl_echo 4160 "Communications Server is not active."
        exit 0
    fi

    #check for uptime == 0, implies SNA not active on node.
    #
    UPTIME=$(snaadmin query_node | grep up_time | cut -f2 -d"=")
    if(( $UPTIME == 0))
    then
        cl_echo 4160 "Communications Server is not active."
	exit 0
    fi

    #Stop the application layer if one is supposed to be stoped
    if [[ -n $APPL ]]
    then
        cl_SNAapp -stop $APPL "SNA"
        if (($? != 0))
        then
            cl_log 4166 "Error processing script $APPL ." $APPL
	    STATUS=1
        fi
    fi

    #Stop the SNA DLC's using the given SNA LINKNAME

    snaadmin stop_dlc, dlc_name=$DLCNAME
    if (($? != 0))
    then
	snaadmin stop_dlc, dlc_name=$DLCNAME, \
	    stop_type = IMMEDIATE_STOP
	if (($? != 0))
	then
	    cl_log 4161 "Unable to stop DLC $DLCNAME." $DLCNAME
	    STATUS=1
	fi
    fi
  else
    #see if sna is running, if not then exit
    sna -d global
    if (($? != 0))
    then
	cl_echo 4160 "Communications Server is not active."
	exit 0
    fi

    if [[ -n $LINKS ]]
    then
	for lstat in $LINKS
	do
	    sna -stop l -p $l_s
	    if (($? != 0))
	    then
	        sna -stop l -t f -p $l_s
	        if (($? != 0))
		then
		    cl_log 4162 "Unable to stop link station $lstat ." $lstat
		    STATUS=1
		fi
	    fi
	done
    fi
  fi
fi

exit $STATUS
