#! /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

# verify pic30-bin2hex
$PIC30_CD/bin/pic30-bin2hex t.exe > temp
if [ $vflag = "on" ]; then
    echo
    echo "$PIC30_CD/bin/pic30-bin2hex t.exe"
    echo $err
    echo
fi

# keep only the heading and eedata lines
grep section temp > test.out
grep '\-\-' temp >> test.out
grep eedata temp >> test.out
if [ $vflag = "on" ]; then
    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
