#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/events/utils/cl_start_snalink.sh 1.15 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1998,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 
# @(#)42  1.15  src/43haes/usr/sbin/cluster/events/utils/cl_start_snalink.sh, hacmp.hawan, 61haes_r714 6/25/04 15:25:10

###############################################################################
#
#  Name:  cl_start_snalink
#
#  This script is used to start 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 andi points to
#  an entry in the ODM object, HACMPsna, from which optional
#  SNA resources will be identified for startup. 
#
# Arguments:    LINKNAME - an SNA DLC 
#
# Returns:      0 - Success
#               1 - Failure
#		2 - Usage error
#
# Called by: swap_adapter, node_up_local, node_down_remote 
# 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' PORTS='%s' LINKS='%s' APPL='%s'\", \
			\$3, \$4, \$5, \$6) }" )

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

else
	# not SNA, nothing to start
	exit 0
fi

if (( $ISSNA5 ))
then
    #SNA will not have to be stopped for version 5
    #but it should be running, issue the command to start sna
    snaadmin query_node
    if (($? != 0))
    then
        sna start
        if (($? != 0))
	then
	    cl_log 4152 "Unable to start Communications Server."
	    exit 1
	fi
    fi

    #The SNA resources that are started for version5 are
    #the DLCs, ports, link stations, and services. There
    #is a hierarchy of resources. If a resource is already active,
    #snadmin will return zero (success).

    #start the DLC
    snaadmin start_dlc, dlc_name=$DLCNAME
    if (($? != 0))
    then
        cl_log 4165 "Unable to start DLC $DLCNAME ." $DLCNAME
        STATUS=1
    fi

    #test for ports.
    if [[ -n $PORTS ]]
    then
        for port in $PORTS
        do
            snaadmin start_port, port_name=$port
            if (($? != 0))
            then
                cl_log 4164 "Unable to start port $port ." $port
                STATUS=1
            fi
        done
    fi

    #test for link stations. 
    if [[ -n $LINKS ]]
    then
	for lstat in $LINKS
	do
	    snaadmin start_ls, ls_name=$lstat
	    if (($? != 0))
	    then
		cl_log 4158 "Unable to start link station $lstat." $lstat
		STATUS=1
	    fi
	done
    fi

else
    #verion 4.2 processing is a little less complex.
    #start all highly available resources associated with $LINKNAME

    #see if sna is running, if not then start it 
    sna -d global
    if (($? != 0))
    then
	sna -start sna
	if (($? != 0))
	then
	    cl_log 4152 "Unable to start Communications Server."
	    exit 1
        fi
    fi
	
    if [[ -n $LINKS ]]
    then
	for lstat in $LINKS
	do
	    sna -s l -p $lstat
	    if (($? != 0))
	    then
		cl_log 4158 "Unable to start link station $lstat." $lstat
		STATUS=1
	    fi
	done
    fi

    #start resources that may have been stopped due to a 
    #configuration change.
    if [[ -f /tmp/sna.link_station ]]
    then
        for l_s in $(cat /tmp/sna.link_station)
        do
            sna -s l -p $l_s
            if (($? != 0))
            then
                cl_log 4158 "Unable to start link station $l_s." $l_s
		STATUS=1
            fi
        done
        rm /tmp/sna.link_station
    fi

    #now start the saved sessions
    if [[ -f /tmp/sna.session ]]
    then
        LINES=$(wc -l /tmp/sna.session | cut -c6-7)
        count=1
        while ((count <= LINES))
        do
            #Work out which sessions may be currently active
            #since starting a link station may also activate
            #a session
            $(sna -d s | cut -c26-50 |sed /"^  *"/,/-/d | \
            sort > /tmp/sna.session.current)
            $(diff /tmp/sna.session /tmp/sna.session.current | \
            grep "<" > /tmp/sna.session.diff)

            if [[ -s /tmp/sna.session.diff ]]
            then
                #Open reading stream
                exec 9< /tmp/sna.session.diff
                read -u9 line
                PARTNERLU=$(echo $line | cut -f2 -d" ")
                MODE=$(echo $line | cut -f3 -d" ")
                sna -s s -pn $PARTNERLU -m $MODE
                #Close reading stream
                exec 9<&-
                rm /tmp/sna.session.current
                rm /tmp/sna.session.diff
            fi
            ((count = count +1))
        done
        rm /tmp/sna.session
    fi
fi

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

exit $STATUS
