:
# This is a set of helper functions for tests written for shell_runner.
# This is a bourne shell script, so you can use it with /bin/sh, bash,
# or presumably with other bourne shell compatibles.
#
# This is not well-tested for use with shunit2.
#
# These tools implement output/reference files.  Your test creates an
# output file.  The function testfile compares it to a reference file.
# If they are the same, the test passes.  If not, the test fails.
# Normally, you expect an error the first time because there is no
# reference file.  To make a failing/erroring test pass, use "pdk ok"
# on the okfile that is left behind after running the test.
#
# To use it:
#
#	# load these functions
#	. pdk_shell_runner_helper
#
#	# note the existence of an okfile
#	init_okfile
#
#	# run your software
#	my_program > my_output_file 2> other_output_file
#
#	# compare the files created
#	testfile diff my_output_file	# use this if you want diff output
#
#	testfile cmp other_output_file	# use this if your output is binary
#
#	# use the exit status left behind by testfile
#	exit $pdk_teststatus
#
#
# To use it from csh, see sample_tests/shell/csh_ref.csh in the source code
#

if [ "$PDK_LOG" != "" ]
then

########
######## define the entire interface for living within pdkrun
########

	# find the correct name of the okfile
	pdk_okfile_dir=$PDK_DIRECTORY/okfile/
	pdk_okfile=$pdk_okfile_dir/`basename $PDK_FILE`.okfile

	# Call this to delcare the existence of an okfile.  If you don't have one,
	# don't call it.
	init_okfile () {
		pdk_okfile=$pdk_okfile_dir/`basename $PDK_FILE`.$1.okfile
		# purge any old okfile sitting around
		rm -f $pdk_okfile
		mkdir -p $pdk_okfile_dir

		# make sure there is at least an empty file to look at
		touch $pdk_okfile

		echo 'tda__okfile='$pdk_okfile >> $PDK_LOG
		init_test
	}

	init_test () {
		pdk_teststatus=0
	}

	# Call this once for each output file that you will okify.
	okfile_entry () {
		echo $1 $2 >> $pdk_okfile
	}

	# usage:
	#	test_attr tda value
	#	test_attr tra value

	test_attr() {
		if [ "$_shunit_test_}" != "" ]
		then
			pdk_attribute $1 $2
		else
			case "$1"
			in
			t[dr]a_*)
				echo $1=$2 >> $PDK_LOG
				;;
			*)
				echo bad attribute type passed to test_attr: $1
				pdk_teststatus=128
				;;
			esac
		fi
	}

else
########
######## PDK_LOG not set - define the interface for living without pdkrun;
######## also provide the helper functions from the shunit2 pandokia plugin 
########


	init_test () {
		pdk_teststatus=0
	}

	init_okfile() {
		init_test
	}

	okfile_entry() {
		:
	}

	test_attr() {
		:
	}

	##

	pdk_attribute() {
		:
	}

	pdk_tda() {
		:
	}

	pdk_tra() {
		:
	}

	pdk_error() {
		echo "PDK_ERROR"
		_shunit_assertFail "$*"
	}


fi

########
########
########

#
# features that work the same whether we are in pdkrun or not
#

# Unless we decide otherwise, the test passes
pdk_teststatus=0

# Here is the function to compare output files.  Call it once for each
# output file.
testfile () {
	# testfile $cmd $file
	#
	# cmd is one of:
	#	cmp
	#	diff
	#
	# file is the output file you just created
	#
	# the reference file name is created automatically
	#

	cmd=$1
	file=$2

	bn=`basename $file`

	#
	echo "Comparing output file $file to ref/$bn"

	# We expect the user to pass in the name of the diff command to use.
	# I want to choose "diff" or "cmp", but you can also add in parameters
	# to the comparison.
	$cmd $file ref/$bn

	testfile_status=$?

	cmp_status $testfile_status

	if [ "$testfile_status" = 0 ]
	then
		# if it passes, we do not need to keep the output file
		rm $file
	else
		# if it fails, make an okfile entry for it
		here=`pwd`
		okfile_entry $here/$file $here/ref/$bn
	fi

	echo ''

}


# This function converts the output from diff/cmp into something suitable for
# pandokia test status.  Call this as many times as you want - the result is the
# worst of pass, fail, error.  The "-e" flag is for use by csh scripts.

cmp_status () {
	for st in $*
	do
		if [ X$st = X-e ]
		then
			exit $pdk_teststatus
		fi

		# elevate the exit status if the result of this comparison is
		# worse than a previous one.
		#
		# cmp and diff use these exit codes:
		#	0 - files match
		#	1 - files do not match
		#	2 - problem with comparison

		case $st
		in
		0) 
			:
			;;
		1)
			# raise pdk_teststatus from 0 to 1, but do not lower it from 128
			if [ $pdk_teststatus -eq 0 ]
			then
				# status 1 will log the test result as Fail
				pdk_teststatus=1

				# if we are in shunit2, use its notifier
				if [ "$_shunit_test_" != "" ]
				then
					fail
				fi
			fi
			;;
		*)
			# status 128 will log the test result as Error
			pdk_teststatus=128

			# if we are in shunit2, use its notifier
			if [ "$_shunit_test_" != "" ]
			then
				pdk_error
			fi
			;;
		esac
	done
}

# a function to diff a bunch of output files and handle all the okfile stuff

csh_diff () {
	init_okfile

	okfile_entry $*

	for x in $*
	do
		testfile diff $x
	done

	exit $pdk_teststatus
}

