#!/bin/sh
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.

#pragma ident	"@(#)jsas-get-na-list.sh	1.1	04/11/29 SMI"

#
# What:
#	This script is used to get the list of Node Agents
#	that are configured to listen on the HOSTNAME
#	passed as an argument to this list.
#
# How:
#	All the Node Agents are created under the Node Agents
#	directory nodeagents_dir. E.g the Node agents directory
#	structure is as shwon below 
#	root@pnode1% pwd
#	/global/dg1/na-dir
#	root@pnode1% ls
#	na1/  na2/  na3/
#	root@pnode1% 
#	The nodeagents.properties file under 
#	<nodeagent_name>/agent/config/nodeagent.properties
#	has a variable agent.client.host which is set to the
#	hostname on which the nodeagent is configured.
#
#	This script lists all the Nodeagents that are configured 
#	on the HOSTNAME, which is passed as a parameter to this
#	script.
#	
#	echo the Node agents that are configured on $HOSTNAME
#	on the stdout.
#	exit 1 if there is a failure or if no Node agent is 
#	configured to listen on the $HOSTNAME.
#set -x

PATH=/bin:/usr/bin:/usr/cluster/bin:/usr/sbin:/usr/cluster/lib/sc:$PATH;export PATH
syslog_tag="SC[SUNW.jsas,jsas-get_node_agents_list]"

# Checking the command syntax

if [ $# -ne 2 ]
then
	scds_syslog -p error -t $syslog_tag -m \
		 "INTERNAL ERROR usage:$0 nodeagents_dir nodeagents-hostname"
	exit 1
fi


NODEAGENTS_DIR=$1
HOSTNAME=$2

count=1

for file in $NODEAGENTS_DIR/*
do
	if [ ! -f $file/agent/config/nodeagent.properties ]
	then
		exit 1
	fi

	client_host=`/usr/bin/grep agent.client.host $file/agent/config/nodeagent.properties | /usr/bin/nawk -F= '{print $2}'`

	if [ $client_host = $HOSTNAME ]
	then
		count=`expr $count + 1`
		agentname=`basename $file`
		/usr/bin/echo "$agentname"
	fi

done

if [ $count -eq 0 ]
then
	exit 1
else
	exit 0
fi
