#!/bin/sh

all_archs="ppc ppc64 i386 x86_64"

sysattr()
{
	echo "    <sysattr name=\"$1\" value=\"$2\" />"
}

doresults()
{
	local ver

	echo "<tests end_time=\"`date +%s`\" start_time=\"$start_time\" comment=\""$comment"\">"

	echo "  <sysattrs>"
	sysattr cctools "`as</dev/null -v 2>&1 |sed 's/.*cctools-//;s/,.*//'`"
	sysattr hostname "`hostname`"
	sysattr os "`uname -r`"
	sysattr platform "`uname -m`"
	sysattr ld64 "`ld64 -v 2>&1|sed 's/.*PROJECT://;s/ .*//'`"
	sysattr ld "`ld_classic -v 2>&1|sed 's/.*cctools-//;s/ .*//'`"
	sysattr gcc "`gcc --version|head -1`"
	sysattr processor "`uname -p`"
	sysattr LANG "$LANG"
	sysattr LC_CTYPE "$LC_CTYPE"
	sysattr LC_MESSAGES "$LC_MESSAGES"
	sysattr LC_ALL "$LC_ALL"
	sysattr TMPDIR "$TMPDIR"
	sysattr GCC_EXEC_PREFIX "$GCC_EXEC_PREFIX"
	sysattr COMPILER_PATH "$COMPILER_PATH"
	sysattr LIBRARY_PATH "$LIBRARY_PATH"
	sysattr LANG "$LANG"
	sysattr CPATH "$CPATH"
	sysattr C_INCLUDE_PATH "$C_INCLUDE_PATH"
	sysattr CPLUS_INCLUDE_PATH "$CPLUS_INCLUDE_PATH"
	sysattr OBJC_INCLUDE_PATH "$OBJC_INCLUDE_PATH"
	sysattr DEPENDENCIES_OUTPUT "$DEPENDENCIES_OUTPUT"
	sysattr SUNPRO_DEPENDENCIES "$SUNPRO_DEPENDENCIES"
	echo "  </sysattrs>"

	echo "<tools>"
	echo "<tool name=\"ld\">"
	echo "  <results incomplete=\"0\">"
	for i in $*
	do
		echo "    <testgroup name=\"$i\">"
		cat $i
		echo "    </testgroup>"
	done

	echo "  </results>"
	echo "</tool>"
	echo "</tools>"
	echo "</tests>"

	#rm $*
}

find_path_to_test_dir()
{
	# FIND THE PATH TO THE TEST DIR
	# SO THAT WE CAN ADD THE BIN DIR INTO
	# THE SEARCH PATH

	# remember the top level execution dir
	chmod +x "$0"	# just in case

	#add path to $0 into search
	local savedir
	savedir=$PWD
		DIRNAME=`dirname $0`
		[ -d "$DIRNAME" ] && cd "$DIRNAME"
		PATH=$PATH:$PWD
	cd "$savedir"

	chmod +x "$0"	# just in case
	EXECNAME=`which $0`
	DIRNAME=`dirname "$EXECNAME"`
	if [ -d "$DIRNAME" ]
	then
		TEST_HOME_DIR=`cd "$DIRNAME";pwd`
	fi

	if [ -z "$TEST_HOME_DIR" ]
	then
		TEST_HOME_DIR="$savedir" # Give up and assume current dir
	fi

	cd "$TEST_HOME_DIR"
	cd ../build/Release

	PATH="$PWD":"$TEST_HOME_DIR/bin":"$PATH"
	cd "$savedir"
}

start_time=`date +%s`

find_path_to_test_dir

# Execute from the location of the script; or if not found the current loc
[ -d $TEST_HOME_DIR/test-cases ] && cd $TEST_HOME_DIR/test-cases || cd test-cases

rm-stale-test-logs 3 >/dev/null &

make -C ../src	# make sure the binaries are available

DATEFORMAT=`date +%F-%H%M | sed -e 's/ //'`
tmpdir=/tmp/proctor$DATEFORMAT

if ! mkdir $tmpdir >/dev/null 2>/dev/null
then
	rm -rf $tmpdir
	mkdir $tmpdir
fi


linestart=0
if [ x$1 = x-comment ]
then
	shift
	comment="$1"
	shift
fi

find_makefile()
{
	local j

	MF=""

	if [ ! -d $1 ]
	then
		return 1
	fi

	for j in Makefile makefile
	do
		[ -f $1/$j ] && MF=$j
	done

	if [ "$NEWTEST" ]
	then
		for j in Makefile.newtest makefile.newtest
		do
			[ -f $1/$j ] && MF=$j
		done
	fi

	[ "$MF" ] && return 0
	return 1
}

one_test()
{
	echo cwd: $1
	echo cmd: $1 ARCH="$arch"
	make -f "$MF" -C $1 ARCH="$arch" 2>$tmpdir/stderr >$tmpdir/stdout
	result=$?
	sed 's/^/stdout: /'<$tmpdir/stdout
	sed 's/^/stderr: /'<$tmpdir/stderr
	echo exit: $?
}

if [ "$1" ]
then
	i="$1"
	for arch in $all_archs
	do
		rm -f $tmpdir/$arch
		if find_makefile $i
		then
			one_test $i
		fi
		#fi | tee -a $tmpdir/raw | ../bin/results-to-xml $linestart>>$tmpdir/$arch
		linestart=`expr $linestart + 10000`
	done | tee -a $tmpdir/raw | ../bin/results-to-xml $linestart>>$tmpdir/$arch
else
	for arch in $all_archs
	do
		rm -f $tmpdir/$arch
		for i in *
		do
			if find_makefile $i
			then
				one_test $i
			fi
		done | tee -a $tmpdir/raw | ../bin/results-to-xml $linestart>>$tmpdir/$arch
		linestart=`expr $linestart + 10000`
	done
fi

(cd $tmpdir; doresults $all_archs)>$tmpdir/o.xml
../bin/xmlparser $tmpdir/o.xml >/dev/null
if [ $? = 0 ]
then
	if ! proctor localhost ld import $tmpdir/o.xml
	then
		proctor database load failed!
	fi
else
	echo Test results not loaded: internal xml error!
	exit 1
fi
