#!/bin/sh


# Check whether shell functions work.  I don't know if this test is
# right, since I don't have a system on which they don't work.

shell_functions_supported()
{
  true
}

shell_functions_supported
if [ 0 != $? ]
then
	echo "This shell does not seem to support shell functions."
	echo "The test suites cannot run without them."
	exit 1
fi

# Generic error message. 
not_installed()
{
 echo "$1 tests have not been installed."
 echo "(They are delivered in a separate TAR file.  See installation instructions.)"
}

# Select and run a test.

if [ "x$2" = xall ] 
then
  GCT_UNPORTABLE_OK=true
  export GCT_UNPORTABLE_OK
elif [ "x$2" != x ]
then
  echo "Second argument must be either 'all' or missing."
  exit 1
fi

if [ "x$1" = x ]
then
	echo "Usage:  one of the following:"
	echo "run install		# The installation test suite."
	echo "run standard		# The standard test suite"
	echo "run weak		# The optional weak-mutation test suite"
	echo "run bugs		# Demonstrate that known bugs still exist."
	echo "Note that weak and standard tests ask permission before starting."
	exit 1
fi

if [ "$1" = "install" ] 
then
	if [ ! -d install ] 
	then
		echo "Installation problem:  installation tests are missing."
		echo "Were they accidentally deleted?"
		exit 1
	fi
	cd install
	make
elif [ "$1" = "bugs" ] 
then
	if [ ! -d known-bugs ] 
	then
		not_installed Known-bug
		exit 1
	fi
	cd known-bugs
	make
elif [ "$1" = "standard" ] 
then
	if [ ! -d instrument ] # If this dir is present, assume rest are.
	then
		not_installed Standard 
		exit 1
	fi
	echo "This script runs the GCT test suite in the background."
	echo "It takes a long time."
	echo "Continue? (y/n)"
	read ans
	if [ x"$ans" = xy ]
	then
		echo "Run 'tail -f LOG' to watch progress."
#		echo "Stop the tests with 'killtest'."
		nice -20 make > LOG 2>&1 &
	fi
elif [ "$1" = "weak" ] 
then
	if [ ! -d weak-mutation ] 
	then
		not_installed "Weak mutation"
		exit 1
	fi
	echo "This script runs the weak mutation tests in the background."
	echo "It takes a long time."
	echo "Continue? (y/n)"
	read ans
	if [ x"$ans" = xy ]
	then
		echo "Run 'tail -f WEAK-LOG' to watch progress."
#		echo "Stop the tests with 'killtest'."
		cd weak-mutation
		nice -20 make > ../WEAK-LOG 2>&1 &
	fi
else
	run	# To get usage message.
	exit 1
fi	


exit 0
