#! /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/pic30-gcc
GLD30=$PIC30_CD/bin/pic30-ld
OBJDUMP=$PIC30_CD/bin/pic30-objdump
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
    $OBJDUMP -g t.exe || exit 99
    echo
fi

rm -f test.out
$OBJDUMP -g t.exe > temp.out
# remove the lines with file names and braces
grep -v "file" temp.out > temp2.out
grep -v "t1.c" temp2.out > temp3.out
grep -v "p30f2010.inc" temp3.out > temp4.out
sed -e '/crt0.s/d' -e '/[{}]/d' < temp4.out > temp5.out
# keep only the function information
head -9 temp5.out > test.out
rm temp*.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
