#! /bin/sh

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

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

# remove temporary files
rm -f t1.o t.exe t.hex test.out

# assemble the source file
$PIC30_CD/bin/pic30-as -o t1.o t1.s
err=$?
if [ $vflag = "on" ]; then
    echo
    echo "$PIC30_CD/bin/pic30-as -o t1.o t1.s"
    echo $err
fi

# link it
$PIC30_CD/bin/pic30-ld -o t.exe t1.o
err=$?
if [ $vflag = "on" ]; then
    echo
    echo "$PIC30_CD/bin/pic30-ld -o t.exe t1.o"
    echo $err
fi

# create a hex file
$PIC30_CD/bin/pic30-bin2hex t.exe > /dev/null
if [ $vflag = "on" ]; then
    echo
    echo "$PIC30_CD/bin/pic30-bin2hex t.exe"
    echo $err
fi

# examine the data record for .text
grep \:100200 t.hex > test.out
if [ $vflag = "on" ]; then
    echo
    echo "grep \:100200 t.hex"
    echo $err
    echo
    echo "Data record for section .text:"
    echo
    cat test.out
fi

# print the header
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
