#! /bin/sh

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

GAS30=$PIC30_CD/bin/pic30-as
GLD30=$PIC30_CD/bin/pic30-ld
OBJDUMP=$PIC30_CD/bin/pic30-objdump


#
# END CONFIGURATION
#

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

rm -f t1.o
$GAS30 -o t1.o t1.s
err=$?
if [ $vflag = "on" ]; then
    echo
    echo "$GAS30 -o t1.o t1.s"
    echo $err
fi


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


rm -f test.out
if [ $vflag = "on" ]; then
    echo
    $OBJDUMP -d -j .ivt t.exe
    $OBJDUMP -d -j .aivt t.exe
fi
$OBJDUMP -d -j .ivt t.exe > test.out
$OBJDUMP -d -j .aivt t.exe >> 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
