	.TITLE	BACKUP	BACKUP FILES FOR USER& REMOVE FROM DUMP SYSTEM.!
	.IDENT	/X002.1/
; LUN USAGE
;
;	1	TI:
;	2	MM:
;	3	INCDMPSTS.SYS
;
;	4	FILE BEING DUMPED.
;
;

	.MCALL	OPEN$W,OPEN$R,WRITE$,CSI$,CSI$1,CSI$2,GCML$,GCMLB$
	.MCALL	OFID$R,ALUN$C,QIOW$C,MRKT$C,EXIT$S,RUN$C,QIOW$S,WTSE$C
	.MCALL	FHDOF$,FDBF$A
	.MCALL	FINIT$
	.MCALL	FDBDF$,FSRSZ$,READ$,GET$R,PUT$R,CLOSE$
	.MCALL	WAIT$
	.MCALL	OPEN$M,FDRC$A,FDOP$A
	.MCALL	CSRQ$C,GTIM$C
	FSRSZ$	2,3000.	;1 FILES BEING ACCESSED  +INCDMPSTS.SYS

	CSI$
CSIBLK:	.BLKB	C.SIZE
GCLBLK:	GCMLB$	4,BKP,,1
CNTFDB::
	FDBDF$
	FDRC$A	FD.RAN,FDMPNO,16.
	FDOP$A	3,CNTDSP,,FO.MFY
CNTDSP::.WORD	4,CNTDEV,6,CNTUIC,13.,CNTFIL
CNTDEV::.ASCII	/SY0:/
CNTUIC::.ASCII	/[10,1]/
CNTFIL::.ASCII	/INCDMPSTS.SYS/
	.EVEN
DMPFDB::FDBDF$			; FDB FOR FILE BEING DUMPED
	FDRC$A	FD.RWM
	FDOP$A	4,CSIBLK+C.DSDS,,FO.RD
DMPSTS::.BLKW	2
FAIL:	.ASCII	/INC... -- CHECK FAIL  /
FILBF:	.BLKW	16.
HDROLD:	.WORD	2048.,1,5,0,5,5,0,0
MMFDB:	FDBDF$
	FDAT$A	R.VAR
	FDRC$A	FD.RWM
	FDBF$A	,2064.
	FDOP$A	2,CSIBLK+C.DSDS,FO.WRT
	.EVEN

FSIZ:	.WORD	0
	FHDOF$
FILNO:	.WORD	0
FDMPNO:	.WORD	0
FSEQNO:	.WORD	0
FUIC:	.WORD	0
FNAM1:	.WORD	0
FNAM2:	.WORD	0
FNAM3:	.WORD	0
FTYP:	.WORD	0
FVER:	.WORD	0
NTBLKS:	.WORD	0
NBLK0:	.WORD	0
NBLK:	.WORD	0
fSTATS::.BLKW	5
; BUFFERS FOR IO TO TAPE
;
TY1HDR::.WORD 2048.,1,0,0,0,0,0,0
TY1BLK::.BLKB	2048.
TY4HDR::.WORD	512.,4,0,0,0,0,0,0
TY4BLK::.BLKB 	512.
CINFO::	.BLKW	8.
STS::	.BLKW	2
	
FILPRM:	.ASCII	<15><12>/FILE>/
CNERR:	.ASCIZ	/BKP -- ERROR WRITING STATUS RECORD/
CNTERR:	.ASCIZ	/BKP -- ERROR MAINTAINING FILE STATUS RECORD/
MMERR:	.ASCII	/BKP -- ERROR WRITING TO TAPE/
MM1ERR:	.ASCIZ	/000000/
SYNERR:	.ASCIZ	/BKP -- SYNTAX ERROR/
BKP:	.ASCII	/BACKUP/<1><0><0><0>/DB0:[126,342]/
	.even

START:	FINIT$
	ALUN$C	1,TI,0
	BR	3$
2$:	MOV	#SYNERR,R0
	CALL	TYPEIT
3$:	GCML$	#GCLBLK
	TSTB	G.ERR+GCLBLK
	BEQ	1$
	EXIT$S
1$:	CSI$1	#CSIBLK,GCLBLK+G.CMLD+2,GCLBLK+G.CMLD
	BCS	2$
	TST	C.CMLD+CSIBLK
	BEQ	3$
	CSI$2	#CSIBLK,OUTPUT
	BCS	2$
	OPEN$W	#MMFDB
	BCS	2$
	BR	NXTFIL
SNR:	MOV	#SYNERR,R0
	CALL	TYPEIT
NXTFIL:	GCML$	#GCLBLK,#FILPRM,#7
	TSTB	GCLBLK+G.ERR
	BGE	1$
	MOV	#2064.,MMFDB+F.FFBY
	MOV	NTBLKS,MMFDB+F.EFBK+2
	MOV	NTBLKS,MMFDB+F.HIBK+2
	CLOSE$	#MMFDB
	EXIT$S
