         .OUTPI ALFRES
         .ENTRY DIRBLD,BATRUN

;Mod to build directory
BLDE01   .BYTE "Error reading main directory",$9B,0
BLDE02   .BYTE "CREDIR failed for the above path",$9B,0
BLDE03   .BYTE "Output path cannot be built",$9B,0
CREMSG   .BYTE "CREDIR issued for:",$9B,0
WILD     .BYTE "*.*",$9B
DIROFF   .BYTE 0
BATRUN   .BYTE 1
;
DIRBLD   LDA #0
         STA DIROFF
         STA BATRUN ;Command buffer is invalid now
         JMP DIR010 ;Go try the dir first
DIR001   LDX DIROFF
DIR002   LDA IDIR,X  ;Look for first dir
         CMP #">"
         BEQ DIR006
         CMP #$9B
         BEQ DIR003
         INX
         BPL DIR002
DIR003   LDA DIROFF
         BEQ DIR004
         LDX #.LO.BLDE03
         LDY #.HI.BLDE03
         JSR SCRPRT
         JMP DIR005
DIR004   LDX #.LO.BLDE01
         LDY #.HI.BLDE01
         JSR SCRPRT
DIR005   PLA         ;Pull return address of stack
         PLA
         JMP DEXIT   ;Return to Dos
DIR006   STX DIROFF
DIR007   LDA IDIR,X  ;Copy this path
         STA OFILE,X
         DEX
         BPL DIR007
         LDX DIROFF
         INX         ;Point to next possible subdir
         STX DIROFF  ;Save this offset
         LDY #0
DIR008   LDA WILD,Y  ;Copy the wildcard
         STA OFILE,X
         INX
         INY
         CPY #4
         BCC DIR008
         LDX #D3
         LDA #.LO.OFILE
         LDY #.HI.OFILE
         JSR OPNDIR ;Try this
         PHP         ;Save the status
         LDX #D3
         JSR CLOSE
         PLP
         BMI DIR009 ;Try to create it
         JMP DIR010 ;See if we're done
DIR009   LDX DIROFF
         DEX
         DEX
         LDA IDIR,X
         CMP #":"    ;Don't do the root
         BEQ DIR004
         INX
         LDA #$9B
         STA OFILE,X ;Rip off the *.*
         LDA #0
         STA OFILE+1,X
         LDX #.LO.CREMSG
         LDY #.HI.CREMSG
         JSR SCRPRT
         LDX #.LO.OFILE
         LDY #.HI.OFILE
         JSR SCRPRT
         LDX #D3
         LDA #42     ;CREDIR Xio
         STA $0342,X
         LDA #.LO.OFILE
         STA $0344,X
         LDA #.HI.OFILE
         STA $0345,X
         LDA #128
         STA $0348,X
         LDA #0
         STA $0349,X
         STA $034A,X
         STA $034B,X
         JSR $E456   ;Go try it
         BPL DIR010
         LDX #.LO.BLDE02
         LDY #.HI.BLDE02
         JSR SCRPRT
         JMP DIR005 ;Say goodbye
DIR010   LDX #D3     ;Try the real one now
         LDA #.LO.IDIR
         LDY #.HI.IDIR
         JSR OPNDIR
         PHP
         LDX #D3
         JSR CLOSE
         PLP
         BMI DIR011
         CLC         ;Build was ok
         RTS
DIR011  JMP DIR001 ;Still not there
