#/bin/bash

if [ $# -ne 2 ]
then
    echo Usage $0 elf-file output-file
    exit 1
fi

echo -n Loading $1 to device ...
nios2-download -g $1 > /dev/null
err=$?
if [ ${err} -ne 0 ]
then
    echo " [ FAIL ]"
    exit ${err}
fi
echo " [ OK ]"

echo -n Reading outputs from device via nios2-terminal ...
nios2-terminal -q > $2 &
terminal_pid=$!
kill -CONT ${terminal_pid}

while ! tail -n1 $2 | grep "test done" > /dev/null
do
    true
done

kill ${terminal_pid}

echo " [ OK ]"

