#! /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/xc16-gcc -mcpu=30F6015 -omf=elf"
OBJDUMP="$PIC30_CD/bin/xc16-objdump -omf=elf"


#
# 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 t.c -Tt.gld || exit 99
err=$?
if [ $vflag = "on" ]; then
    echo "$GCC30 -o t.exe t.c -Tt.gld"
    echo $err
fi

rm -f temp
$OBJDUMP -h t.exe > temp
err=$?
if [ $vflag = "on" ]; then
    echo
    echo "$OBJDUMP -h t.exe"
    echo $err
    cat temp
fi

perl -n -e 'if (/(\.const)\s+(\w+)\s+(\w+)\s+(\w+)/) {print "$1 $2 $3 $4\n"}' temp > test.out

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
