#! /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
GAS30="$PIC30_CD/bin/xc16-as -W"
GLD30=$PIC30_CD/bin/xc16-ld
OBJDUMP=$PIC30_CD/bin/xc16-objdump

BIN2HEX=$PIC30_CD/bin/xc16-bin2hex
SIM30=$PIC30_CD/bin/sim30

#
# END CONFIGURATION
#

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

rm -f t1.o
$GAS30 t1.s -o t1.o
err=$?
if [ $vflag = "on" ]; then
    echo
    echo "$GAS30 t1.s -o t1.o"
    echo $err
    echo
fi

rm -f t.exe
$GLD30 t1.o -o t.exe -Map=t.map --script=t.gld
err=$?
if [ $vflag = "on" ]; then
    echo
    echo "$GLD30 t1.o -o t.exe -Map=t.map --script=t.gld"
    echo $err
    echo
fi

rm -f temp
$OBJDUMP -h t.exe > temp
err=$?
if [ $vflag = "on" ]; then
    echo
    echo "$OBJDUMP -h t.exe"
    echo $err
    cat temp
    echo
fi

# lift some info from objdump, but not the section file offset which tends to change
LINE1=`awk '/bss_7600/' temp`
LINE1=`echo $LINE1 | perl -n -e 'if (/(.*)\s+\w+\s+2\*\*1$/) {print "$1"}' -`
LINE2=`awk '/ALLOC, ABSOLUTE/' temp`
LINE3=`awk '/bss_dma/' temp`
LINE3=`echo $LINE3 | perl -n -e 'if (/(.*)\s+\w+\s+2\*\*1$/) {print "$1"}' -`
LINE4=`awk '/ALLOC, DMA/' temp`

{
echo
echo "excerpt from xc16-objdump (-h option):"
echo
echo "$LINE1"
echo "$LINE2"
echo "$LINE3"
echo "$LINE4"
echo
} > test.out

echo
echo `head -1 info.txt`

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

echo "All Tests Pass"
echo
exit 0