1$:	CSI$1	#CSIBLK,G.CMLD+GCLBLK+2,GCLBLK+G.CMLD
	BCS	SNR
	CSI$2	#CSIBLK,OUTPUT
	BCS	SNR
	MOV	#FSTATS,DMPFDB+F.STBK
	OPEN$R	#DMPFDB
	BCS	SNR
	CALL	DMPFIL
	BCS	SNR
	CLOSE$	#DMPFDB
	BR	NXTFIL

ATTLST:	.BYTE	-10.,0
	.WORD	TY4BLK
	.WORD	0
DMPFIL:
	QIOW$C	IO.RAT,4,1,,,,<DMPFDB+F.FNB+N.FID,ATTLST>
	BCC	11$
	RTS	PC		;FAILED TO READ ATTRIBUTES OF FILE
11$:	MOV	#FILNO,R0
	MOV	DMPFDB+F.FNB+N.FID,(R0)+
	MOV	#-2,(R0)+		;DUMP NO
	MOV	DMPFDB+F.FNB+N.FID+2,(R0)+
	MOV	FSTATS+6,FSIZ
	MOV	TY4BLK+H.FOWN,(R0)+
	MOV	DMPFDB+F.FNB+N.FNAM,(R0)+
	MOV	DMPFDB+F.FNB+N.FNAM+2,(R0)+
	MOV	DMPFDB+F.FNB+N.FNAM+4,(R0)+
	MOV	DMPFDB+F.FNB+N.FTYP,(R0)+
	MOV	DMPFDB+F.FNB+N.FVER,(R0)+
	call	wrtty2
	call	wrtty4
	mov	#1,nblk
	clr	nblk0
3$:	qiow$s	#io.rvb,#4,#1,,#dmpsts,,<#ty1blk,#2048.,,#0,nblk>
	cmpb	dmpsts,#1
	bne	5$			;assume eof
	call	wrtty1
	add	#4,nblk
	br	3$
5$:	call	wrtty1
7$:	close$	#dmpfdb
	OPEN$M	#CNTFDB
	BCS	20$
	put$r	#cntfdb,#fdmpno,#16.,filno
	BCS	21$
	CLOSE$	#CNTFDB
	CLC	
	RTS	PC
20$:	MOV	#CNTERR,R0
22$:	CALL	TYPEIT
	RTS	PC
21$:	MOV	#CNERR,R0
	BR	22$

wbk::	inc	ntbLks
	qiow$s	#io.wVb,#2,#1,,#sts,,<r0,r1,,#0,NTBLKS>
	cmpb	sts,#1
	beq	1$
	MOV	STS,R1
	MOV	#MM1ERR,R0
	MOV	R0,R2
	CALL	$CBOMG
	MOV	#MMERR,R0
	CALL	TYPEIT
1$:	rts	pc
typeit:	mov	r1,-(sp)
	mov	r0,r1
1$:	tstb	(r1)+
	bne	1$
	sub	r0,r1
	qiow$s	#io.wlb,#1,#1,,,,<r0,r1,#40>
	mov	(sp)+,r1
	rts	pc


WRTTY1:	TST	DMPSTS+2
	BEQ	55$
	;write data blocks - assumes header record set by type 2
	mov	#ty1hdr,r0
	mov	r0,r1
	add	#4,r1
	tst	(r1)
	BEQ	1$
	inc	(r1)
	inc	(r1)
	inc	(r1)
1$:	inc	(r1)
	mov	dmpsts+2,r1
	mov	#1,2(r0)
	mov	r1,(r0)
	add	#16.,r1
	call	wbk
55$:	rts	pc


wrtty4:	;write index file block
	mov	ty1hdr+10,ty4hdr+10
	mov	ty1hdr+12,ty4hdr+12
	mov	#ty4hdr,r0
	mov	#528.,r1
	call	wbk
	rts 	pc


wrtty2:;	write type 2 backup block
	mov	#ty1hdr,r0
	mov	#4000,(R0)+
	mov	#2,(r0)+
	clr	(r0)+
	clr	(r0)+
	mov	filno,(r0)+
	mov	fseqno,(r0)+
	mov	#2002,r1
1$:	clr	(r0)+
	sob	r1,1$
	mov	fsiz,ty1blk+90.
	mov	#bkp,r0
	mov	#ty1blk,r2
	mov	#6,r1
	mov	#ty1blk+3776,r3
2$:	movb	(r0),(r2)+
	movb	(r0)+,-(r3)
	sob	r1,2$
	mov	#21,r1
3$:	movb	(r0)+,(r2)+
	sob	r1,3$
	mov	r2,r0
	mov	fnam1,r1
	call	$c5ta
	mov	fnam2,r1
	call	$c5ta
	mov	fnam3,r1
	call	$c5ta
	movb	#'.,(r0)+
	mov	ftyp,r1
	call	$c5ta
	movb	#';,(r0)+
	mov	fVER,r1
	clr	r2
	call	$cbomg
	mov	#ty1blk+10,r2
	sub	#2,r0
	sub	r2,r0
	mov	r0,(r2)
	mov	#ty1hdr,r0
	mov	#2064.,r1
	call	wbk
	rts	pc


	.END	START
