NEW
  AUTO 3,1
				.LIST OFF
*--------------------------------------
X.RESET.SRC.DIR	.EQ 0					single pass
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPPtr1			.BS 2
ZPPtr2			.BS 2
ZPFileName		.BS 2
ZPFileStat		.BS 2

bContinue		.BS 1
bRecurse		.BS 1
bQuiet			.BS 1
bPause			.BS 1

Count			.BS 2

PageCount		.EQ *
ArgIndex		.BS 1

hSrcFullPath	.BS 1

				.DO X.COPY.TO.DEST=1
X.COPY.MINBUF	.EQ 1024
X.COPY.MAXBUF	.EQ 4096

bNoConfirm		.BS 1

hDstFullPath	.BS 1

hSrcFile		.BS 1
hDstFile		.BS 1
hCopyBuf		.BS 1
				.FIN

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 #64					SS
				.DA #ZS.END-ZS.START	Zero Page Size
				.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.REG		.DA MSG.REG
				.DO X.COPY.TO.DEST=1
L.MSG.OVERWRITE	.DA MSG.OVERWRITE
				.FIN
L.MSG.OK		.DA MSG.OK
L.MSG.ERR		.DA MSG.ERR
L.MSG.CRLF		.DA MSG.CRLF
L.MSG.DONE		.DA MSG.DONE
				.DA 0
*--------------------------------------
CS.INIT			clc
CS.INIT.RTS		rts
*--------------------------------------
CS.RUN			jsr CS.RUN.CheckOpt
				bcs CS.INIT.RTS

				>LDYAI 256
				>SYSCALL GetMem
				bcs CS.INIT.RTS

				stx hSrcFullPath

				.DO X.COPY.TO.DEST=1
				>LDYAI 256
				>SYSCALL GetMem
				bcs CS.INIT.RTS

				stx hDstFullPath
				.FIN
*--------------------------------------
CS.RUN.Loop		jsr CS.RUN.CheckKey
				bcs .99

				bit bPause
				bmi CS.RUN.Loop

				jsr X.GetEntry
				bcs .80

				jsr X.IncludeMatch
				bcs .8					no match, skip....

				jsr X.IgnoreMatch
				bcc .8

				jsr CS.RUN.BuildFilePath

				ldy #S.STAT.MODE+1
				lda (ZPFileStat),y
				and #$70
				bne .1					REG file ?

				jsr CS.RUN.REG
				bra .7

.1				cmp /S.STAT.MODE.DIR	DIR ?
				bne .98

				bit bRecurse
				bpl .8

				lda (ZPFileName)
				cmp #'.'
				bne .2					Skip "." & ".."

				ldy #1
				lda (ZPFileName),y
				beq .8

				cmp #'.'
				bne .2					Skip "." & ".."

				iny
				lda (ZPFileName),y
				beq .8

.2				jsr CS.RUN.DIR

.7				jsr CS.RUN.CheckErr
				bcs .99

.8				jsr X.GetNextEntry
				bcc CS.RUN.Loop

.80				jsr CS.RUN.LEAVE
				bcc .8

				rts

.98				lda #MLI.E.UNSUPST
				sec
.99				rts
*--------------------------------------
CS.RUN.LEAVE 	jsr X.LeaveSubDir		exit this sub dir....
				bcs .90					base, we are done, exit

				jsr X.BasePath..
				.DO X.DELETE.SOURCE=1

				jsr X.GetEntry
				jsr CS.RUN.BuildFilePath
				jsr CS.RUN.DIR.MSG
				bcs .99

				lda hSrcFullPath
				jsr CS.RUN.GetPathA
				>SYSCALL Remove
				.ELSE
				clc
				.FIN

				rts

.90				bit bQuiet
				bmi .98

				>PUSHW L.MSG.DONE
				>PUSHW Count
				>PUSHBI 2

				>SYSCALL PrintF
				bcs .99

