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

BIN2HEX="$PIC30_CD/bin/xc16-bin2hex -omf=elf"
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
err=$?
if [ $vflag = "on" ]; then
    echo
    echo "$GLD30 t1.o -o t.exe -Map=t.map"
    echo $err
    echo
fi

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

LINE0=`awk '/Name/' temp`
LINE1=`awk '/data_merge/' temp`
LINE15=`awk '/Key/' temp`
LINE2=`awk '/\(merge\)/' temp`

{
echo
echo "excerpt from pic30-readelf (-S option):"
echo
echo "$LINE0"
echo "$LINE1"
echo "$LINE15"
echo "$LINE2"
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
