	TITLE move

; 3-24-85 return a error code



;Routines

	public	START,ERROR_JUMP,MM,M0,M1,M2,M3,M4,M5,M1_JUMP,ERROR,EXIT
	public	ERROR_EXIT

;Variables

	public	SOURCE_NAME,DESTINATION_NAME,PARSED_DESTINATION_NAME,D_SWITCH
	public	FILE_ALREADY_EXISTS

	EXTRN	PARSE:NEAR
	EXTRN	COPYFILE:NEAR
	EXTRN	PRINT_FILENAME:NEAR
	EXTRN	MOVE_PARAMETERS:NEAR
	EXTRN	MOVE_PNAME:NEAR
	EXTRN	GET_D_SWITCH:NEAR
	EXTRN	ERROR_ROUTINE:NEAR
	EXTRN	APPEND_STAR_DOT_STAR:NEAR
	EXTRN	FIXUP_COMMAND_LINE:NEAR

G	GROUP	CODE
CODE	SEGMENT PUBLIC
	ASSUME CS:G,  DS:G,  ES:G,  SS:G
	ORG 100H				; com file

FILE_NOT_FOUND		EQU	11
DELETE_ERROR		EQU	14
FILE_ALREADY_EXISTS_EQU	EQU 	6
MAX_NAME_LENGTH		EQU	80


;  Function calls
RENAME		EQU	56H
DELETE_FILE	EQU	41H
TERMINATE	EQU	4CH
SET_DTA		EQU	1AH
FIND_FIRST_FILE	EQU	4EH
FIND_NEXT_FILE	EQU	4FH


DATABLOCK	STRUC			; For "find first" and "find next"
		DB	21 DUP (?)	; reserved for find_nexts
ATTRIBUTE	DB	0
TIME		DW	0
DATE		DW	0
LOW_SIZE	DW	0
HIGH_SIZE	DW	0
PNAME		DB	13 DUP (?)
DATABLOCK	ENDS	

START:
	JMP	MM

SOURCE_NAME		DB	MAX_NAME_LENGTH DUP(?)
FOUND_SOURCE_NAME	DATABLOCK <,,,,,,>
DESTINATION_NAME	DB	MAX_NAME_LENGTH DUP(?)
PARSED_DESTINATION_NAME	DB	MAX_NAME_LENGTH DUP(?)
SCRATCH			DATABLOCK <,,,,,,>
D_SWITCH		DB	?
FILE_ALREADY_EXISTS	DB	FILE_ALREADY_EXISTS_EQU

ERROR_JUMP:
	JMP	ERROR

MM:
	CMP	AL, 0FFH	; Check for valid drive specifications.
	JZ	ERROR_JUMP
	CMP	AH, 0FFH
	JZ	ERROR_JUMP

	CALL	FIXUP_COMMAND_LINE

	CALL	GET_D_SWITCH			; look for the "d" switch
	MOV	[D_SWITCH], AL

	LEA	DX, SOURCE_NAME		; move both filenames to 
	LEA	BX, DESTINATION_NAME		; buffers
	CALL	MOVE_PARAMETERS			
	JC	ERROR_JUMP

	MOV	AH, SET_DTA
	LEA	DX, FOUND_SOURCE_NAME
	INT	21H

M0:
	MOV	AH, [FIND_FIRST_FILE]	; search for a file
	LEA	DX, SOURCE_NAME
	MOV	CX, 20H
	INT	21H
	JNC	M1
	
	MOV	AH, [FIND_FIRST_FILE]	; search for a dir
	MOV	CX, 10H
	INT	21H
	MOV	AL, FILE_NOT_FOUND
	JC	ERROR_JUMP	

	LEA	SI, SOURCE_NAME		; append \*.* to the name
	CALL	APPEND_STAR_DOT_STAR
	JMP	M0

M1:
	LEA	BX, FOUND_SOURCE_NAME.PNAME	; Parse the file
	LEA	SI, DESTINATION_NAME
	LEA	DI, PARSED_DESTINATION_NAME
	CALL	PARSE
	JC	ERROR_JUMP

	MOV	AH, SET_DTA
	LEA	DX, SCRATCH
	INT	21H
	
	MOV	AH, FIND_FIRST_FILE		; Look for the file 
	LEA	DX, PARSED_DESTINATION_NAME
	MOV	CX, 20H
	INT	21H
	JNC	M2
	
	MOV	AH, FIND_FIRST_FILE		; look for a dir
	MOV	CX, 10H
	INT	21H
	JC	M4

	LEA	SI, DESTINATION_NAME		; append \*.* to the name
	CALL	APPEND_STAR_DOT_STAR		; and reparse
	JC	ERROR
	JMP	M1

M2:
	CMP	[D_SWITCH], 0			; Is the delete switch on?
	JNZ	M3
	
	LEA	SI, PARSED_DESTINATION_NAME	; print the filename which
	CALL	PRINT_FILENAME		; already exists

	MOV	AL, [FILE_ALREADY_EXISTS]	; print "file already exists"
	CALL	ERROR_ROUTINE
	JMP	M5

M3:
	MOV	AH, DELETE_FILE		; delete the old file
	LEA	DX, PARSED_DESTINATION_NAME
	INT	21H
	MOV	AL, DELETE_ERROR
	JC	ERROR
	
M4:
	LEA	SI, PARSED_DESTINATION_NAME	; print the filename 
	CALL	PRINT_FILENAME			; to be moved
	
	LEA	SI, FOUND_SOURCE_NAME.PNAME	; add the pname found
	LEA	DI, SOURCE_NAME	      	; to the end of "source_name"
	CALL	MOVE_PNAME
	JC	ERROR
	
	MOV	AH, RENAME			; rename the file.
	LEA	DX, SOURCE_NAME
	LEA	DI, PARSED_DESTINATION_NAME
	INT	21H
	JNC	M5

	CMP	AX, 11H			; copy the file if on 
	JNZ	ERROR				; different devices
	CALL	COPYFILE
	JC	ERROR

M5:
	MOV	AH, SET_DTA
	LEA	DX, FOUND_SOURCE_NAME
	INT	21H

	MOV	AH, FIND_NEXT_FILE		; search for a file
	LEA	DX, SOURCE_NAME
	MOV	CX, 20H
	INT	21H
	JNC	M1_JUMP
	
	MOV	AH, FIND_NEXT_FILE		; search for a dir
	MOV	CX, 10H
	INT	21H
	JC	EXIT	

	LEA	SI, SOURCE_NAME		; append "\*.*" to the name
	CALL	APPEND_STAR_DOT_STAR
	JC	ERROR

M1_JUMP:
	JMP	M1			

ERROR:
	CALL	ERROR_ROUTINE
	mov	ax, 1
	jmp	short error_exit

EXIT:	
	XOR	AX, AX				; set the error code = no
						; error
ERROR_EXIT:
	MOV	AH,4CH
	INT	33

CODE	ENDS
	END	START
