#!/bin/bash
#@DOC@ try various spells with fmtb_parse
#
# Copyright (C) 2006  Nathan (Acorn) Pooley
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 2 as published by the Free Software Foundation.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License (gpl.txt) for more details. 
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
# 
# You can contact the author, Nathan (Acorn) Pooley, by writing
# to Nathan (Acorn) Pooley, 949 Buckeye Drive, Sunnyvale, CA  94086, USA
# or through the form at http://www.rawbw.com/~acorn/wand/feedback.html
# 

_usage() {
	if [[ -n $1 ]] ; then
		echo "$*"
	fi
	echo "Usage: tryem [OPTIONS]"
	echo "Usage: tryem [OPTIONS] <file> ..."
	echo "   Test all motions in picfmtb_spells/ dir against all known spells."
	echo "   If <file>s are specified then just use those files."
	echo "Options:"
	echo "   -a   test all motions in several dirs (more than default)"
	echo "   -t   just compare tokens from old & new algorithm"
	echo "   -O   use old version of program (fmtb_parse_oldA)"
	echo "   -o   use original algorithm"
	echo "   -M   use classic minimal algorithm"
	echo "   -m   use minimal algorithm"
	echo "   -X   use experimental minimal algorithm"
	echo "   -p   use picsim algorithm"
	echo "   -3   use all 3 algorithms (default)"
	echo "   -s   use silent flag (default)"
	echo "   -S   more silent"
	echo "   -n   do not use silent flag"

	exit 1
}




OPT_prog="fmtb_parse"
OPT_all=0
OPT_skipcheck=0
OPT_tokencheck=0
OPT_use_minimal=0
OPT_use_classic=0
OPT_use_xperimental=0
OPT_use_picsim=0
OPT_use_original=0
OPT_use_all=0
OPT_use_opt=""
OPT_silent=""
OPT_noisy="-s"
OPT_moresilent=0

flist[0]=""
fcnt=0

for i in "$@"
do
	if [[ x${i%\?}y = x-y ]] ; then
		_usage
	fi
	case $i in
		-a)		OPT_all=1	;;
		-t)		OPT_tokencheck=1	;;
		-O)		OPT_prog="fmtb_parse_oldA"	;;
		-o)		OPT_use_original=1
				OPT_use_opt="-o"	;;
		-m)		OPT_use_minimal=1
				OPT_use_opt="-m"	;;
		-M)		OPT_use_classic=1
				OPT_use_opt="-M"	;;
		-x)		OPT_use_experimental=1
				OPT_use_opt="-x"	;;
		-p)		OPT_use_picsim=1
				OPT_use_opt="-p"	;;
		-3)		OPT_use_all=1
				OPT_use_opt="-3"	;;
		-s)		OPT_silent="-s"
				OPT_noisy="-s"		;;
		-S)		OPT_moresilent=1
				OPT_silent="-s"
				OPT_noisy="-s"		;;
		-n)		OPT_silent=""
				OPT_noisy=""		;;
		-*)		_usage "Unknown option '$i'"	;;
		*)		flist[$fcnt]="$i"
				(( fcnt = fcnt + 1 ))
				;;
	esac
done

results=tryem_results.txt
results2=tryem_results2.txt
tmp=tryem_tmp.txt

if (( OPT_use_classic + OPT_use_minimal + OPT_use_picsim + OPT_use_original + OPT_use_all + OPT_use_experimental > 1 ))
then
	usage "Use only one of -O -M -m -p o -3 options"
fi

if (( 1 )) ; then

\rm -f $results

_getspells() {
	grep '{[ 	]*0x[0-9a-fA-F]*,[ 	]*"' spells.c | \
	sed -n -e 's|^[ 	]*{[ 	]*0x[0-9a-fA-F]*,[ 	]*"\([a-zA-Z0-9_]*[a-zA-Z_]\)[0-9]*".*|\1|p' | \
	sort -u
}

list=`_getspells`

if (( OPT_all )) ; then
_getfiles() {
	for j in wpic*.txt picfmtb_*/wpic*.txt
	do
		if [[ -f $j ]] ; then
			echo "$j"
		fi
	done
}
elif (( fcnt > 0 )) ; then
_getfiles() {
	for j in "${flist[@]}"
	do
		if [[ -f $j ]] ; then
			echo "$j"
		fi
	done
}
else
_getfiles() {
	for i in $list
	do
		for j in wpic*$i*.txt picfmtb_spells/wpic*.txt
		do
			if [[ -f $j ]] ; then
				echo "$j"
			fi
		done
	done
}
fi

list2=`_getfiles | sort -u`


if (( OPT_moresilent )) ; then
	_runspell2() {
		$OPT_prog -C -c $OPT_use_opt $OPT_noisy < $1 | \
			tee -a $results > $tmp
	}
else
	_runspell2() {
		$OPT_prog -C -c $OPT_use_opt $OPT_noisy < $1 | \
			tee -a $results | \
			tee $tmp | \
			grep RESULT
	}
fi

_runspell() {
	if (( ! OPT_moresilent )) ; then
		echo "================ $OPT_prog -C -c $OPT_use_opt $OPT_noisy $1"
	fi
	echo "RESULT:================ $1" >> $results
	_runspell2 "$1"
}

if (( OPT_tokencheck )) ; then
	_runspell() {
		./$OPT_prog -C -c -q -t -3 -s < $1 > $tmp
		grep '\*' $tmp >/dev/null && _badspell $1
	}
	_badspell() {
		echo "================ $1"
		echo "./$OPT_prog -C -c -q -t -3 < $1"
		echo "RESULT:================ $1" >> $results
		cat $tmp | tee -a $results
	}
	OPT_skipcheck=1
fi

