#!/bin/sh

#
# include common functions, variables...
#
# qryParam
#
source /usr/lib/cgi-bin/stf/xml/bin/common.sh

#
# run-std-tests
#
#
# given a set of xml files in /var/www/stf/xml/std-tests
# put the results in /var/www/stf/xml/std-results
#
echo "Content-type: text/html"
echo

#
# 1) set fname
#
fname=`mk-name`

#
# 2) get test name from query string
#
testnames=`(cd /var/www/stf/xml/std-tests; lessecho *.xml)`

if [[ ${#testnames} == 0 ]]; then
  echo "<h2>Error! no tests specified</h2>"
  exit 0
fi

#
# make sure testname exists!
#
if [[ ! -d ${xmlpath}/std-results ]]; then 
    echo "<h2>Error\!  No results directory</h2>"
    exit 0
fi

#
# get the list of stfservers -- and put them
# in stfmode...
#
stfmode > /tmp/dhtab.$$

ndoms=`wc -l /tmp/dhtab.$$ | awk '{print $1; }'`
ntests=`echo ${testnames} | wc -w`
ntests=`printf "%d" ${ntests}`

#
#
# setup the header...
#
echo "<html>"
echo "<head>"
echo "<title>Run all tests</title>"
echo "</head>"
echo "<body>"

#
# setup the table
#
echo "<table><tbody>"
echo "<tr> <th>Card</th> <th>Ch</th> <th>DOM</th> <th>Sel</th>"
i=1
for f in ${testnames}; do
    fullqxml=${xmlpath}/std-tests/$f
    testname=`cat ${fullqxml} | xmlv | \
	awk '/^\(/ { depth++; if (depth==2) print substr($0, 2); } '`
    echo "<th><a href=\"/cgi-bin/stf/conf-test?testname=${testname}\">${i}</a></th>"
    i=`expr ${i} + 1`
done
echo "</tr>"

#
# get current picks from cookie or set default (first
# in servers file...
#
qstfserver=`currentServer`
qstfport=`currentPort`

session=`mk-name`
mkdir ${xmlpath}/std-results/${session}

for (( i=1; i<=${ndoms}; i++ )); do
    respath=`printf "%s/%04d" ${xmlpath}/std-results/${session} $i`
    mkdir ${respath}
    dhline=`sed -n "${i}p" /tmp/dhtab.$$`
    stfserver=`echo ${dhline} | awk '{print $1;}'`
    stfport=`echo ${dhline} | awk '{print $2;}'`

    echo "<tr>"
    echo "<td>" `echo ${dhline} | awk '{print $3;}'` "</td>"
    echo "<td>" `echo ${dhline} | awk '{print $4;}'` "</td>"
    echo "<td>" `echo ${dhline} | awk '{print $5;}'` "</td>"
    echo "<td>"
    if [[ ${qstfserver} == ${stfserver} && ${qstfport} == ${stfport} ]]; then
	echo "*"
    else
	echo " "
    fi
    echo "</td>"

    for f in ${testnames}; do
	fullqxml=${xmlpath}/std-tests/$f
	fullrxml=${respath}/$f

	testname=`cat ${fullqxml} | xmlv | \
	    awk '/^\(/ { depth++; if (depth==2) print substr($0, 2); }'`
	   
	touch ${xmlpath}/${stfserver}:${stfport}
	chkpt 100 \
	    stftcp ${stfserver} `expr ${stfport} + 3000` \
		-send ${fullqxml} -go ${testname} \
		-rcv ${testname} ${fullrxml} >& /tmp/stf.out
	rm -f ${xmlpath}/${stfserver}:${stfport}

	if [[ ! -e ${fullrxml} ]]; then
	    echo "<h3>Error: couldn't create results</h3>"
	    echo "<p>The reason:"
	    echo "<pre>"
	    cat /tmp/stf.out
	    echo "</pre>"
	    rm /tmp/stf.out
	    exit 0
	fi

	rm -f /tmp/stf.out

	#
	# add results to db...
	#
	resid=`addResults ${fullrxml}`

	#
	# find out if we passed...
	#
	cgiurl="/cgi-bin/stf"
	echo "<td><a href=\"${cgiurl}/view-results?file=${resid}\">"
	
	qry="select passed from STFResult where stf_result_id=${resid};"
	torf=`${mysqlcmd} "${qry}"`

	if [[ ${torf} = "1" ]]; then
	    echo "P"
	else
	    echo "F"
	fi
		
	echo "</a></td>"
    done
    echo "</tr>"
done

echo "</tbody>"
echo "</table>"
echo "</body>"
echo "</html>"

rm -f /tmp/dhtab.$$





