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

BIN2HEX="$PIC30_CD/bin/xc16-bin2hex -omf=coff"
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 t.exe
$GCC30 t1.c -o t.exe > test.out 2>&1
err=$?
if [ $vflag = "on" ]; then
    echo
    echo "$GCC30 t1.c -o t.exe"
    cat test.out
    echo $err
    echo
fi

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

$OBJDUMP -s -j foo t.exe >> temp
err=$?
if [ $vflag = "on" ]; then
    echo
    echo "$OBJDUMP -s -j foo t.exe "
    echo $err
    cat temp
    echo
fi

LINE1=`awk '/foo/' temp`
LINE2=`awk '/111111/ { print $2,$3,$4,$5 }' temp`
LINE3=`awk '/boo/' temp`
LINE4=`awk '/2222/ { print $2,$3,$4,$5 }' temp`

{
echo
echo "excerpt from xc16-objdump (-s 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
