#!/bin/ksh
#
#ident	"@(#)devid.sh	1.9	04/12/15 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
LOCALDEV=
REMOTEDEV=
L_DEVLIST=
R_DEVLIST=
RHOST=$1
RUNPRDR=10
RUNPRDL=
RNUMDEV=
rhname=$1
utildir=/opt/SUNWhcts/prgs

# functions
clean_up()
{
        if [ -f /tmp/devlist* ]
        then
		rm -f /tmp/*devnam*
		rm -f /tmp/dev.var
		rsh -l root $rhname rm -f /tmp/devnam*
        fi

}

### MAIN ###

# Trap signals
trap 'clean_up; exit' 17

rsh -l root $rhname "if [ -f /tmp/devnam ]
then
        rm -f /tmp/devnam* 
fi"

L_DEVLIST=`ls /dev/cua `
if [ -z "$L_DEVLIST" ]
then
	echo "!ERROR: No local device available for test."
	exit 1
fi

R_DEVLIST=`rsh -l root $rhname ls /dev/cua `
if [ -z "$R_DEVLIST" ]
then
	echo "!ERROR: No remote device available for test."
	exit 1
else
	RNUMDEV=`echo $R_DEVLIST |wc -w`
	RUNPRDL=`expr $RUNPRDR \* $RNUMDEV`
fi

for i in $L_DEVLIST
do
	env RUNPRD=10 $utildir/devido -d /dev/cua/$i -b 38400  > /tmp/devido 2>&1 &
	#env RUNPRDL $utildir/devido -d /dev/cua/$i -b 38400  > /tmp/devido 2>&1 & 
	devid=$!
	LOCALDEV=$i
	for j in $R_DEVLIST
	do
	     rsh -l root $rhname $utildir/devidi -d /dev/cua/$j -b 38400  > /tmp/devidi 2>&1 &
	done
	sleep $RUNPRDR
	rsh -l root $rhname "if [ -f /tmp/devnam ]
	then
		mv /tmp/devnam /tmp/devnam.$i 
	fi"
	wait $devid
done

rcp $rhname:/tmp/devnam.* /tmp 
#ls /tmp >> /tmp/errors

for i in `ls /tmp/devnam.*`
do
	TEMPDEV=`ls $i | awk -F. '{print $2}'`
	LOCALDEV=`echo /dev/cua/$TEMPDEV`
	REMOTEDEV=`cat $i`
	echo $LOCALDEV.$REMOTEDEV >> /tmp/devlist.$rhname 
done

devnamvar=`ls /tmp/devnam*` 
if [ -n "$devnamvar" ]
then
	rm -f /tmp/devnam* 
fi

rdevnamvar=`rsh -l root $rhname ls /tmp/devnam*`
if [ -n "$rdevnamvar" ]
then
        rsh -l root $rhname rm -f /tmp/devnam*  
        rsh -l root $rhname rm -f /tmp/devnam  
fi
