.key arg1,arg2,arg3

; ** compile, assemble and link an ACE program **

; 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.
 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:$source

   IF NOT ERROR
     ; assemble and link
     delete >NIL: T:$source
     phxass T:<arg2>.s
     delete >NIL: T:<arg2>.s
     phxlnk from T:<arg2>.o <arg3> ACElib:startup.lib ACElib:db.lib ACElib:ami.lib to T:<arg2> SC SD ND

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

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

 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 -iO T:$source

   IF NOT ERROR
     ; assemble and link.
     delete >NIL: T:$source
     phxass T:<arg1>.s
     delete >NIL: T:<arg1>.s
     phxlnk from T:<arg1>.o <arg2> ACElib:startup.lib ACElib:db.lib ACElib:ami.lib to T:<arg1> SC SD ND

     ; 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
