#!/bin/ksh
#
#ident	"@(#)lookup.sh	1.22	04/12/15 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#


stafdir=/opt/SUNWstaf		# STAF directory
hctsdir=SUNWhcts
cfile=DVT.cmd			# dvt form command file
tdir=tests
tmpdir=/tmp
prgdir=prgs

hname=$1
option=$2
technology=$3
nfile=nethost.dat  		# network client file
ser_rdwt_flag=0                 # this flag is to note if ser_rdwt test case has relative devices to use ;

lfile=/opt/SUNWhcts/$tdir/lookup

lvfile=/tmp/level.dat
stfile=/tmp/suite.dat

i6file=/tmp/ipv6.dat				#if this file exists, then means ipv6 test enabled.bill
l6file=/opt/SUNWhcts/$tdir/lookup.net6		#Lookup file for net6

multiportfile=/tmp/multiportfile # for CC multiport certification #yxy

dvt_sys=$tmpdir/dvt_sys
dvt_str=$tmpdir/dvt_str
tmp_str=$tmpdir/tmp_str

if [ -s $stfile ]
then
    suite=`cat $stfile`
fi

if [ -s $lvfile ]
then
    level=`cat $lvfile`
fi

if [ -s $tmpdir/$nfile ]
then
	nets=`cat $tmpdir/$nfile`
fi


# do function
go_fun()
{
        funcs "$*" 2>&1
}

clean_up()
{
	rm -f $tmpdir/cmd-file.* >/dev/null 2>&1
	rm -f $tmpdir/ldev.$$ >/dev/null 2>&1
	rm -f $tmpdir/rdev.$$ >/dev/null 2>&1
	rm -f $tmpdir/pclient >/dev/null 2>&1
        rm -f $tmpdir/devlist* >/dev/null 2>&1
        rm -f $tmpdir/ppp_rhname >/dev/null 2>&1
        rm -f $tmpdir/rhnames >/dev/null 2>&1
        rm -f $tmpdir/grp_* > /dev/null 2>&1

}

