#! /bin/sh

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

XGCC="$PIC30_CD/bin/xc16-gcc -mcpu=30F6015 -omf=elf"
GLD30="$PIC30_CD/bin/xc16-ld -omf=elf"
OBJDUMP="$PIC30_CD/bin/xc16-objdump -omf=elf"
READELF=$PIC30_CD/bin/xc16-readelf
LIBPATH=$PIC30_CD/lib

#
# END CONFIGURATION
#

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

rm -f t.exe
$XGCC -o t.exe -L$LIBPATH -g t1.c || exit 99
err=$?
if [ $vflag = "on" ]; then
    echo "$XGCC -o t.exe -L$LIBPATH -g t1.c"
    echo $err
fi

if [ $vflag = "on" ]; then
    echo $READELF -wp t.exe
    echo
fi

rm -f test.out
$READELF -wp t.exe | awk '/^[ ]+[0-9]+/ {print $2} ' > test.out || exit 99

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