.98				lda #0
				sec
.99				rts
*--------------------------------------
CS.RUN.DIR		jsr CS.RUN.DIR.MSG
				bcs .99

				.DO X.COPY.TO.DEST=1

				jsr CS.RUN.CheckOverwrite
				bcs .99					ABORT

				bmi .99					NO

				bne .8					YES, no create but enter

				lda hDstFullPath		NOT EXISTS
				jsr CS.RUN.PushPathA

				ldy #S.STAT.MODE+1
				lda (ZPFileStat),y
				>PUSHA
				dey
				lda (ZPFileStat),y
				>PUSHA
				>SYSCALL MKDir
				bcs .99

				jsr CS.RUN.IncCount
				.FIN

.8				>LDYA ZPFileName
				jmp X.EnterSubDirYA

.99				rts
*--------------------------------------
CS.RUN.REG		ldx #2
				jsr CS.RUN.REG.MSG
				bcs .99

				.DO X.COPY.TO.DEST=1

				jsr CS.RUN.CheckOverwrite
				bcs .99					ABORT

				bmi .99					do not overwrite

.2				.DO X.DELETE.SOURCE=1	mv file, check if srcbase=dstbase
				bne .3					overwrite, copy over

				>LDA.G hSrcBasePath
				jsr CS.RUN.PushPathA
				>LDA.G hDstBasePath
				jsr CS.RUN.PushPathA
				>SYSCALL StrCaseCmp
				bcs .3					not same dir, go copy/delete

				lda hSrcFullPath
				jsr CS.RUN.PushPathA
				lda hDstFullPath
				jsr CS.RUN.PushPathA
				>SYSCALL Rename
				bcc .8

				rts

				.FIN
.3				jsr CS.RUN.CopyStart
				bcs .99

				.FIN

.4				.DO X.DELETE.SOURCE=1

				lda hSrcFullPath
				jsr CS.RUN.GetPathA
				>SYSCALL Remove
				bcs .99

				.FIN

.8				jmp CS.RUN.IncCount

.99				rts
*--------------------------------------
CS.RUN.DIR.MSG	ldx #0

CS.RUN.REG.MSG	bit bQuiet
				bmi .8

				>PUSHW L.MSG.DIR,x

				lda hSrcFullPath
				jsr CS.RUN.PushPathA

				.DO X.COPY.TO.DEST=1
				lda hDstFullPath
				jsr CS.RUN.PushPathA
				>PUSHBI 4

				.ELSE
				>PUSHBI 2

				.FIN

				>SYSCALL PrintF
				rts

.8				clc
				rts
*--------------------------------------
CS.RUN.CR		>PUSHW L.MSG.CRLF
				>PUSHBI 0
				>SYSCALL PrintF
CS.RUN.CR.RTS	rts
*--------------------------------------
				.DO X.COPY.TO.DEST=1
CS.RUN.CopyStart
				stz hSrcFile
				stz hDstFile
				stz hCopyBuf

				lda hSrcFullPath
				ldx #O.RDONLY
				jsr CS.RUN.Open
				bcs .9

				sta hSrcFile

				lda hDstFullPath
				ldx #O.WRONLY+O.CREATE
				jsr CS.RUN.Open

				bcs .9

				sta hDstFile

				lda /X.COPY.MAXBUF

.1				sta PageCount

				ldy #0
				>SYSCALL GetMem
				bcc .2

				tax

				lda PageCount
				lsr
				cmp /X.COPY.MINBUF
				bcs .1

				txa
				sec
.9				bra CS.RUN.CopyEnd

.2				>STYA ZPPtr1
				stx hCopyBuf
*--------------------------------------
CS.RUN.Copy		>PUSHB hSrcFile
				>PUSHW ZPPtr1			Dst Ptr
				>PUSHB PageCount		Bytes To Read
				>PUSHBI 0
				>SYSCALL FRead
				bcc .1

				cmp #MLI.E.EOF
				sec
				bne CS.RUN.CopyEnd

				lda #0
				clc
				bra CS.RUN.CopyEnd

