#! /bin/sh

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

GCC30="$PIC30_CD/bin/xc16-gcc -omf=elf"
#
# END CONFIGURATION
#

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

rm -f t1.out t2.out
$GCC30 t1.s -o t.exe -Tt.gld -Wa,-W -Wl,-Map=t.map,--no-check-sections,--report-mem > temp
err=$?
if [ $vflag = "on" ]; then
    echo
    echo "$GCC30 t1.s -o t.exe -Tt.gld -Wl,-Map=t.map,--no-check-sections,--report-mem"
    echo $err
    echo
    cat temp
fi

SIZE=`perl -n -e 'if (/Total "data" memory used \(bytes\):\s+(\w+)/) {print $1}' temp`

{
echo "total data memory used (should be 0):    $SIZE"
} > test.out

echo
echo `head -1 info.txt`

diff test.out expect.out
if [ $err -ne 0 ]; then
    echo "ERROR Detected!!"
    echo
    exit 99
fi

echo "All Tests Pass"
echo
exit 0
