#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/utilities/cllsparent.sh 1.2 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1999,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 
# @(#)38	1.2 src/43haes/usr/sbin/cluster/utilities/cllsparent.sh, hacmp.cspoc, 61haes_r714 2/20/04 10:51:42

#############################################################################
#
# Desription
#
#   Checks for accessibility to all nodes.  If all are accessible,
#   then for each node, displays the possible parent devices that 
#   accept a specified connection type or device.
#
# Args
#
#   -C 		Lists information about a device that is in the Customized 
#		Devices object class
#
#   -P	 	Lists information about a device that is in the Predefined 
#	 	Devices object class.
#
#   -k ChildConnectionKey   Specifies the connection key that identifies
#	   	the device subclass name of the child device.
#
#   nodelist 	A comma-separated list of node names
#
#
# Returns
#
#	0 List of device types retrieved from a node
#	1 Error
#	2 Usage error
#
#############################################################################
PATH="$($(dirname ${0})/../utilities/cl_get_path all)"
CMD_NAME=`basename $0`

#  Check for flags
ARGS=""
CuDv=""
PdDv=""
Child=""
while [[ $1 = -* ]] ; do
    case $1 in
        -C ) ARGS=$ARGS" -C ";CuDv="True";;
	-P ) ARGS=$ARGS" -P ";PdDv="True";;
        -k ) ARGS=$ARGS" -k "$2;Child="True";shift;;
    esac
    shift
done

NODENAMES="$@"
NODENAMES_WITH_SPACE=`echo $NODENAMES | sed 's/,/ /g'`

if [ -z "$NODENAMES_WITH_SPACE" -o -z "$Child" -o  -z "$CuDv" -a -z "$PdDv" -o -n "$CuDv" -a -n "$PdDv" ]
then
    cl_echo 5400 "Usage: \n\
    cllsparent -C {-k ChildConnectionKey} nodelist \n\
    cllsparent -P {-k ChildConnectionKey} nodelist\n"
    exit 2
fi

STATUS=0
for NODE in $NODENAMES_WITH_SPACE
do
    ADDRESS=`clgetaddr $NODE 2>/dev/null`
    if [ $? -ne 0 ]
    then
        cl_echo 5301 "$CMD_NAME Node: $NODE is not reachable.  Please check rhosts entries." $CMD_NAME $NODE
	STATUS=1
    fi
done

if [ $STATUS -eq 1 ]
then
    exit 1
fi

# Print Node-Parent pairs
TEMP_FILE="/tmp/cllsparent"
for NODE in $NODENAMES_WITH_SPACE
do

    # Get parent list from each node and save to temporary file
    ADDRESS=`clgetaddr $NODE 2>/dev/null`
    cl_rsh $ADDRESS "lsparent $ARGS" > $TEMP_FILE
    if [ $? -ne 0 ]
    then
	exit 1
    fi

    # Prepend node name to beginning of each line
    while read -r field1
    do
        print $(printf "%s %s ") $NODE,$field1
    done < $TEMP_FILE

done


exit 0
