// Initialization for Micro
// last edited December 21, 1979  1:42 PM
// Copyright Xerox Corporation 1979

	get "micdecl.d"
	get "streams.d"
	get "altofilesys.d"


external
[		// O.S.
	Closes; Endofs; Gets; MoveBlock; OpenFile; Puts; Resets
	fpComCm; fpSysDir
	dsp
		// MDI
	LookupEntries
		// GP
	SetupReadParam; ReadParam
		// BcplRuntime
	InitBcplRuntime
	RamImage
		// LoadRam
	LoadRam
		// TimeIO
	CONVUDT
]


let micinit(mbfile) = valof
  [	if LoadRam(RamImage) eq 0 then InitBcplRuntime()
	lchan = dsp
	lversion()
	let infile = vec filenamelength
	let lsfile = vec filenamelength
	let erfile = vec filenamelength
	let stfile = vec filenamelength
	let rsfile = vec filenamelength
	let xlistf,binf,ltof,rsf,ucf,stf,lllflag = false,true,false,false,false,true,nil
	let fels = "LS"
	let feer = "ER"
	let fest = "ST"
	let femc = "MC"
	let fixname = "Micro.fixups"
	ttochan = dsp
	ettchan = ttochan
	rv infile = 0
	rv lsfile = 0
	rv erfile = 0
	rv mbfile = 0
	rv stfile = 0
	rv rsfile = 0
	comchan = OpenFile(0, ksTypeReadOnly, charItem, 0, fpComCm)
	if comchan eq 0 then error("No Com.Cm")
	let arg, option = vec filenamelength, vec 30
	SetupReadParam(arg, option, comchan, option)
	for i = 1 to option!0 do
	 switchon option!i&#137 into
	 [ case $L: xlistf = true; endcase
	   case $N: binf = false; endcase
	   case $O: stf = false; endcase
	   case $U: ucf = true; endcase
	 ]
	let sources = 0
	let lsource = lv sources
	let nfiles = 5
	  [	if ReadParam($P, -1, arg, option) eq -1 break
		let src = true
		let ucf = false
		for i = 1 to option!0 do
		  [	switchon option!i & #137 into
			 [ case $B:
				binf = true
				copyfile(mbfile,arg)
				endcase
			   case $L:
				xlistf = true
				copyfile(lsfile,arg)
				endcase
			   case $E:
				copyfile(erfile,arg)
				endcase
			   case $S:
				stf = true
				copyfile(stfile,arg)
				endcase
			   case $R:
				rsf = true
				copyfile(rsfile,arg)
				endcase
			   case $U:
				ucf = true
				loop
			   default:
				loop
			 ]
			src = false
		   ]
		if src then
		  [	copyfile(infile,arg)
			filext(arg,arg,femc)
			src = alloc((length(arg) rshift 1)+3+lFP)
			@src = 0
			src!1 = ucf
			copyfile(src+(2+lFP), arg)
			@lsource = src
			lsource = src
			nfiles = nfiles+1
		   ]
	   ] repeat
	if sources eq 0 then error("No source files")
	if length(rsfile) ne 0 then filext(rsfile, nil, fest)
	if length(mbfile) eq 0 then
	  [	filext(mbfile, infile, "")
		mbfile>>BS.length = mbfile>>BS.length-1	// remove '.'
	   ]
	filext(erfile,mbfile,feer)
	if xlistf then filext(lsfile,mbfile,fels)
	filext(stfile,mbfile,fest)
	let namevec = alloctemp(nfiles)
	let prvec = alloctemp(nfiles*lDV)
	namevec!0 = (length(rsfile) eq 0? 0, rsfile)
	namevec!1 = fixname
	namevec!2 = erfile
	namevec!3 = (length(lsfile) eq 0? 0, lsfile)
	namevec!4 = (stf? stfile, 0)
	let p = sources
	for j = 5 to nfiles-1 do
	[ namevec!j = p+(2+lFP)
	  p = @p
	]
	// Call LookupEntries with the largest possible buffer
	let dir = OpenFile(0, ksTypeReadOnly, wordItem, 0, fpSysDir)
	let bot, top = fslim, fstop+fsbot
	LookupEntries(dir, namevec, prvec, nfiles, true, bot, top-bot)
	Closes(dir)
	// Set up error streams first
	erlchan = OpenFile(erfile, ksTypeWriteOnly, charItem, verLatestCreate, prvec+(2*lDV+1))
	let est = alloc(lST)
	est>>ST.par1 = ttochan
	est>>ST.par2 = erlchan
	est>>ST.puts = putboth
	ettchan = est
	lchan = erlchan
	lversion()
	Resets(comchan)
	let ch = nil
	until Endofs(comchan) do
	[ ch = Gets(comchan)
	  lchr(ch)
	]
	if ch ne $*N then lcrlf()
	lcrlf()
	Closes(comchan)
	test length(rsfile) eq 0
	ifso	inchan = 0
	ifnot
	  [	inchan = OpenFile(rsfile, ksTypeReadOnly, wordItem, verLatest, prvec+1)
		if inchan eq 0 then
			error("/R FILE DOES NOT EXIST")
	   ]
	fixchan = OpenFile(fixname, ksTypeReadWrite, wordItem, verLatestCreate, prvec+(lDV+1))
	lstchan = (length(lsfile) eq 0? ttochan, OpenFile(lsfile, ksTypeWriteOnly, charItem, verLatestCreate, prvec+(3*lDV+1)))
	symchan = (stf? OpenFile(stfile, ksTypeWriteOnly, wordItem, verLatestCreate, prvec+(4*lDV+1)), 0)
	p = sources
	for j = 5 to nfiles-1 do
	[ MoveBlock(p+2, prvec+j*lDV+1, lFP)
	  p = @p
	]
	xlistflag = xlistf
	binflag = binf
	ltoflag = ltof
	ucflag = ucf
	lllflag = length(lsfile) ne 0
	errcnt = 0
	lllength = (lllflag? 82, 72)
	dalloctemp(prvec)
	dalloctemp(namevec)
	resultis sources
   ]

and lversion() be
[	llstr("*NMicro of ")
	ldec(version rshift 12)
	lchr($-)
	ldec((version & 7777b)/100)
	lchr($-)
	ldec((version & 7777b) rem 100)
	lcrlf()
	let dts = vec 10
	CONVUDT(dts, 0)
	llstr("  at ")
	lstr(dts)
	lcrlf()
]