#!/bin/sh

function myprint()
{
	echo -n "$1"
	for ((a=0; a <=$[40 -  `echo $1 | wc -c` ]; a++))  
	do
		echo -n " "
	done
	echo $2
}

if [ $1 = "" ]
then
	echo No argument given, aborting...
	exit 1
fi

if [ ! `echo $1 | tail -c 3 | head -c 2` = ".h" ]
then
#	echo Invalid file
	exit 2
fi

nr_of_methods=`check_test $1 -a 2>/dev/null | wc -l`
if [ $nr_of_methods = 1 ]
then
	exit 0
fi

prefix=`echo $1 | sed "s/.*\///g"`
size=$[ `echo $prefix  | wc -c` ]
prefix=`echo $prefix | head -c $[ $size-3 ]`
size=$[ `echo $prefix | wc -c` ]
testfile=`echo $prefix | head -c 1 | tr a-z A-Z`
size=$[ `echo $prefix  | wc -c` ]
testfile=$testfile`echo $prefix | tail -c $[ $size-1 ]`_test.C
testfileWithPath=$BALL_PATH/source/TEST/$testfile
#echo $testfile

if [ ! -e $testfileWithPath ]
then
	#echo Testfile missing for $prefix
	myprint $testfile "!"
	exit 1
fi

if [ $2 = "-x"  ]
then
	check_test $1 $testfileWithPath -c 2>/dev/null
	exit 0
fi

if [ ! -n $2 ] && [ $2 -eq "-c"  ]
then
	check_test $1 $testfileWithPath $3 -c 2>/dev/null
	exit 0
fi

errorline=""
nr_of_problems=`grep "???" $testfileWithPath -c`
if [ $nr_of_problems != 0 ]
then
	errorline="$errorline $nr_of_problems m "
	exit 3
fi

#check_test $1 $testfileWithPath $3 2>/dev/null
check_test $1 $testfileWithPath $3 2>/dev/null 1> $HOME/test_header.tmp

nr_of_problems=`grep "WARNING:" $HOME/test_header.tmp -c`
if [ $nr_of_problems != 0 ]
then
	errorline="$errorline $nr_of_problems""D"
fi

tmp=`grep "Methods missing" $HOME/test_header.tmp`
if [ "$tmp" != "" ]
then
	errorline=" $errorline M "
fi

tmp=`grep "Unknown methods" $HOME/test_header.tmp`
if [ "$tmp" != "" ]
then
	errorline="$errorline U "
fi

if [ "$errorline" != "" ]
then myprint $testfile $errorline
fi

