#! /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/xc16-as -omf=elf -W"
GLD30="$PIC30_CD/bin/xc16-ld -omf=elf"
BIN2HEX="$PIC30_CD/bin/xc16-bin2hex -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

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

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

# link it
$GLD30 -o t.exe t1.o 
err=$?
if [ $vflag = "on" ]; then
    echo
    echo "$GLD30 -o t.exe t1.o"
    echo $err
fi

# verify xc16-objdump
$OBJDUMP -D -j foo t.exe > test.out
if [ $vflag = "on" ]; then
    echo
    echo "$OBJDUMP -D -j foo t.exe"
    echo $err
    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
