#!/bin/sh

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

# 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
$PIC30_CD/bin/pic30-as -o t1.o t1.s
err=$?
if [ $vflag = "on" ]; then
    echo
    echo "$PIC30_CD/bin/pic30-as -o t1.o t1.s"
    echo $err
fi

$PIC30_CD/bin/pic30-as -o t2.o t2.s
err=$?
if [ $vflag = "on" ]; then
    echo
    echo "$PIC30_CD/bin/pic30-as -o t2.o t2.s"
    echo $err
fi

$PIC30_CD/bin/pic30-as -o t3.o t3.s
err=$?
if [ $vflag = "on" ]; then
    echo
    echo "$PIC30_CD/bin/pic30-as -o t3.o t3.s"
    echo $err
fi

# make an archive
$PIC30_CD/bin/pic30-ar r t.a t1.o t2.o
err=$?
if [ $vflag = "on" ]; then
    echo
    echo "$PIC30_CD/bin/pic30-ar r t.a t1.o t2.o"
    echo $err
fi

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

# verify pic30-objdump
$PIC30_CD/bin/pic30-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 "pic30-objdump PASS"
else
        echo "pic30-objdump FAIL"
fi
} >> test.out

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

# verify pic30-nm
$PIC30_CD/bin/pic30-nm 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 "pic30-nm      PASS"
else
        echo "pic30-nm      FAIL"
fi
} >> test.out

#verify pic30-ranlib
$PIC30_CD/bin/pic30-ranlib t.a
$PIC30_CD/bin/pic30-nm -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 "pic30-ranlib  PASS"
else
        echo "pic30-ranlib  FAIL"
fi
} >> test.out

#verify pic30-size
#$PIC30_CD/bin/pic30-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 "pic30-size    PASS"
#else
#        echo "pic30-size    FAIL"
#fi
#} >> test.out

#verify pic30-strings
$PIC30_CD/bin/pic30-strings -tx 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 "pic30-strings PASS"
else
        echo "pic30-strings FAIL"
fi
} >> test.out

#verify pic30-strip
$PIC30_CD/bin/pic30-strip t1.o
$PIC30_CD/bin/pic30-nm t1.o >temp 2>temp2
if [ $vflag = "on" ]; then
    echo
    cat temp
fi
grep "no symbols" temp2 > /dev/null
{
if [ $? -eq 0 ]; then
        echo "pic30-strip   PASS"
else
        echo "pic30-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
