;---------------------------------------------------------------------------;
; Obviously, the error routine should not rely upon any libs being open!!   ;
; Temp001(a5) will be used as a base for dos.library                        ;
; Temp002(a5) will be used as the adr of the allocated mem for error text.  ;
; - This routine will attempt to open dos, get the output handle and list   ;
;   which libraries couldn't be openned (including the required versions).  ;
;---------------------------------------------------------------------------;
OpnLib_Error_Title
	PROGNAM
	Dc.b	": Could not open the following libraries:",10
	Dc.b	"(Minimum version numbers shown.)",10,10
OpnLib_Error_L	Equ *-OpnLib_Error_Title

OpnLib_Error_Body_Main_Length Equ 40
; 30 chars = maximum filename length, + " vXX",10,0 + a bit more.

	Even
;---------------------------------------------------------------------------;
OpnLib_Error
	Lea	N_DOSName(pc),a1
	Moveq	#0,d0			ANY version of dos.library
	N_CallExec	OpenLibrary
	Move.l	d0,Temp001(a5)		Store base adr.
	Beq	Finish			If we can't get it, Exit!

	Move.l	Temp001(a5),a6
	Jsr	_LVOOutput(a6)		-._ Get output Handle
	Move.l	d0,CLI_Hdl(a5)		-'  (CLI window or whatever)

	Lea	OpnLib_Error_Title(pc),a0
	Moveq	#OpnLib_Error_L,d3
	Bsr.s	OLE_CLIRite

	Bsr.s	BuildLibList

OpnLib_Error__2
	Move.l	Temp001(a5),a1		-.
	N_CallExec	CloseLibrary	 |- Close dos.library
	Bra	Finish			-'  and Finish program.
;---------------------------------------------------------------------------;
BuildLibList
	Moveq	#OpnLib_Error_Body_Main_Length,d0
	Bsr	LeoAllocPooled		Alloc mem for message.
	Move.l	d0,Temp002(a5)
	Beq.s	OpnLib_Error__2		If Allocation failed, Exit Program

	Lea	LibData_Start(pc),a2	Start of struc w/ name-ptrs and ver#s
	Lea	LibBases_Start(a5),a3	Start of struc w/ Base-Adrs.

BuildLibList_MainLoop
	Lea	LibBases_Finish(a5),a4
	Cmp.l	a4,a3			All libraries been checked?
	Blt.s	OLEBL_Skp1

	Moveq	#OpnLib_Error_Body_Main_Length,d0	-.
	Move.l	Temp002(a5),a1				 |- Deallocate Msg mem.
	Bsr	LeoFreePooled				-'
	RTS				If so, exit.

OLEBL_Skp1
	Tst.l	(a3)+			Did this lib fail to open?
	Beq.s	OLEBL_Skp2		If not, don't include in msg.
	Addq.l	#8,a2			Update the other pointer.
	Bra.s	BuildLibList_MainLoop	Check the rest of the libs.

OLEBL_Skp2
	Lea	LibNames_Start(pc),a0	-._ Point to the name of
	Add.l	(a2)+,a0		-'  the lib.
	Move.l	Temp002(a5),a1		Space to put it in.
	Bsr	CopyCN3			Copy it (excluding null).
	Move.b	#" ",(a1)+		A space after it.
	Move.b	#"v",(a1)+		version...

	Move.l	(a2)+,d1		Version of lib.
	Bsr	N2A2Dig			Convert number to output.
;	Note: N2A2Dig ONLY uses d1 and a1.
	Move.b	#10,(a1)+		A return after it.

	Movem.l	a2/a3,-(SP)
	Move.l	Temp002(a5),a0		Adr of line.
	Move.l	a1,d3			-._ Length of
	Sub.l	a0,d3			-'  line.
	Bsr.s	OLE_CLIRite		Write to shell.
	Movem.l	(SP)+,a2/a3

	Bra.s	BuildLibList_MainLoop
;---------------------------------------------------------------------------;
;- CLI WRITE (For OpenLibrary Error routine) -------------------------------;
;---------------------------------------------------------------------------;
; a0 = Start of text to be written    | d3 = #Chars to be Written           ;
; CLI_Hdl(a5) - Handle of CLI         |                                     ;
;---------------------------------------------------------------------------;
OLE_CLIRite
	Move.l	a0,d2
	Move.l	CLI_Hdl(a5),d1
	Move.l	Temp001(a5),a6
	Jsr	_LVOWrite(a6)
	Cmpi.l	#-1,d0
	Beq	Finish			Quit program NOW if there's an error.
	RTS
