#!/bin/bash


PATH=.:$PATH
timeout=60
JAVA="java -Xmx400m"

if [ ! -f libic.a ]
then
	echo libic.a not found
	exit 1
fi


if [ ! -f ../classes/IC/Compiler.class ]
then
	echo IC/Compiler.class not found
	exit 1
fi

alltests=`ls *.is`
log="log.txt"

rm -f $log

for i in $alltests
do
	test=`echo $i | sed 's/\.ic$//'`
	
	if [ ! -f $test.in ]
	then
		echo $test.in not found
		exit
	fi

	if [ ! -f $test.out ]
	then
		echo $test.out not found
		exit
	fi
done

for i in $alltests
do
    test=`echo $i | sed 's/\.ic$//'`
	
    echo | tee -a $log
    echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" \
	    | tee -a $log
    echo "+++ Test: $test" | tee -a $log
    #echo Press any key to continue...
    #read dummy

    ok=1

    for FLAGS in "" "-opt" "-opt-backend" "-opt -opt-backend";
    do

        rm -f $test.s $test.o $test.exe

	if [ $ok -ne 1 ] 
	then 
	    continue
	fi

	withflags="(with $FLAGS)"
	
	echo | tee -a $log
	echo "+++ Compiling $test.ic $withflags" | tee -a $log
       	./timeout $timeout \
	    time -f "+++ Compile time: %E" -o $test.compile.time \
	    $JAVA -cp "../classes;java_cup.jar" \
	    IC.Compiler $FLAGS $test.ic 2>&1 >$test.compile.out

        if [ $? -eq 99 ]
        then
#           pskill time > /dev/null
#           pskill java > /dev/null              
	    echo "*** Compile failed" | tee -a $log
	    echo "*** Time limit ($timeout sec.) exceeded" | tee -a $log
	    ok=0
	    continue
	else
	    cat $test.compile.time | grep "+++.*" | tee -a $log
        fi

	if [ ! -f $test.s ]
	then
	    echo "*** Compile failed"  | tee -a $log
	    echo "*** $test.s not found" | tee -a $log
	    ok=0
	    continue
	fi

	echo "+++ Assembling $test.s" | tee -a $log
	as -o $test.o $test.s 2>&1 >$test.s.out

	if [ ! -f $test.o ]
	then
	    cat $test.s.out | tee -a $log
	    echo "*** as failed" | tee -a $log
	    echo "*** $test.o not found" | tee -a $log
	    ok=0
	    continue
	fi
	
	echo "+++ Linking $test.o" | tee -a $log
	ld -o $test.exe $test.o /lib/crt0.o libic.a \
	    -lcygwin -lkernel32 2>&1 > $test.link.out

	if [ ! -f $test.exe ]
	then
	    cat $test.link.out | tee -a $log
	    echo "*** Linker failed" | tee -a $log
	    echo "*** $test.exe not found" | tee -a $log
	    ok=0
	    continue
	fi

	rm -f $test.ic.out

	input=`cat $test.in`

        if [ ! -f $test.stdin ]
        then
	    echo "+++ Running $test.exe "
	    ./timeout $timeout \
	        time -f "+++ Run time: %E" -o $test.run.time \
	        ./$test.exe $input 2>&1 >$test.ic.out
        else
	    echo "+++ Running $test.exe with stdin"
 	    ./timeout $timeout \
                ./runapp.sh $test
        fi
	if [ $? -eq 99 ]
	then
#           pskill time > /dev/null
#           pskill $test.exe > /dev/null
	    echo "*** Run failed" | tee -a $log
	    echo "*** Time limit ($timeout sec.) exceeded" | tee -a $log
	    continue
	else
	    cat $test.run.time | grep "+++.*" | tee -a $log
	fi

	if [ ! -f $test.ic.out ]
	then
	    echo "*** Run failed" | tee -a $log
	    echo "*** $test.ic.out not found" | tee -a $log
	    ok=0
	    continue
	fi

	echo "+++ Diffing output"
	diff -q -w -a $test.ic.out $test.out > /dev/null

	if [ $? -eq 0 ]
	then
	    echo "+++ Output is correct" | tee -a $log
	else
	    ok=0
	    echo "*** Output is incorrect" | tee -a $log
	fi
    done
done

