.key arg1,arg2,arg3

; ** Preprocess, compile, assemble and link an ACE program **
; ** using PhxAss and PhxLnk for the last two stages. **

; 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.

 IF EXISTS <arg2>.bas
   ;try ".bas" extension
   set source <arg2>.bas
 ELSE
   ;assume ".b" extension
   set source <arg2>.b  
 ENDIF

 ; preprocess source file.
 app $source ram:t/$source
 IF NOT ERROR

   ; compile preprocessed source file.
   ace <arg1> ram:t/$source

   IF NOT ERROR
     ; assemble and link
     delete >NIL: ram:t/$source
     PhxAss ram:t/<arg2>.s
     delete >NIL: ram:t/<arg2>.s
     PhxLnk ram:t/<arg2>.o <arg3> ACElib:startup.lib ACElib:db.lib ACElib:ami.lib SMALLCODE SMALLDATA

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

     IF EXISTS ram:t/<arg2>.info
       copy ram:t/<arg2>.info ""
     ENDIF

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

 ENDIF

ELSE

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

 IF EXISTS <arg1>.bas
   ;try ".bas" extension
   set source <arg1>.bas
 ELSE
   ;assume ".b" extension
   set source <arg1>.b  
 ENDIF

 app $source ram:t/$source

 IF NOT ERROR

   ; compile source file.
   ace ram:t/$source

   IF NOT ERROR
     ; assemble and link.
     delete >NIL: ram:t/$source
     PhxAss ram:t/<arg1>.s
     delete >NIL: ram:t/<arg1>.s
     PhxLnk ram:t/<arg1>.o <arg2> ACElib:startup.lib ACElib:db.lib ACElib:ami.lib SMALLCODE SMALLDATA

     ; leave us with the executable (and icon?).
     copy ram:t/<arg1> ""

     IF EXISTS ram:t/<arg1>.info
       copy ram:t/<arg1>.info ""
     ENDIF

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

ENDIF
