#! /bin/sh

#
# CONFIGURATION SECTION
#
if [ -z "$PIC30_CD" ]; then
    echo "Environmental variable PIC30_CD must be set up.";
    exit 1;
fi

GCC30=$PIC30_CD/bin/pic30-gcc
GLD30=$PIC30_CD/bin/pic30-ld
OBJDUMP=$PIC30_CD/bin/pic30-objdump

BIN2HEX=$PIC30_CD/bin/pic30-bin2hex
SIM30=$PIC30_CD/bin/simpic30

#
# END CONFIGURATION
#

# process args
vflag=off
while [ $# -gt 0 ]
do
    case "$1" in
        -v)  vflag=on;;
    esac
    shift
done


rm -f t.exe
$GCC30 -o t.exe t1.c -mconst-in-data -Wl,--heap=512 || exit 99
err=$?
if [ $vflag = "on" ]; then
    echo "$GCC30 -o t.exe t1.c -mconst-in-data -Wl,--heap=512"
    echo $err
fi

rm -f t.hex
$BIN2HEX t.exe > /dev/null
err=$?
if [ $vflag = "on" ]; then
    echo
    echo "$BIN2HEX t.exe"
    echo $err
fi

rm -f test.out
$SIM30 run.cmd > /dev/null
err=$?
if [ $vflag = "on" ]; then
    echo
    echo "$SIM30 run.cmd"
    echo $err
fi
cp UartOut.txt test.out
rm -f UartOut.txt

echo
echo `head -1 info.txt`

if [ $vflag = "on" ]; then
    diff -b -B test.out expect.out
else
    diff -b -B test.out expect.out > /dev/null
fi


if [ $? -ne 0 ]; then
    echo "ERRORs Detected!!"
    echo
    exit 199
fi

echo "All Tests Pass"
echo
exit 0
