#!/bin/bash
#@DOC@ check good spells
#
# 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() {
	echo "USAGE: regress [OPTIONS]"
	echo "   Run many spells from several dirs through fmtb_parse."
	echo "   Options are passed on to fmtb_parse."
	echo "   Results placed in result_regress.txt"
	echo "OPTIONS"
	echo "   --all  use all files"
	exit 1
}

opts=""
OPT_allfiles=0

for i in "$@"
do
	if [[ x${i%\?}y = x-y ]] ; then
		_usage
	fi
	case $i in
		--all)	OPT_allfiles=1	;;
		*)
			opts="$opts$i "
			;;
	esac
done

dirs=(
	picfmtb_official
	picfmtb_spells
	picfmtb_sean
	picfmtb_acorn1
	picfmtb_acorn_extras
	picfmtb_sanctuary
)

tmp=tmp_regress
tmp1=${tmp}1
tmp2=${tmp}2
tmp3=${tmp}3
tmp4=${tmp}4

\rm -f ${tmp}*
	
if (( OPT_allfiles )) ; then
	_files() {
		for j in picfmtb_*/wpic*.txt wpic*.txt
		do
			if [[ -s $j ]] ; then
				echo "$j"
			fi
		done
	}
else
	_files() {
		for i in "${dirs[@]}"
		do
			for j in $i/wpic*.txt
			do
				if [[ -s $j ]] ; then
					echo "$j"
				fi
			done
		done
	}
fi
_spells() {
	fmtb_parse -d | tail +5 | \
		sed -e 's| *\([^ ]*\).*|\1|'
}

uslist=`_spells | sed -e 's|\([a-z]*\).*|\1|' | sort -u`
_files > $tmp1
for i in $uslist
do
	grep $i $tmp1 > ${tmp2}_$i
done
cat ${tmp2}_* > $tmp3
catsub $tmp1 $tmp3 > ${tmp2}_zzzothers

_doit() {
	for i in ${tmp2}_*
	do
		echo "==================== $i"
		if [[ -s $i ]] ; then
			rrr $opts -c -S $(<$i)
		fi
	done
}

_doit | tee result_regress.txt

echo "Results are in result_regress.txt"
echo "vw result_regress.txt"
echo "less result_regress.txt"
echo "cat result_regress.txt"
