#!/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=coff -W"
GLD30="$PIC30_CD/bin/xc16-ld -omf=coff"
GAR30="$PIC30_CD/bin/xc16-ar -omf=coff"
GNM30="$PIC30_CD/bin/xc16-nm -omf=coff"
STRINGS=$PIC30_CD/bin/xc16-strings
STRIP="$PIC30_CD/bin/xc16-strip -omf=coff"
RANLIB="$PIC30_CD/bin/xc16-ranlib -omf=coff"
OBJDUMP="$PIC30_CD/bin/xc16-objdump -omf=coff"

BIN2HEX="$PIC30_CD/bin/xc16-bin2hex -omf=coff"

#
# 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 t2.o t3.o t.a t.exe t.hex temp temp2 test.out

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

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

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

# make an archive
$GAR30 r t.a t1.o t2.o
err=$?
if [ $vflag = "on" ]; then
    echo
    echo "$GAR30 r t.a t1.o t2.o"
    echo $err
fi

# verify xc16-ar
$GLD30 -o t.exe t3.o t.a
err=$?
if [ $vflag = "on" ]; then
    echo
    echo "$GLD30 -o t.exe t3.o t.a"
    echo $err
fi
{
if [ $err -eq 0 ]; then
        echo "xc16-ar      PASS"
else
        echo "xc16-ar      FAIL"
fi
} > test.out

# verify xc16-objdump
$OBJDUMP -d -j .text t.exe > temp
if [ $vflag = "on" ]; then
    echo
    cat temp
    diff -w temp expect1
fi
diff -w temp expect1 > /dev/null
{
if [ $? -eq 0 ]; then
        echo "xc16-objdump PASS"
else
        echo "xc16-objdump FAIL"
fi
} >> test.out

# verify xc16-bin2hex
$BIN2HEX t.exe -v | head -14 > temp
if [ $vflag = "on" ]; then
    echo
    cat temp
    diff -w temp expect2
fi
diff -w temp expect2 > /dev/null
{
if [ $? -eq 0 ]; then
        echo "xc16-bin2hex PASS"
else
        echo "xc16-bin2hex FAIL"
fi
} >> test.out

# verify xc16-nm
$GNM30 t.a > temp
if [ $vflag = "on" ]; then
    echo
    cat temp
    diff -w temp expect3
fi
diff -w temp expect3 > /dev/null
{
if [ $? -eq 0 ]; then
        echo "xc16-nm      PASS"
else
        echo "xc16-nm      FAIL"
fi
} >> test.out

#verify xc16-ranlib
$RANLIB t.a
$GNM30 -s t.a > temp
if [ $vflag = "on" ]; then
    echo
    cat temp
    diff -w temp expect4
fi
{
diff -w temp expect4 > /dev/null
if [ $? -eq 0 ]; then
        echo "xc16-ranlib  PASS"
else
        echo "xc16-ranlib  FAIL"
fi
} >> test.out

#verify xc16-size
#$PIC30_CD/bin/xc16-size -A t.exe > temp
#if [ $vflag = "on" ]; then
#    echo
#    cat temp
#    diff -w temp expect5
#fi
#diff -w temp expect5 > /dev/null
#{
#if [ $? -eq 0 ]; then
#        echo "xc16-size    PASS"
#else
#        echo "xc16-size    FAIL"
#fi
#} >> test.out

#verify xc16-strings
$STRINGS -tx -a -f t1.o > temp
if [ $vflag = "on" ]; then
    echo
    cat temp
    diff -w temp expect6
fi
diff -w temp expect6 > /dev/null
{
if [ $? -eq 0 ]; then
        echo "xc16-strings PASS"
else
        echo "xc16-strings FAIL"
fi
} >> test.out

#verify xc16-strip
$STRIP t1.o
$GNM30 t1.o >temp 2>temp2
if [ $vflag = "on" ]; then
    echo
    cat temp
fi
grep "no symbols" temp2 > /dev/null
{
if [ $? -eq 0 ]; then
        echo "xc16-strip   PASS"
else
        echo "xc16-strip   FAIL"
fi
} >> test.out


# 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
