#!/bin/sh
#
# Usage:
#
#       asm file.s [-l file1.o file2.o ...] [-m]
#
# -m option turns on assembler listing ("file.lis") and
#       linker mapfile ("file.map")
#
# ard, Jan 94.
# modified heavily by Dean Armstrong (daa1@waikato.ac.nz) 27/02/2001.
# and again on 3/5/01 to place output files in working directory, not
# directory of input file

######################################################################
usage() {
        more << EOF
USAGE:
        $ME file.s [-l] [file1.o file2.o ...] [ -m ] [ -i | -r ]

OPTIONS

        -l  Produce a downloadable file, possibly linking
            with other object files.

        -m  Generate assembler listing ("file.lis") and
            linker mapfile ("file.map").

EOF
}

######################################################################

ME=`basename $0`
LISTING=
MAP=

if [ $# -lt 1 ] ; then
        usage
        exit 1
fi

#  echo `date` `whoami` $* >> /home/tonym/asm-log

#
# Strip off the sourcefile suffix to find a base filename.
#
SOURCE=$1
shift
BASE=`basename ${SOURCE}`
#BASE=`dirname ${SOURCE}`/`echo ${BASE} | sed "s/\.[a-zA-Z]*//g"`
BASE=`echo ${BASE} | sed "s/\.[a-zA-Z]*//g"`
EXT=`echo ${SOURCE} | sed "s/.*\.//g"`
FULL=`echo ${SOURCE} | sed "s/\.[a-zA-Z]*//g"` 

#
# Scan for a listing/mapfile option
#
for arg do
        case $arg in 
        -m)
                LISTING=-ahls
                MAP="-Map ${BASE}.map";;
	-1)
		echo "$ME: Bad argument -1. Maybe you mean -l"
		exit 1
        esac
done



#
# Check suitability of files.
#
if [ ! -r ${SOURCE} ] ; then
        echo "$ME: Can't read source file \"${SOURCE}\""
        exit 1
fi
#if [ ! -w ${BASE}.o ] ; then
#       echo "$ME: Can't write object file \"${BASE}.o\""
#       exit 1
#fi

#
# Assemble it
#

echo "Assembling \"${SOURCE}\" to \"${BASE}.o\""
if [ ${LISTING} ] ; then
        echo "Listing file is \"${BASE}.lis\""
        mips-as -g2 ${LISTING} -o ${BASE}.o -G 0 ${SOURCE} > ${BASE}.lis
        ret=$?
else
        mips-as -g2 ${LISTING} -o ${BASE}.o -G 0 ${SOURCE} 
        ret=$?
fi


if [ $ret -ne 0 ] ; then
        echo "Assembler returned exit code $ret - aborting."
        exit 1
fi

####################
# Now link, if asked.

if [ "x${1-x}" = "x-l" ] ; then
        shift
        LINKFILES=${BASE}.o
        while [ "$1" != "" ] ; do
                if [ "${1}" != "-m" ] ; then
                  if [ "${1}" != "-i" ] ; then
                    if [ "${1}" != "-r" ] ; then
			LINKFILES="${LINKFILES} $1"
		    fi
                  fi
                fi
                shift
        done

        echo "Linking ${LINKFILES} to \"${BASE}.aout\""
        if [ "$MAP" ] ; then
                echo "Map file is \"${BASE}.map\""
        fi

        mips-ld -Ttext 0xa0010000 -e main ${LINKFILES}  -o ${BASE}.aout ${MAP}
        ret=$?
        if [ $ret -ne 0 ] ; then
                echo "Linker returned exit code $ret - aborting."
                exit 1
        fi

        echo "Converting \"${BASE}.aout\" to downloadable \"${BASE}\""
	mips-objcopy -O srec ${BASE}.aout ${BASE}
        ret=$?
        if [ $ret -ne 0 ] ; then
                echo "Converter returned exit code $ret - aborting."
                exit 1
        else
		rm -f ${BASE}.aout
	fi
        
fi

