#!/bin/sh -x


sanitize()
{
	basename "$1" .fnp | tr ' ' _ | tr -cd 'A-Za-z0-9_-' | tr -s _ _
}


usage()
{
cat <<EOF 1>&2
usage: $0 [-e|-p] [-n [-o] [-s]] dir

  -e  generate the calculated expression (default: just dump debug output)
  -p  profile 10000 runs (default: just dump debug output)
  -n  use "new" scheduler
  -o  enable LCPF optimizer
  -s  enable register pressure statistics
EOF

}


M1=`make path`

extra=
sched=
profile=false
evaluate=false

while [ "${1#-}" != "$1" ]; do
	case "$1" in
	-e)	evaluate=true;;
	-p)	profile=true;;
	-n)	sched=SCHED=sched.o;;
	-o)	extra="$extra -DLCPF";;
	-s)	extra="$extra -DREG_STATS";;
	*)	usage;;
	esac
	shift
done

[ -z "$1" -o "$2" ] && usage
if [ ! -d "$1" ]; then
	echo "$1: directory not found" 1>&2
	exit 1
fi

$profile || extra="$extra -DCOMP_DEBUG"

make spotless
make CFLAGS_EXTRA="$extra" $sched all

for n in $M1/flickernoise/patches/*/*.fnp; do
	s=`sanitize "$n"`
	if $profile; then
		./main "$n" 10000 || exit
		gprof main >"$1"/$s
	elif $evaluate; then
		./main "$n" | ./eval.pl >"$1"/$s || exit
	else
		./main "$n" >"$1"/$s || exit
	fi
done
