#!/bin/bash

# regression test  (need to run regrtest -ocaml in ../elsa before)
#
# run ast_graph and dot on all ast files found
#
# takes a long time because doing graph layout with dot is time consuming
# to improve to timings you can patch dot to exit after parsing the dot file
# call this dot-parseonly, place it into the PATH and use the option -parse


trap "echo Interupted; exit 1" SIGINT


# load variable ast-files
. regtest-ast-files


function usage(){
    echo "usage: regtest-graph [-parse] [-skip <n>]"
    exit 1
}

skip=0
typeset -i count=0

dot=dot

#echo args: $#

while [ $# -ge 1 ] ; do
    case $1 in
	-parse)
	    dot=dot-parseonly;;
	-skip)	
	    if [ $# -lt 2 ] ; then
		usage
	    fi
	    skip=$2
	    shift;;
	*)  usage;;
    esac
    shift
done

#exit

for f in $ast_files ; do
    if [ $skip -gt $count ]; then
	echo "[$count] skip $f"
    else
	echo "[$count] test $f"
	./ast_graph -all $f -o $f.dot
	result=$?
	if [ ! $result -eq 0 ] ; then
	    echo ast_graph exit $result on $f >&2
	    exit $result
	fi
	echo $f | grep -q big
	if [ $? = 0 ] ; then
	    $dot -v -Tps $f.dot -o $f.ps
	    result=$?
	else
	    $dot -Tps $f.dot -o $f.ps
	    result=$?
	fi
	result=$?
	if [ ! $result -eq 0 ] ; then
	    echo $dot exit $result on $f >&2
	    exit $result
	fi
    fi
    count=$count+1
done

