#!/bin/sh
#
# RoBIOS batch file for gnu-68000-asm

# Need to do this before we reset the PATH below
files=`echo $*|tr ' ' "\n"|grep -v ^-|tr "\n" ' '`
opts=`echo $*|tr ' ' "\n"|grep ^-|tr "\n" ' '`

# apapt the following path if necessary
export basedir=/user/res/uwarobot
export gccdir=$basedir/g++-m68k
export gccparts=$gccdir/lib/gcc-lib/m68k-coff/2.95.2

# Find the RoBIOS includes and libraries based on path to script.
cmddir=`dirname $0`
cmddir2=`dirname $cmddir`
# One or the other - hopefully. :-)
if test -d $cmddir2/include; then
  export mc=`dirname $cmddir`
fi
if test -d $cmddir2/mc/include; then
  export mc=`dirname $cmddir`/mc
fi
export libc=$mc/libc/include

export PATH=$gccdir/bin:/bin

export CC="m68k-coff-gcc -B$gccparts/"
export WARN="-W -Wall -ansi -pedantic"
export CPU=-m68332
export LIBS="-limpro -lmym -lmyc -lmygcc -lrobi"
export TMP=/tmp
export LDSCRIPT=-T$mc/ldfiles/robi-ram.ld

export CFLAGS="$WARN -I$mc/include -I$libc  $CPU -O -msoft-float  $opts"

# run gcc for a list of .c source files

for file in $files; do
    name=`basename $file .c`
    tmpfile=$TMP/$name

    echo FILE $file
    echo c-compiler
    if $CC -S -c $file $CFLAGS
    then true; else
      echo "Error: c-compiler failed"
    fi

    echo 68332-assembler
    if m68k-coff-as -o $tmpfile.o $name.s
    then true; else
      echo "Error: 68332-assembler failed"
    fi

    echo linker
    if m68k-coff-ld $LDSCRIPT -L$mc/lib -o $tmpfile.l $tmpfile.o $opts $LIBS 
    then true; else
      echo "Error: linker failed"
    fi

    echo obj-copy
    if m68k-coff-objcopy -O srec $tmpfile.l $name.hex
    then true; else
      echo "Error: obj-copy failed"
    fi

    /bin/rm $name.s $tmpfile.l $tmpfile.o

    echo " "
done