filter_storage()
{
	if [ $# -eq 0 ]
	then
		del_line=2
	else
		del_line=$1
	fi

	conf_file="/opt/SUNWstaf/config-util/config.s+h.info"
	mount_file="/opt/SUNWstaf/config-util/mount.info"
	res_num=0
	disk_num=0

	res_num=`wc -l $mount_file | awk '{print $1}'`
	if [ -z "$res_num" ]
	then
		res_num=0
	fi
	disk_num=`cat $conf_file $mount_file |wc -l | awk '{print $1}'`
	if [ -z "$disk_num" ]
	then
		disk_num=0
	fi

	loop=`expr $res_num + 1`
	if [ $loop -gt $disk_num ]
	then
		loop=0
	fi
	i=1
	while [ $i -le 3 ]
	do
		if [ $i -ne $loop ]
		then
			del_num=`grep -n "tests/str/dd.stf --disk_specified $i" $dvt_str |awk -F: '{print $1}'`
			if [ -n "${del_num}" ]
			then
				if [ ${del_line} -eq 2 ]
				then
					del_num1=`expr ${del_num} + 1`
					sed "${del_num},${del_num1}d" $dvt_str >$tmp_str
				else
					sed "${del_num}d" $dvt_str >$tmp_str
				fi
				cp $tmp_str $dvt_str
			fi
		fi
		i=`expr $i + 1`
	done

	rem_fs_num=`expr 3 - $disk_num`
	i=3
	while [ $i -gt $disk_num ] 	
	do
		del_num=`grep -n "tests/str/Fs_stress.stf --disk_specified $i" $dvt_str |awk -F: '{print $1}'`
		if [ -n "${del_num}" ]
		then
			if [ ${del_line} -eq 2 ]
			then
				del_num1=`expr ${del_num} + 1`
				sed "$del_num,${del_num1}d" $dvt_str >$tmp_str
			else
				sed "${del_num}d" $dvt_str >$tmp_str
			fi
			cp $tmp_str $dvt_str
		fi
		i=`expr $i - 1`
	done
}

### MAIN ###

# change to base directory
cd `dirname $0`
while [ `basename $PWD` != $hctsdir ]
do
        cd ..
done

# parse input
if [ -n "$CONTENT_LENGTH" ]
then
        eval `$prgdir/pq.p`
        CONTENT_LENGTH=
fi


if [ $suite = CC ]; then
	tests=`cat "$lfile" | grep -v '#' | grep "$suite" | grep "$level" | grep "$option" | sort -t , -k 5,5`

	if [ -z "$tests" ]; then
		go_fun bhtml $hname
		echo "!ERROR: Failed to find tests for specified technology in the lookup table"
		exit 1
		go_fun ehtml
	fi        

####added by bill for IPv6 test begin
	if [[ ($technology = network) && -e $i6file ]]
	then
		tests=`cat "$lfile" "$l6file" | grep -v '#' | grep "$suite" | grep "$level" | \
				grep "$option" |  sort -t , -k 5,5n`
		if [ -z "$tests" ]; then
			go_fun bhtml $hname
			echo "!ERROR: No tests found for IPv6 in the lookup table."
			exit 1
			go_fun ehtml
		fi        
	fi
####added by bill for IPv6 test end
else
   #add for motherboard certification
   if [ $option = "system" ];then
	cat $lfile | grep -v '#' | grep "$suite" | grep "$level"|grep -v "motherboard" | grep -v "str" > $dvt_sys
	cat $lfile | grep -v '#' | grep "$suite" | grep "$level"|grep -v "motherboard" | grep "str" > $dvt_str

	filter_storage 1

	if [ -e $i6file ]
	then
		tests=`cat "$dvt_sys" "$dvt_str" "$l6file" | grep -v '#' | grep "$suite" | grep "$level"|grep -v "motherboard"`
	else
		tests=`cat "$dvt_sys" "$dvt_str"`
	fi

	if [ -n  "$tests" ]; then
		if [ -f $tmpdir/opt ]; then
			option=`cat $tmpdir/opt`
			for i in $option
			do
				tests=`echo "$tests" | grep -v "$i"`
			done
		fi
	
	
		tests=`echo "$tests" | sort -t , -k 5,5`
	
	else
		go_fun bhtml $hname
		echo "!ERROR: No tests found for the specified technology in the lookup table."
		exit 1
		go_fun ehtml
	fi
    elif [ $option = "motherboard" ];then
	cat $lfile | grep -v '#' | grep "$suite" | grep "$level"|grep "motherboard" | grep -v "str" > $dvt_sys
	cat $lfile | grep -v '#' | grep "$suite" | grep "$level"|grep "motherboard" | grep "str" > $dvt_str

	filter_storage 1

	if [ -e $i6file ]
	then
		tests=`cat "$dvt_sys" "$dvt_str" "$l6file" | grep -v '#' | grep "$suite" | grep "$level"|grep "motherboard"`
	else
		tests=`cat "$dvt_sys" "$dvt_str"`
	fi

	if [ -n  "$tests" ]; then

		tests=`echo "$tests" | sort -t , -k 5,5`


	else
		go_fun bhtml $hname
		echo "!ERROR: No tests found for the specified technology in the lookup table."
		exit 1
		go_fun ehtml
	fi

    fi
  #end add
fi

mingrp=`echo "$tests" | sed -n '1p' | cut -f5 -d','`
#maxgrp=`echo "$tests" | sed -n '$p' | cut -f5 -d','`

find $tmpdir -name grp_*.dvt -exec rm {} \;

tmp=$mingrp
echo "$tests" | while read inline
do
	grp=`echo "$inline" | cut -f5 -d','`

	log=`echo "$inline" | cut -f6 -d','`
	if [ -z "$log" ]; then
		log=false
	fi
	
	if [ $grp -eq $tmp ]; then
		echo "$inline" | nawk -F, '{print $4, $7, $8}' >> $tmpdir/grp_$tmp.dvt
		echo "LOG=$log" >> $tmpdir/grp_$tmp.dvt
			
	elif [ $grp -gt $tmp ]; then
		echo "wait" >> $tmpdir/grp_$tmp.dvt
		echo "$inline" | nawk -F, '{print $4, $7, $8}' >> $tmpdir/grp_$grp.dvt
		echo "LOG=$log" >> $tmpdir/grp_$grp.dvt

		tmp=$grp
	fi
			
done

# Next, we build a command file for the staf test engine
# telling the engine which test modules to run.
# In the /opt/SUNWhcts/tests directory, any test module
# ending in the postfix .dvt will run at this point.
# If you wish to exclude a test module, change the ending
# name to .tst, and if you wish to add new modules, add
# test file names ending in .dvt and edit the test.lst file.


if [ -d $stafdir/cmd-files/$hname ]
then
	rm -f $stafdir/cmd-files/$hname/$cfile >/dev/null 2>&1
else
	mkdir -p $stafdir/cmd-files/$hname
fi
	
			
if [[ -n "$nets" && ($technology = network || $suite = SC) ]]; then
	for i in `ls $tmpdir/*.dvt`
	do
		#yxy modification begins
		if [ -s "$multiportfile" ]
		then
				line_curr=0
				line_find=0
				cat $i | while read inline
				do
					line_curr=`expr $line_curr + 1`
					case `echo "$inline" | cut -b1` in
					[0-9])
						if [ ! -z `echo "$inline" | /usr/xpg4/bin/grep 'net'` ]
						then

							line_find=`expr $line_curr + 1`
							if [ `head -$line_find $i | tail -1 | cut -b1` != 'L' ]
							then
									lineno=1
									for multiline in `cat $multiportfile`
									do
										echo $inline" "$lineno | sed -e "s/HOSTNAME/`echo $multiline | cut -d';' -f2`/g" >> $stafdir/cmd-files/$hname/$cfile
										echo "LOG=false" >> $stafdir/cmd-files/$hname/$cfile
										lineno=`expr $lineno + 1`
									done
							else
									lineno=1
									for multiline in `cat $multiportfile`
									do
										echo $inline" "$lineno | sed -e "s/HOSTNAME/`echo $multiline | cut -d';' -f2`/g" >> $stafdir/cmd-files/$hname/$cfile
										head -$line_find $i | tail -1 >> $stafdir/cmd-files/$hname/$cfile
										lineno=`expr $lineno + 1`
									done
							fi
						else
							line_find=`expr $line_curr + 1`
                                                        if [ `head -$line_find $i | tail -1 | cut -b1` != 'L' ]
                                                        then
                                                                                echo $inline >> $stafdir/cmd-files/$hname/$cfile
                                                                                echo "LOG=false" >> $stafdir/cmd-files/$hname/$cfile
                                                	else
										echo $inline >> $stafdir/cmd-files/$hname/$cfile
                                                                                head -$line_find $i | tail -1 >> $stafdir/cmd-files/$hname/$cfile
                                         		fi
						fi
					;;
					'L')
					;;
					*)
						echo $inline >> $stafdir/cmd-files/$hname/$cfile
					;;
					esac
				done
		else
			cat $i | sed -e "s/HOSTNAME/$nets/g" >> $stafdir/cmd-files/$hname/$cfile
		fi
		#yxy modification ends
	done
		
