#!/bin/bash
OPT="0"
if [ "$1" == "-O" ] ; then
    # possible options might include:
    # LIST CODE FIXED PROFILE NOTRACE NOLINE NOCHECK MIPS2 PPRO PPRO2 PPROONLY CHECK NODIAG
    # NOARRAY ARRAY PARMX PARMY PARMZ QUOTES LINE MINWORK BIGWORK MAXWORK MAXDICT SCAN VERSION

    # The MAXWORK,MAXDICT,NOCHECK,MAXSTACK options just increase compiler table size and allow
    # us to compile larger source files.  These don't affect the generated code, and are safe
    # to enable for all compilations on a modern system with much larger memory available than
    # the machines this was built for in the 90's.
    
    shift
    OPT="1"
fi

if [ "$1" == "" ] ; then
    echo "syntax: i80 [-O] filename.i (or filename.imp)"
    exit 1
fi

if [ "$1" == "-h" ] ; then
    echo "syntax: i80 [-O] filename.i (or filename.imp)"
    echo "        i80 generates a .o file, it does not link it"
    echo "        The only option is -O to generate optimised code"
    echo "        A listing file is written to filename.lis"
    echo "        For now the assembly source code is saved in filename.s"
    exit 0
fi

case "$1" in
  *.i)
      # an imp file of the form filename.i:
      
      if [ "$OPT" == "1" ] ; then
        rm -f ./`basename "${1%.*}".s`
        # first generate a listing file:
        WINEDEBUG=-all wineconsole /home/gtoal/src/compilers101/imp80/cygwin/imp80.exe $1 LIST,MAXWORK  > /dev/null 2> /dev/null
        if [ ! -f `basename "${1%.*}".s` ] ; then
            # Compilation failed.  Do it again but show the user the error messages this time
            WINEDEBUG=-all wineconsole /home/gtoal/src/compilers101/imp80/cygwin/imp80.exe $1 MAXWORK \
                | /home/gtoal/src/compilers101/imp80/cygwin/ansi_screen_filter \
                | grep -v "^$"
            exit 1
        fi
        mv `basename "${1%.*}".s` `basename "${1%.*}".lis`
        ecce `basename "${1%.*}".lis` --command '(r?v/.file "/\pm)0l0rlk0;%c' 2> /dev/null
        
        # now do the compilation with all optimisations on:
        WINEDEBUG=-all wineconsole /home/gtoal/src/compilers101/imp80/cygwin/imp80.exe -O $1 MAP,PPRO,NOTRACE,NOLINE,NOARRAY,NODIAG,NOCHECK,MAXDICT,MAXWORK \
            | /home/gtoal/src/compilers101/imp80/cygwin/ansi_screen_filter \
            | grep -v "^$"
      else
        rm -f ./`basename "${1%.*}".s` ./`basename "${1%.*}".s`
        WINEDEBUG=-all wineconsole /home/gtoal/src/compilers101/imp80/cygwin/imp80.exe $1 LIST,MAXWORK  > /dev/null 2> /dev/null
        if [ ! -f `basename "${1%.*}".s` ] ; then
            WINEDEBUG=-all wineconsole /home/gtoal/src/compilers101/imp80/cygwin/imp80.exe $1 MAXWORK \
                | /home/gtoal/src/compilers101/imp80/cygwin/ansi_screen_filter \
                | grep -v "^$"
            exit 1
        fi
        mv `basename "${1%.*}".s` `basename "${1%.*}".lis`
        ecce `basename "${1%.*}".lis` --command '(r?v/.file "/\pm)0l0rlk0;%c' 2> /dev/null

        # now do the compilation:
        WINEDEBUG=-all wineconsole /home/gtoal/src/compilers101/imp80/cygwin/imp80.exe $1 MAXWORK,MAXDICT,NOCHECK,MAXSTACK \
            | /home/gtoal/src/compilers101/imp80/cygwin/ansi_screen_filter \
            | grep -v "^$"
      fi
      gcc -m32 -c `basename "${1%.*}".s`
      exit 0
      ;;
  
  *.imp)
      # an imp file of the form filename.imp:
      
      if [ "$OPT" == "1" ] ; then
        rm -f ./`basename "${1%.*}".ims`
        # first generate a listing file:
        WINEDEBUG=-all wineconsole /home/gtoal/src/compilers101/imp80/cygwin/imp80.exe $1 LIST,MAXWORK  > /dev/null 2> /dev/null
        if [ ! -f `basename "${1%.*}".ims` ] ; then
            # Compilation failed.  Do it again but show the user the error messages this time
            WINEDEBUG=-all wineconsole /home/gtoal/src/compilers101/imp80/cygwin/imp80.exe $1 MAXWORK \
                | /home/gtoal/src/compilers101/imp80/cygwin/ansi_screen_filter \
                | grep -v "^$"
            exit 1
        fi
        mv `basename "${1%.*}".ims` `basename "${1%.*}".lis`
        ecce `basename "${1%.*}".lis` --command '(r?v/.file "/\pm)0l0rlk0;%c' 2> /dev/null
        
        # now do the compilation with all optimisations on:
        WINEDEBUG=-all wineconsole /home/gtoal/src/compilers101/imp80/cygwin/imp80.exe -O $1 MAP,PPRO,NOTRACE,NOLINE,NOARRAY,NODIAG,NOCHECK,MAXDICT,MAXWORK \
            | /home/gtoal/src/compilers101/imp80/cygwin/ansi_screen_filter \
            | grep -v "^$"
      else
        rm -f ./`basename "${1%.*}".ims` ./`basename "${1%.*}".s`
        # first generate a listing file:
        WINEDEBUG=-all wineconsole /home/gtoal/src/compilers101/imp80/cygwin/imp80.exe $1 LIST,MAXWORK  > /dev/null 2> /dev/null
        if [ ! -f `basename "${1%.*}".ims` ] ; then
            WINEDEBUG=-all wineconsole /home/gtoal/src/compilers101/imp80/cygwin/imp80.exe $1 MAXWORK \
                | /home/gtoal/src/compilers101/imp80/cygwin/ansi_screen_filter \
                | grep -v "^$"
            exit 1
        fi
        mv `basename "${1%.*}".ims` `basename "${1%.*}".lis`
        ecce `basename "${1%.*}".lis` --command '(r?v/.file "/\pm)0l0rlk0;%c' 2> /dev/null
        
        # now do the compilation:
        WINEDEBUG=-all wineconsole /home/gtoal/src/compilers101/imp80/cygwin/imp80.exe $1 MAXWORK,MAXDICT,NOCHECK,MAXSTACK \
            | /home/gtoal/src/compilers101/imp80/cygwin/ansi_screen_filter \
            | grep -v "^$"
      fi
      mv `basename "${1%.*}".ims` `basename "${1%.*}".s`
      gcc -m32 -c `basename "${1%.*}".s`
      exit 0
    ;;
  *)
      echo "i80: input file must end in .i or .imp"
      exit 1
    ;;
esac