.1				sta ZPPtr2+1
				>PUSHB hDstFile
				>PUSHW ZPPtr1			Src Ptr
				lda ZPPtr2+1
				>PUSHYA					Bytes To Write
				>SYSCALL FWrite
				bcs CS.RUN.CopyEnd

				bit bQuiet
				bmi .2

				lda #'.'
				>SYSCALL PutChar

.2				jsr CS.RUN.CheckKey
				bcs CS.RUN.CopyEnd
				
				lda ZPPtr2+1
				cmp PageCount
				bcs CS.RUN.Copy
				
*				clc
*--------------------------------------
CS.RUN.CopyEnd	php
				pha

				lda hDstFile
				beq .1

				>SYSCALL FClose

.1				lda hSrcFile
				beq .2

				>SYSCALL FClose

.2				lda hCopyBuf
				beq .3

				>SYSCALL FreeMem

.3				pla
				plp

CS.RUN.Copy.RTS	rts
				.FIN
*--------------------------------------
CS.RUN.Open		phx						Save open mode
				jsr CS.RUN.PushPathA

				pla
				>PUSHA

				ldy #S.STAT.P.TYPE
				>PUSHB (ZPFileStat),y

				ldy #S.STAT.P.AUXTYPE+1
				>PUSHB (ZPFileStat),y
				dey
				>PUSHB (ZPFileStat),y

				>SYSCALL FOpen
				rts
*--------------------------------------
CS.RUN.CheckErr	bcs .1

				bit bQuiet
				bmi .8

				>LDYA L.MSG.OK
				>SYSCALL PutS
.8				rts

.1				tax
				clc
				beq .8					SKIP

				>PUSHW L.MSG.ERR
				txa
				pha
				>PUSHA
				>PUSHBI 1
				>SYSCALL PrintF

				pla
				sec
				bit bContinue
				bpl .99

				clc

.99				rts
*--------------------------------------
CS.RUN.BuildFilePath
				lda hSrcFullPath
				jsr CS.RUN.PushPathA
				>LDA.G hSrcBasePath
				jsr CS.RUN.PushPathA
				>SYSCALL StrCpy

				lda hSrcFullPath
				jsr CS.RUN.PushPathA
				>PUSHW ZPFileName
				>SYSCALL StrCat

				.DO X.COPY.TO.DEST=1
				lda hDstFullPath
				jsr CS.RUN.PushPathA
				>LDA.G hDstBasePath
				jsr CS.RUN.PushPathA
				>SYSCALL StrCpy

				lda hDstFullPath
				jsr CS.RUN.PushPathA

				>LDA.G hDstFileName
				beq .1

				>SYSCALL GetMemPtr
				>PUSHYA
				bra .2

.1				>PUSHW ZPFileName

.2				>SYSCALL StrCat
				.FIN
				rts
*--------------------------------------
CS.RUN.GetPathA	sec
				.HS 90					BCC
CS.RUN.PushPathA
				clc
				php

				>SYSCALL GetMemPtr

				plp
				bcs .8

				>PUSHYA

.8				rts
*--------------------------------------
CS.RUN.IncCount	inc Count
				bne .8

				inc Count+1

.8				rts
*--------------------------------------
				.DO X.COPY.TO.DEST=1
CS.RUN.CheckOverwrite
*--------------------------------------
				lda hDstFullPath
				jsr CS.RUN.PushPathA
				>PUSHEA.G STATBUF
				>SYSCALL Stat
				bcs .8					File Not exists...

				bit bNoConfirm
				bmi .4					no prompt, override

				bit bQuiet
				bmi .10

				jsr CS.RUN.CR

.10				>PUSHW L.MSG.OVERWRITE
				lda hDstFullPath
				jsr CS.RUN.PushPathA
				>PUSHBI 2
				>SYSCALL PrintF
				bcs .99