echo "RESULT:" > $results
for j in $list2
do
	if [[ -f $j ]] ; then
		_runspell "$j"
		grep 'ERROR' $tmp && exit 1
		grep 'ASSERT' $tmp && exit 1
	fi
done

if (( ! OPT_moresilent )) ; then
	echo "######################"
	echo "######################"
	echo "######################"
fi

fi

if (( OPT_skipcheck )) ; then
	exit 0
fi


if (( ! OPT_moresilent )) ; then
	grep 'RESULT:' $results | egrep '\*|=|\+' 
fi
grep 'RESULT:' $results > $results2



_check() {
	typeset showSummary=$1
	typeset showColor=$2
	typeset a
	typeset b
	typeset c
	typeset mo=""
	typeset mofile=""
	typeset val1=""
	typeset val2=""
	typeset resa=""
	typeset resb=""
	typeset scorea=0
	typeset scoreb=0
	typeset cnta=0
	typeset cntb=0
	typeset cntc=0
	typeset cntd=0
	typeset cnte=0
	typeset cntf=0
	typeset cntg=0

	if (( showColor )) ; then
		c0="[0m"
		c03="[30;43m"
		c71="[37;41m"
		c72="[37;42m"
		c74="[37;44m"
	else
		c0=""
		c03=""
		c71=""
		c72=""
		c74=""
	fi

	while read a b c
	do
#echo "READ $a $b $c" >&2
		case $a in
			A)	mo=`echo $c`
				mofile="$b"
				;;
			B)	resa=`echo $c`
				scorea=`echo $b`
				;;
			C)	resb=`echo $c`
				scoreb=`echo $b`
				;;
			*)	echo "ERROR: $a $b $c"
				;;
		esac
		if [[ -n $resa && -n $resb ]] ; then
#echo "READ mo=$mo  resa=$resa  scorea=$scorea  resb=$resb  scoreb=$scoreb" >&2
			val1=""
			val2=""
			if [[ $resa = $mo ]] ; then
				if (( scoreb < 20 )) ; then
					val1="F$c71"
					val2="TOO CLOSE$c0"
					(( cntf = cntf + 1 ))
				elif (( scorea < 11 )) ; then
					val1="A$c72"
					val2="good$c0"
					(( cnta = cnta + 1 ))
				elif (( scoreb - scorea < 30 )) ; then
					val1="E$c03"
					val2="TOO CLOSE$c0"
					(( cnte = cnte + 1 ))
				elif (( scorea < 30 )) ; then
					val1="B$c74"
					val2="close$c0"
					(( cntb = cntb + 1 ))
				else
					val1="C$c03"
					val2="FAR OFF$c0"
					(( cntc = cntc + 1 ))
				fi
			elif (( scoreb < 30 )) ; then
				val1="G$c71"
				val2="FALSE POSITIVE$c0"
				(( cntg = cntg + 1 ))
			else
				val1="D$c03"
				val2="MISS$c0"
				(( cntd = cntd + 1 ))
			fi

			printf "%s%-25s %-15s %4d %-15s  %4d %-15s   %s\n" \
					"$val1" "$mofile" "$mo" \
					$scorea "$resa" $scoreb "$resb" \
					"$val2"
			mo=""
			mofile=""
			resa=""
			resb=""
			scorea=0
			scoreb=0
		fi
	done
	if (( showSummary )) ; then
		printf "X      GRN BLU   YELLOW_____   RED____\n" 
		printf "Y WAS: %3d %3d   %3d %3d %3d   %3d %3d\n" \
					23   9     2   1   6     7   1
#					17   7     1   0   6     2   0
#					18   7     1   1   8     5   0
#					20   7     1   2   8     8   1
		printf "Z NOW: %3d %3d   %3d %3d %3d   %3d %3d\n" \
					$cnta $cntb $cntc $cntd $cnte $cntf $cntg
	fi
}

if (( ! OPT_moresilent )) ; then
	grep 'RESULT:====' $results | sort
fi

if (( 1 || ! OPT_moresilent )) ; then
	grep 'RESULT:' $results | egrep '\*|=|\+' | \
		sed -n \
		-e 's|^\(RESULT:==*\) *[a-zA-Z0-9_/]*/\(wpic[a-zA-Z0-9_]*\.txt.*\)|\1 \2|' \
		-e 's|^RESULT:==* \(wpic\([a-z]*\)[_A-Z0-9][^.]*\.txt\).*|A \1 \2|p' \
		-e 's|^RESULT: \* *0*\([0-9][0-9]*\) *\([a-zA-Z_]*\)[0-9]*.*|B \1 \2|p' \
		-e 's|^RESULT: + *0*\([0-9][0-9]*\) *\([a-zA-Z_]*\)[0-9]*.*|C \1 \2|p' | \
		_check 0 0 | sed 's|^.|OPL: |' | sort
fi

if (( 1 || ! OPT_all )) ; then
	grep 'RESULT:' $results | egrep '\*|=|\+' | \
		sed -n \
		-e 's|^\(RESULT:==*\) *[a-zA-Z0-9_/]*/\(wpic[a-zA-Z0-9_]*\.txt.*\)|\1 \2|' \
		-e 's|^RESULT:==* \(wpic\([a-z]*\)[_A-Z0-9][^.]*\.txt\).*|A \1 \2|p' \
		-e 's|^RESULT: \* *0*\([0-9][0-9]*\) *\([a-zA-Z_]*\)[0-9]*.*|B \1 \2|p' \
		-e 's|^RESULT: + *0*\([0-9][0-9]*\) *\([a-zA-Z_]*\)[0-9]*.*|C \1 \2|p' | \
		_check 1 1 | sort | sed 's|^.||'
fi
