;*****************************************
;
;   RECOVR - recovers erased disk files
;
;*****************************************
;1.0 18-Oct-84 DFP written by D. Pallmann.
;
; File makes external references to MAC:SYSLIB.LIB.  
; After assembly, enter this command: .LNKLIT file
;

	VMAJOR=1

	SEARCH	SYS
	SEARCH	SYSSYM

	EXTERN	$FNPPN

	.OFINI
	.OFDEF	DISK,D.DDB		;DDB for disk I/O
	.OFSIZ	IMPSIZ

START:	PHDR	-1,0,PH$REE!PH$REU!PH$L12;program header
	GETIMP	IMPSIZ,A5		;allocate impure area
	CLR	D4			;clear recovered count

CHKLIN:	BYP				;bypass leading spaces in cmd line
	LIN				;empty cmd line?
	BNE	GETFIL			; no - branch

HELP:	TYPECR	<? usage: .RECOVR file, file, ... file>
	JMP	EXIT			;explain syntax and exit

GETFIL:	FSPEC	DISK(A5),BAK		;load DDB w/spec
	INIT	DISK(A5)		;fetch driver, allocate buffer
	PFILE	DISK(A5)		;echo filename
	TYPESP				;print a space
	LOOKUP	DISK(A5)		;does file already exist?
	JEQ	EXISTS			; yes

LKPUFD:	MOVW	DISK+D.PPN(A5),D1	;set up registers D1
	BNE	10$			;branch if specified
	JOBIDX	A0			;index JCB
	MOVW	JOBUSR(A0),D1		;use log default PPN
10$:	SAVE	A2			;save line index
	LEA	A2,DISK(A5)		; and A2 for $FNPPN call
	CALL	$FNPPN			;find PPN in appropriate MFD
	REST	A2			;restore line index
	TST	D0			;successful?
	BNE	NOTFND			; no - branch
	CLR	D5			;set link
	MOVW	2(A1),D5		; to first block of UFD

REDUFD:	CTRLC	EXIT			;branch on ^C
	MOV	D5,DISK+D.REC(A5)	;set link into DDB record number
	BEQ	NOTFND			; branch if zero (end of UFD)
	READ	DISK(A5)		;read disk block
	MOV	DISK+D.BUF(A5),A0	;point A0 to buffer
	CLR	D5			;load link
	MOVW	(A0)+,D5		; into D5

SEARCH:	MOV	#42.,D0			;scan 42 file entries
10$:	CTRLC	EXIT			;branch on ^C
	CMM	2(A0),DISK+D.FIL+2(A5)	;is this our file?
	BEQ	RECOVR			; yes - go restore it
	ADD	#14,A0			; no - try next file in UFD
	SOB	D0,10$			;      until end-of-block
	BR	REDUFD			;go scan next block of UFD

RECOVR:	MOVW	DISK+D.FIL(A5),@A0	;restore filename
	WRITE	DISK(A5)		;re-write UFD block
	TYPECR	recovered		;tell user we did it
	INC	D4			;remember that we did a recovery
	BR	NXTFIL

NOTFND:	TYPECR	not found		;tell user file does not exist
	BR	NXTFIL			;go process next file

EXISTS:	TYPECR	has not been erased	;tell user file already exists

NXTFIL:	CMPB	(A2)+,#',		;are there more files on the cmd line?
	BNE	EXIT			; no
	BYP				; yes - bypass leading spaces
	JMP	GETFIL			;       and process the next file

EXIT:	TST	D4			;did we recover any files?
	BEQ	10$			; no
	TYPECR	% run DSKANA immediately; yes - tell user he MUST run DSKANA
10$:	CRLF				;newline
	EXIT				;exit

	END