.1				>SYSCALL GetChar

				cmp #3
				beq .99					abort

				jsr X.ToUpper

				cmp #'N'
				bne .2

				lda #$80				EXISTS, NO OVERWRITE
				clc
				rts

.2				cmp #'Y'
				beq .4

.3				cmp #'A'
				bne .1

*				sec
				ror bNoConfirm

.4				lda #$01				EXISTS, OVERWRITE
				clc
				rts

.8				lda #0					NOT EXISTS
				clc
.99				rts
				.FIN
*--------------------------------------
CS.RUN.CheckKey	ldy #S.PS.hStdIn
				lda (pPS),y
				>SYSCALL FEOF
				bcs .9					I/O error

				tay
				bne .8

				>SYSCALL GetChar
				bcs .9					I/O error

				cmp #$03				Ctrl-C
				beq .9

				cmp #$13				Ctrl-S
				bne .8

				lda bPause
				eor	#$ff
				sta bPause

.8				clc

.9				rts
*--------------------------------------
CS.RUN.CheckOpt	jsr CS.RUN.NextOpt
				bcs .7

				>STYA ZPPtr1

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

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

.1				ldx #OptionVars-OptionList-1

.2				cmp OptionList,x
				beq .3

				dex
				bpl .2

.98				>LDYA L.MSG.USAGE
				>SYSCALL PutS
				lda #E.SYN
				sec
.99				rts

.3				lda OptionVars,x
				beq .4					-I

				tax
				dec 0,x

				iny
				lda (ZPPtr1),y
				bne .1

				bra CS.RUN.CheckOpt

.4				iny
				lda (ZPPtr1),y
				bne .98

				>LDA.G hIgnore
				bne .98

				jsr CS.RUN.NextOpt
				bcs .98

				>SYSCALL StrDup
				bcs .99

				txa
				>STA.G hIgnore
				bra CS.RUN.CheckOpt
*--------------------------------------
.5				>LDA.G index

				.DO X.COPY.TO.DEST=1
				bne .6					Already have a Src dir...
				.ELSE
				bne .98
				.FIN

				jsr X.InitSrcDirPtr1
				bcc CS.RUN.CheckOpt		success, scan for any other args

				rts

				.DO X.COPY.TO.DEST=1
.6				>LDA.G hDstBasePath
				bne .98					we already have a second arg....error!

				jsr X.InitDstDirPtr1
				bcc CS.RUN.CheckOpt		success, scan for any other args

				rts
				.FIN
*--------------------------------------
.7				>LDA.G index			processed all args
				beq .98					, no src ? ERROR

				.DO X.COPY.TO.DEST=1
				>LDA.G hDstBasePath
				bne .8					we also have a Dst folder

				ldy #S.PS.hCWD			no dst folder, use actual prefix
				lda (pPS),y
				>SYSCALL GetMemPtr
				jsr X.InitDstDirYA
				bcs .99
				.FIN

.8				clc
				rts
*--------------------------------------
CS.RUN.NextOpt	inc ArgIndex
				lda ArgIndex
				>SYSCALL ArgV
				rts
*--------------------------------------
CS.DOEVENT		sec
				rts
*--------------------------------------
CS.QUIT			jsr X.LeaveSubDir
				bcc CS.QUIT

				.DO X.COPY.TO.DEST=1

				lda hDstFullPath
				jsr .8

				ldy #hDstFileName
				jsr .7
				.FIN

				lda hSrcFullPath
				jsr .8

				ldy #hInclude
				jsr .7

				ldy #hIgnore

.7				lda (pData),y
.8				beq .9

				>SYSCALL FreeMem
.9				clc
				rts
*--------------------------------------
MAN
SAVE usr/src/shared/x.cpmvrm.s
LOAD usr/src/bin/cp.s
ASM
