.key arg1,arg2,arg3

; ** Preprocess, compile, assemble and link an ACE program using **
; ** ACPP rather than APP as a preprocessor. Could use NAP instead. **

; make sure stack is big enough.
STACK 40000

; don't abort script if ACE or APP quits 
; with a return code of 10 (ERROR).
FAILAT 11

IF <arg2> GT ""

 ; at least 2 arguments: <arg1> = compiler options.
 ;                       <arg2> = ACE source file.
 ;                       <arg3> = extra object module/library to link.

 ; preprocess source file.
 acpp <arg2>.b T:<arg2>.beta
 IF NOT ERROR
  ; remove preprocessor-inserted information lines.
  removeline t:<arg2>.beta t:<arg2>.b
  IF NOT ERROR
   delete >NIL: T:<arg2>.beta
   ; compile preprocessed source file.
   ace <arg1> T:<arg2>

   IF NOT ERROR
     ; assemble and link
     delete >NIL: T:<arg2>.b
     a68k T:<arg2>.s
     delete >NIL: T:<arg2>.s
     blink from T:<arg2>.o LIB <arg3>+ACElib:startup.lib+ ACElib:db.lib+
                              ACElib:ami.lib SMALLCODE SMALLDATA

     ; leave us with the executable (and icon?).
     copy T:<arg2> ""

     IF EXISTS T:<arg2>.info
       copy T:<arg2>.info ""
     ENDIF

     ; kill any remaining temporary files.
     delete >NIL: T:<arg2>#?
   ENDIF

  ENDIF
 ENDIF
ELSE 

 ; no compiler options: <arg1> = ACE source file.
 ;                      <arg2> = extra object module/library to link.

 ; preprocess source file.
 acpp <arg1>.b T:<arg1>.beta
 IF NOT ERROR
  ; remove preprocessor-inserted information lines.
  removeline t:<arg1>.beta t:<arg1>.b
  IF NOT ERROR
   delete >NIL: t:<arg1>.beta
   ; compile source file.
   ace T:<arg1>

   IF NOT ERROR
     ; assemble and link.
     delete >NIL: T:<arg1>.b
     a68k T:<arg1>.s
     delete >NIL: T:<arg1>.s
     blink T:<arg1>.o LIB <arg2>+ACElib:startup.lib+ACElib:db.lib+
                              ACElib:ami.lib SMALLCODE SMALLDATA

     ; leave us with the executable (and icon?).
     copy T:<arg1> ""
   
     IF EXISTS T:<arg1>.info
       copy T:<arg1>.info ""
     ENDIF

     ; kill any remaining temporary files.
     delete >NIL: T:<arg1>#?
   ENDIF
  ENDIF 
 ENDIF

ENDIF
