NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF bin/mkdir
*/-------------------------------------
* # MKDIR
* Create directories
* ## Arguments
* **-M mode**
* Apply specified mode to created directories
* **-P**
* Create parent directories if required
* **-V**
* Verbose mode
* ## Return Value
*\-------------------------------------
				.INB inc/macros.i
				.INB inc/a2osx.i
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPPtr1			.BS 2
ZPMode			.BS 2
ArgIndex		.BS 1
ArgLen			.BS 1
bParent			.BS 1
bVerbose		.BS 1
ZS.END			.ED
*--------------------------------------
*			File Header (16 Bytes)
*--------------------------------------
CS.START		cld
				jmp (.1,x)
				.DA #$61				6502,Level 1 (65c02)
				.DA #1					BIN Layout Version 1
				.DA 0
				.DA CS.END-CS.START		CS
				.DA DS.END-DS.START		DS
				.DA #16					SS
				.DA #0					ZP
				.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------
.1				.DA CS.INIT
				.DA CS.RUN
				.DA CS.DOEVENT
				.DA	CS.QUIT
L.MSG.USAGE		.DA MSG.USAGE
L.MSG.DIR		.DA MSG.DIR
L.MSG.OK	    .DA MSG.OK	
L.MSG.ERR       .DA MSG.ERR
				.DA 0
*--------------------------------------
CS.INIT
CS.QUIT			clc
				rts
*--------------------------------------
CS.RUN			>LDYAI S.STAT.MODE.FO+S.STAT.MODE.FG+S.STAT.MODE.FU
				>STYA ZPMode

.1				jsr CS.RUN.GetNextArg
				bcs .8

				lda (ZPPtr1)
				cmp #'-'
				bne .6

				ldy #1
				lda (ZPPtr1),y
				beq .9

.2				ldx #OptionVars-OptionList-1

.3				cmp OptionList,x
				beq .5

				dex
				bpl .3

				cmp #'M'
				beq .4

				cmp #'m'
				bne .9

.4				jsr CS.RUN.GetNextArg
				bcs .9

				jsr CS.RUN.Str2Mode
				bcc .1

				bra .9

.5				lda OptionVars,x
				tax
*				sec
				ror $0,x

				iny
				lda (ZPPtr1),y
				bne .2

				bra .1

.6				lda (pData)
				bne .9

				>PUSHW ZPPtr1
				>PUSHW pData
				>SYSCALL RealPath
				bcc .1

.9				>LDYA L.MSG.USAGE
				>SYSCALL PutS

				lda #E.SYN
				sec
				rts

.8				lda (pData)
				beq .9
*--------------------------------------
CS.RUN.Create	bit bParent
				bmi CS.RUN.CreateP
				
				jsr CS.RUN.Create1
				bcs .9

				lda #0
				sec
.9				rts
*--------------------------------------
CS.RUN.CreateP	ldy #$ff

.1				iny
				lda (pData),y
				bne .1

				sty ArgLen
				
				ldy #0					skip /
				
.2				iny
				lda (pData),y
				beq .8					/VOL
				
				eor #'/'
				bne .2
				
.3				iny						/VOL/
				lda (pData),y
				beq .4					/VOL/DIR
				
				eor #'/'
				bne .3
				
.4				sty ArgIndex			/VOL/DIR/

				sta (pData),y			/VOL/DIR

				>PUSHW pData
				>PUSHEA.G StatBuf
				>SYSCALL Stat
				bcc .5

				jsr CS.RUN.Create1
				bcs .9
				
.5				ldy ArgIndex
				cpy ArgLen
				beq .8

				lda #'/'
				sta (pData),y			/VOL/DIR/.....
				bra .3

.8				lda #0
				sec
.9				rts
*--------------------------------------
CS.RUN.Create1	clc				
				bit bVerbose
				bpl .1
				
				>PUSHW L.MSG.DIR
				>PUSHW pData
				>PUSHBI 2
				>SYSCALL PrintF

.1				>PUSHW pData
				>PUSHW ZPMode
				>SYSCALL MKDir

				bit bVerbose
				bpl .8

				bcs .2
				
				>LDYA L.MSG.OK
				>SYSCALL PutS

				clc
				rts

.2				pha
				>PUSHW L.MSG.ERR
				pla
				pha
				>PUSHA
				>PUSHBI 1
				>SYSCALL PrintF
				pla
				sec

.8				rts
*--------------------------------------
CS.RUN.GetNextArg
				inc ArgIndex

				lda ArgIndex
				>SYSCALL ArgV
				bcs .9

				>STYA ZPPtr1

.9				rts
*--------------------------------------
CS.RUN.Str2Mode	ldy #$ff

.1				iny
				lda (ZPPtr1),y
				bne .1

				cpy #4					must be "765" max
				bcs .9

				stz ZPMode
				stz ZPMode+1

				ldy #0

.2				lda (ZPPtr1),y
				beq .8
				cmp #'0'
				bcc .9

				cmp #'7'+1
				bcs .9

				asl ZPMode
				rol ZPMode+1
				asl ZPMode
				rol ZPMode+1
				asl ZPMode
				rol ZPMode+1
				and #7
				clc
				adc ZPMode
				sta ZPMode
				bcc .3

				inc ZPMode+1

.3				iny
				bra .2

.8				clc
				rts

.9
*--------------------------------------
CS.DOEVENT		sec
				rts
*--------------------------------------
CS.END
*--------------------------------------
OptionList		.AS "PpVv"
OptionVars		.DA #bParent,#bParent
				.DA #bVerbose,#bVerbose
*--------------------------------------
MSG.USAGE		.CS "Usage : MKDIR newdir\r\n"
				.CS "   -M mode : set mode\r\n"
				.CS "   -P : Make parent directories as needed\r\n"
				.CZ "   -V : Verbose"
MSG.DIR			.CZ "MKDir:%s..."
MSG.OK			.CZ "[OK]"
MSG.ERR			.CS "[%h]"
MSG.CRLF		.CZ "\r\n"				
*--------------------------------------
				.DUMMY
				.OR 0
DS.START		.BS 128					Path buffer
StatBuf			.BS S.STAT
DS.END			.ED
*--------------------------------------
MAN
SAVE usr/src/bin/mkdir.s
ASM
