#!/bin/sh
#
# YesCREW GFA-NextGeneration CLI script v0.2
# So far tested only on Falcon/MiNT OS. There 
# should be no problems making it to work on 
# compatibles runing MiNT or Mupfel shell in
# Magic(XX).
#
# build.gfa: Make all from *.lst files
#
# Usage:
# build.gfa foobar will load and process foobar.lst!
#
#

#  *** WARNING: DON'T use fileanme extensions!!***



# Some ENVOIRMENT settings

# Library path. Required by GFA Linker 
#
#  using library path from gfalink.opt file
#GFAlib="+/usr/local/lib/GFAlib/gfalib.lib"

#  Read options from project folder
GFA_compiler_options=`tail -n 1 ./gfacomp.opt`
GFA_linker_options=`tail -n 1 ./gfalink.opt`

# Compiler and Linker options setups. Refer to GFA.HYP
# for detials and description . Now reads gfacomp.opt and
# gfalink.opt files in applications folder!

#Compiler options:
#    $%0     Variables are converted to floating point and then divided.
#    $%3     Always execute integer division as an integer division.
#    $mx     The program should only use x bytes of ram.
#    $*&     Longword multiplication with muls.
#    $*%     Longword multiplication without muls.
#    $F%     RETURN value of a FUCTION will be an integer.
#    $RC&    RC_INTERSECT() parameters treated as 2 byte values.
#    $RC%    RC_INTERSECT() parameters treated as 4 byte values.
#    $X name The routine name to be taken from a linked file.
#    $U      Check for Control+Shift+Alternate, EVERY/AFTER once.
#    $U+     Check for Control+Shift+Alternate, EVERY/AFTER after each instruction.
#    $U-     Test checking of Control+Shift+Alternate, EVERY/AFTER.
#    $I+     Enable interrupt routines.
#    $I-     Disable interrupt routines.
#    $S&     Treat SELECT-CASE parameters as two-byte values.
#    $S%     Treat SELECT-CASE parameters as four-byte values.
#    $S>     Optimise SELECT-CASE for execution time.
#    $S<     Optimise SELECT-CASE for program length.
#    $E$     ERROR messages displayed as text.
#    $E#     ERROR messages displayed as numbers.
#    $B+     ERROR number instead of bombs.
#    $P>     Subroutines to be compiled as GFA BASIC subroutines.
#    $P<     Subroutines to be compiled as 68000 subroutines.
#    $F>     Switch ENDFUNC checking on.
#    $F<     Switch ENDFUNC checking off.
#    $C+     Save registers A3 to A6 on stack
#    $C-     Do not save registers A3 to A6 on stack
#    $N+     Switch FOR-NEXT loop checking on.
#    $N-     Switch FOR-NEXT loop checking off.
#    $%6     Round integers up.
#GFA_compiler_options="G3OBJ=test.o G3MOVE"


# Linker options:
#   -s     Append symbol table.
#    +NAME  Use library NAME instead of GFA3BLIB.
#    NAME   Include object file NAME.O.
#    #      Do not include TEST.O.
#    -T     Produce code to run in TT ram
#GFA_linker_options="-s"



# Here we go....

# Edit source file
pico $1.lst

# Add CR to suit CRLF only handling of lst2gfa
crlf -a $1.lst

# Tokenize LST to GFA
lst2gfa $1.lst

# Create noname.o in current folder. Its necesary to have that file
# and atm i dont know if it can be avoided.
touch noname.o

# Compiling
echo
echo "Compiling....."
echo $GFA_compiler_options
gfacompiler $1.gfa $GFA_compiler_options
echo "done."

# Linking
echo
echo "Linking....."

# Disabled. Using project  settings instead!
#gfalinker test $GFAlib $GFA_linker_options

gfalinker test $GFAlib $GFA_linker_options
echo 'done.'
echo

# Removing Object files
echo
echo "Removing Object files..."
rm noname.o
rm test.o
echo "done."
echo

# Renaming result
# WARNING! ONLY USE THIS ON UNIX FS or ull face loosing source
echo "Renaming and setting access permissions..."
echo "Renaming noname.app > "$1.prg
mv noname.app $1.prg
echo "Setting permissions > "$1.prg
chmod 755 $1.prg
echo 'done.'
echo

# end action
if [ $2 = 'run' ] ; then
   echo 'starting '$1'.....'
   echo
   ./$1.prg
fi

if [ $2 = 'compile' ] ; then
   echo 'compiled only.'
   echo
fi