function elapsed_to_ms()
{
	local ELAPSED=$1
	local IFS=:.
	local MS

	set -- $ELAPSED
	if [ $# -eq 3 ] ; then
		let MS=${3#0}*10+${2#0}*1000+$1*60000
	else
		let MS=${4#0}*10+${3#0}*1000+${2#0}*60000+$1*3600000
	fi

	echo $MS
}

function t_timestamp()
{
	date +"%Y-%m-%d %H:%M:%S %z"
}

function t_marker()
{
	echo
	echo "======================================================================"
}

function t_detail()
{
	local KEY=$1
	local VALUE=$2
	local DOTS=" ............"

	printf "%-.12s: %s\n" "$KEY$DOTS" "$VALUE"
}

function t_announce()
{
	local TESTNAME="$1"
	local len=`echo "$1" | wc -c`
	local start=`expr $len - 31`
	if [[ $len > 32 ]] ; then
	   # test name too long - trim from left
	   TESTNAME="...`echo $TESTNAME | cut -c ${start}-`"
	fi

	printf "$BOLD%-.35s$RESET " "$TESTNAME .............................."
	t_marker >> "$LOGFILE"
	t_detail "DATE" "$(t_timestamp)" >> "$LOGFILE"
	t_detail "TESTNAME" "$TESTNAME" >> "$LOGFILE"
}

function t_result()
{
	local COLOR="$1"
	local TEXT="$2"

	printf "[$COLOR$TEXT$RESET]"
}

function t_pass()
{
	local TESTNAME="$1"

	t_result "$GREEN" "pass"
	echo "pass $TESTNAME" >> "$COUNTFILE"
}

function t_fail()
{
	local TESTNAME="$1"

	t_result "$RED" "fail"
	echo "fail $TESTNAME" >> "$COUNTFILE"
}

function t_kill()
{
	local TESTNAME="$1"

	t_result "$RED" "kill"
	echo "fail $TESTNAME" >> "$COUNTFILE"
}

function t_skip()
{
	local TESTNAME="$1"

	t_result "$BLUE" "skip"
	echo "skip $TESTNAME" >> "$COUNTFILE"
}

function t_indent()
{
	sed -e 's/^/  /'
}

LOGFILE="$TOPDIR/test.log"
COUNTFILE="$TOPDIR/test.counts"
TIMEFILE="$TOPDIR/test.time"

if [ -t 1 ] ; then
	RED="\e[31m"
	GREEN="\e[32m"
	BLUE="\e[34m"
	BOLD="\e[1m"
	DEFAULT="\e[39m"
	RESET="\e[0m"
fi