elif [ $technology = serial ]; then

	lineno_pr=`cat $tmpdir/saip_linenopr`
	cat ppp_rhname > /tmp/saipdefault
	flag=1
	for i in $tmpdir/*.dvt
	do
		ser_rdwt_flag=0
		pcrhname=`cat $tmpdir/ppp_rhname`
		if [ -n "$pcrhname" ]
		then
			for pcr in  $pcrhname
			do
				if [ -f $tmpdir/devlist3.$pcr ]
				then
					lineno=`wc -l $tmpdir/devlist3.$pcr | cut -f8 -d ' '`
					if [ $lineno = 1 ]
					then
						if [ $lineno_pr = 1 ]
						then
							if [ $level = L2 ]
							then
								echo "!ERROR:Enter another ppp_client or add another null modem cable."
								exit 78
							else
								head -1 $tmpdir/devlist3.$pcr > $tmpdir/devlist2.$pcr
								echo "$pcr" >> $tmpdir/rhnames
							fi
						elif [ $flag = 1 ]
						then
							head -1 $tmpdir/devlist3.$pcr > $tmpdir/devlist2.$pcr
							echo "$pcr" >> $tmpdir/rhnames
							flag=2
						else
							tail -$lineno $tmpdir/devlist3.$pcr > $tmpdir/devlist1.$pcr
							echo "$pcr" >> $tmpdir/pclient
							flag=1
						fi

					else
						head -1 $tmpdir/devlist3.$pcr > $tmpdir/devlist2.$pcr
						echo "$pcr" >> $tmpdir/rhnames
						let lineno=lineno-1
						tail -$lineno $tmpdir/devlist3.$pcr > $tmpdir/devlist1.$pcr
						echo "$pcr" >> $tmpdir/pclient
					fi
					pcrhvar=`cat $tmpdir/ppp_rhname | grep -v $pcr`
					echo "$pcrhvar" > /tmp/ppp_rhname
				fi
			done
		fi

		pclient=`cat $tmpdir/pclient`
		if [ -n "$pclient" ]
		then
			lastadd=1
			for ppc in  $pclient
			do
				if [ -f $tmpdir/devlist1.$ppc ]
				then
					for k in `cat $tmpdir/devlist1.$ppc`
					do
						echo $k |awk -F. '{print $1}' > $tmpdir/ldev.$$
						LDEV=`cat $tmpdir/ldev.$$`
						echo $k |awk -F. '{print $2}' > $tmpdir/rdev.$$
						RDEV=`cat $tmpdir/rdev.$$`
						let lastadd=lastadd+1
						LIP="192.168.0."
						RIP="192.168.1."
						cat $i | grep PPH | sed -e "s/PPH/$ppc/g" | sed -e "s/RRM/$LDEV/g" | sed -e "s/RRN/$RDEV/g" | sed -e "s/PSIP/$LIP$lastadd/g" | sed -e "s/PCIP/$RIP$lastadd/g" >> $tmpdir/cmd-file.$$
					done
				fi
			done
		fi				 

		rhname=`cat $tmpdir/rhnames`
		if [ -n "$rhname" ]
		then
			for rh in  $rhname
			do
				if [ -f $tmpdir/devlist2.$rh ]
				then
					for k in `cat $tmpdir/devlist2.$rh`
					do
						if [[ $ser_rdwt_flag = 0 ]]
						then
							echo $k |awk -F. '{print $1}' > $tmpdir/ldev.$$
							LDEV=`cat $tmpdir/ldev.$$`
							echo $k |awk -F. '{print $2}' > $tmpdir/rdev.$$
							RDEV=`cat $tmpdir/rdev.$$`
							cat $i | grep RRH | sed -e "s/RRH/$rh/g" | sed -e "s/RRM/$LDEV/g" | sed -e "s/RRN/$RDEV/g" >> $tmpdir/cmd-file.$$
							ser_rdwt_flag=`expr $ser_rdwt_flag + 1`
						fi
					done
				fi
			done
		fi				 
		cat $i | grep -v PPH | grep -v RRH | grep -v LOG | grep -v wait >> $tmpdir/cmd-file.$$
		echo "wait" >> /tmp/cmd-file.$$
	done

	cat $tmpdir/cmd-file.$$ | sed -e "s/*/\//g" > $tmpdir/cmd-file.$$.after
	mv $tmpdir/cmd-file.$$.after $stafdir/cmd-files/$hname/$cfile
elif [ $technology = storage ]; then

	:>$dvt_str
	for i in `ls $tmpdir/*.dvt`
	do
		cat $i >>$dvt_str
	done

	filter_storage
	cat $dvt_str > $stafdir/cmd-files/$hname/$cfile
else
	
	for i in `ls $tmpdir/*.dvt`
	do
		cat $i >> $stafdir/cmd-files/$hname/$cfile			
	done

fi

clean_up

exit 0



