;**********************************************************************
; RVINI.LIT
;
; Deciphered from disassembled version
;
;**********************************************************************


	SEARCH	SYS			;search normals
	SEARCH	SYSSYM


	VMAJOR=3.			;define version number
	VMINOR=1.


L0:	PHDR	-1,0,PH$REE!PH$REU	;list a program header

	.OFINI				;define some variables
	.OFDEF	RETURN,4		;return address of JMP VUE
	.OFDEF	INSTR,4			;instructions overwritten
	.OFSIZ	IMPSIZ

	GETIMP	IMPSIZ,A3		;A3 points to variables




	;First search for both VUE.LIT and REVUE.SYS in system memory
	;
	LEA	A2,VUELIT		;point A2 to VUE.LIT
	SRCH	@A2,A1			;look for VUE.LIT in RES:, A1 pointer
	BEQ	VUEOK			;found RES:VUE.LIT
	TYPECR	<?VUE.LIT not found in SYSTEM>
	TYPECR	<REVUE not installed!>
	MOV	#7,D1
	TTY
	EXIT
VUELIT:	RAD50	/VUE   LIT/
NEWNAM:	RAD50	/VUELIT/

	;RENAME VUE.LIT to VUELIT.LIT
	;
VUEOK:	LEA	A5,NEWNAM		;point A5 to VUELIT name
	PUSH	A1			;save pointer to VUE.LIT
	SUB	#6,A1			;point to VUE name
	MOV	@A5,@A1			;rename VUE to VUELIT
	POP	A1			;repoint A1 to VUE

	; Find REVUE.SYS
	;
	LEA	A2,RVSYS		;point A2 to REVUE.SYS
	SRCH	@A2,A2			;look for REVUE.SYS, A2 pointer
	BEQ	RVSOK			;found it
	TYPECR	<?REVUE.SYS not found in SYSTEM>
	TYPECR	<REVUE not installed!>
	MOV	#7,D1
	TTY
	EXIT
RVSYS:	RAD50	/REVUE SYS/


	;Finally, search for RV.LIT in System Memory and rename it to VUE.LIT
	;
RVSOK:	PUSH	A2			;save current pointers
	PUSH	A1
	LEA	A2,RV			;point A2 to RV.LIT
	SRCH	@A2,A1			;look for RV.LIT in RES:, A1 pointer
	BEQ	RVOK			;found RES:RV.LIT
	TYPECR	<?RV.LIT not found in SYSTEM>
	TYPECR	<REVUE not installed!>
	MOV	#7,D1
	TTY
	POP	A1			;restore pointers
	POP	A2
	EXIT
RV:	RAD50	/RV    LIT/	

RVOK:	LEA	A5,VUELIT		;point A5 to VUE.LIT name
	SUB	#6,A1			;point to RV name
	MOV	@A5,@A1			;rename RV to VUE
	POP	A1			;restore pointers
	POP	A2

	;Locate the first occurance of TIN in REVUE.SYS
	;
	CALL	FNDTIN			;find first TIN in REVUE.SYS (A2^)


	;now find first occurance of TIN in VUE.LIT
	;
VUETIN:	CMPW	@A1,#120004		;TIN?
	BEQ	10$			;yup
	ADD	#2,A1			;nope, point to next word
	BR	VUETIN			;and look again

	;Overwrite TIN blah blah in VUE with JMP REVUE.SYS
	;and then at end of routine, write JMP VUE
	;
10$:	CALL	MOVE			;replace VUE TIN with JMP REVUE.SYS

	;now find second occurance of TIN in REVUE
	;
	CALL	FNDTIN			;find next occurance of TIN in REVUE

20$:	ADD	#2,A1			;Find next TIN in VUE.LIT
	CMPW	@A1,#120004		;TIN?
	BNE	20$			;nope, still look
	CALL	MOVE			;yup, move in new instructions

	TYPECR	<REVUE installed>	;let 'em know you're done
	EXIT				;back to dot


	;SUBROUTINE to find occurance of TIN in REVUE.SYS
	;
FNDTIN:	CMPW	@A2,#120004		;TIN?
	BNE	10$			;nope
	RTN				;yup, A2 points to TIN in REVUE.SYS
10$:	ADD	#2,A2			;point to next word
	BR	FNDTIN			;and look for TIN again


	;SUBROUTINE to replace first TIN in VUE with JMP REVUE.SYS
	;
MOVE:	MOV	A1,A4			;move VUE TIN address to A4
	ADD	#6,A4			;bypass next three instructions
	MOV	A4,RETURN(A3)		;save return address in buffer
	MOV	2(A1),INSTR(A3)		;save stuff after TIN in buffer
	MOVW	#47371,(A1)+		;move JMP into TIN's place in VUE
	MOV	A2,(A1)+		;next place address to JMP in VUE

	;now locate first NOP in REVUE.SYS
	;
FNDNOP:	CMPW	@A2,#47161		;look for NOP in REVUE.SYS
	BEQ	10$			;found a NOP
	ADD	#2,A2			;else point to next word
	BR	FNDNOP			;and look for NOP

	;found a NOP, place overwritten instruction + JMP VUE in this location
	;
10$:	MOV	INSTR(A3),(A2)+		;replace instructs overwritten by JMP
	MOVW	#47371,(A2)+		;place JMP into REVUE.SYS
	MOV	RETURN(A3),(A2)+	;place return address after JMP
	RTN				;done with first replacement

	END