#!/bin/bash

source scripts/config

progpath=$1
shift
bzreaxoption="$*"
hept_comp_opts="--heptc ../${COMPILER_DIR}/${COMPILER} --ctrl2ept ${CTRL2EPT} -v"

# run the program: no by default
run=0

cp $progpath $checkdir

HEPTLIB=../../lib
export HEPTLIB

pushd $checkdir > /dev/null

heptprog=`basename $progpath`
heptroot=`basename $heptprog .ept`

if grep "node main()" $heptprog >/dev/null; then
    bzreaxoption="$bzreaxoption main -s"
    run=1
fi

# assert_node=$(eval grep CHECK $heptprog | awk '{ print $3 }')

# if [ -n "$assert_node" ]; then
#     bzreaxoption="$bzreaxoption --heptc-opts '-assert $assert_node'"
#     run=1
# fi

algo_sdc=$(eval grep SDC $heptprog | awk '{ print $3 }')
if [ -n "$algo_sdc" ]; then
    bzreaxoption="$bzreaxoption -a '$algo_sdc'"
fi

bzreaxoption="$bzreaxoption $hept_comp_opts"

echo $BZREAX $heptprog $bzreaxoption
if $BZREAX $heptprog $bzreaxoption; then
    if [[ $run == 1 ]]; then
	echo ./sim $NBSTEP
	if ./sim $NBSTEP; then
	    echo "Test successful."
	    res=0
	else
	    echo "Run failed."
	    res=1
	fi
    else
	echo "Test successful (compilation and sdc only; no run)."
	res=0
    fi
else
    echo "Compilation or sdc of $heptprog failed"
    res=1
fi

popd > /dev/null

exit $res
