// pjinit.sr

// Last modified September 25, 1982  12:45 PM by Taft

get "BRAVO1.DF";
get "AltoFileSys.D";
get "HEAP.DF";
get "DISPLAY.DF";
get "MEASURE.DF";
get "OM.DF";
get "vm.Df";
get "State.Df";
get "FONT.Df";
get "SELECT.Df";


// Incoming Procedures

external [
	ult;
	errhlta
	move;
	array
	hpinit
	addbp
// 	dumpcore
	ugt
	movec
	array1;
	initom;
	chuckinit;
	findomspace;
	hplay;
	chuckinitselect;
	initsb;
	lastproc
	FtyOpen
	ReadVec;
	umax
	RealDA
	swat
	hpalloca
	EnuStu
	InputStuAb
	activateomseq
	fnalloc
	OutSims
	EnuSimInit
	deallocfn
	deactivateomseq
	MyFrame
	chuckinitformat
	chuckinitdoc
	chuckinitdisplay
	InitCalc
	VirtualDA
	removefont
	flushfn
	puts
	FindMacPos;
	underline
	DAYTIME
	InitVrldsys
	createdocm
	updatedisplay
	selectwholeww
	flushvm
	invalidatewindow
	Init2Rls
	hpcompact;
	CreateVrldsys
	CacheInit
	InitMsg
	fillinfdd
	getbin
	SetRegionW
	SetRegionSys
	FddlMaxInit
	CwFddl;
	FCkUserOf
	errhlt
	initvm
	InitFdh;
	ReadDiskKd;
	WriteDiskKd;
	initkbd
	pjinit2
	];


// Incoming Statics

external [
	freet;
	freee;
	mpfunsb;
	rgcdlblank;
	rgmaccp;
	macww;
	vpzone;
	mpfnof;
	fillInDA;
	eofDA;
	DCread;
	dnbp;
	cominstream
	pzone
	stackroot
	tsmacro
	vmtb
	rgbp
	rgvpa
	rglastused
	rgbs
	macbp
	lrutime
	dnfn
	macfn
	cvmfree
	rgnchlast
	vdeltafp
	begofopsys;
	USERNAME;
	mpfnsb;
	SavedDiskKd;
	diskKd;
	vup;
	macfda;
	curfda;
	vcwaitfd;
// 	vputmsbomseq;
// 	vputomfixedstor;
// 	vcfreemin;
	vmeasurestatus;
	voverlay;
// 	vputsbomseq;
	vkcb;
	fpDiskDescriptor;
	fpSysLog;
	vFNoLog;
	vCfaEndRes;
	OsVersion;
	ozone
	ozonel
	vfdd0
	vwaCode
	vcaCode
	vbbs
	fpSysDir
	selmain
	vtodstart
	vcpagehc
	vbttotd
	vbttoti
	mpWwWwd
	vllDcb
	macmn
	mpmnom
	cpscrt
	vcache
	vbp
	rgmpbifb
	vrlwsys
	fpUserCm
	vSkfd
	vfddfirst
	vfontFixed;
	vfddlFixed;
	vcwFontFixed;
	crefKd
	diskBTsize
	nTracks
	nSectors
	nDisks
	nHeads
	defaultVersionsKept
	vswitch
	cfaSysDirEnd
	vyorig
	vdcborig
	vwaitproc
	daPgn1Kd;
	];

// Outgoing Procedures

external [
	pjinit1;
	PreJuntaInit
	];

// Outgoing Statics

external [
	SavedUsername;
	vfIniting
	vmacFrScrInit
	]; 

// Local Statics

static [
	SavedUsername;
	spyloc
	vfIniting
	vmacFrScrInit
	];

manifest [ 
	vcNewest = 2; vcNewestOrNew = 4
	docuserfirst = 7
	risyspast = 3;	// com.df
	cwOhdSv = 14+32+#300	// 14 for SV.H
				// 32 for SV.BLV
				// #300 for SV.page0
	globalSwitches=1	//From Executive -- body is ASCII codes
	] 

structure BLV:
	[ blank	word	26
	startOfStatics	word
	endOfStatics	word
	startOfCode	word
	afterLastCodeWord	word
	endCode	word
	blank	word
	] 

structure UPE[		//User param format: 0 terminates
	type bit 12
	length bit 4
	]


// P J I N I T
//
let pjinit1(stacktop) be
[ pjinit2(stacktop);
let cwInitCode1 = (lastproc-chuckinitselect) & -2
hplay(-cwInitCode1,true)
ozone = chuckinitselect
let bbs = vbbs;
test bbs >> BBS.fInstalled ifnot
	[ activateomseq("JVQT",0,0)
// 	unless FtyOpen(fnDiskDesc,"DiskDescriptor",false,true,vcNewest,fpDiskDescriptor) then
// 		errhlta(48);
	] 
ifso	unless FCkUserOf() then
		[ voverlay = true;
		activateomseq("JQT",0,0)
		deallocfn(fnuser)
		FtyOpen(fnuser,"USER.CM",false,false,vcNewestOrNew)
		deactivateomseq("JQT","JQT")
		voverlay = false;
		] 

// creates(fnopsys,"$OPSYS",true,false);
// dumpcore(fnopsys, begofopsys, (#176777-begofopsys+1) lshift 1);
// setmacfp(fnopsys,((mpfnof ! fnopsys) >> OF.macpos) << PCD.p+2);
// freee = #176777;
// freet = rv #335;
// let stackloc = array(stackl);
// movestack(init,stackloc+stackl-1); // ,lv pstack,... if any
// rv #335 = stackloc;
// freet = freet+1000;
// unless hplay(-1000,false) do errhlt("hpl");
// while macbp ls 12 do addbp(array)
// let tsizKd = lKDHeader+diskBTsize;
// diskKd = array(tsizKd)
// unless diskBTsize eq diskKd >> KD.diskBTsize then errhlta(49);
// lastSN = lv (diskKd >> KD.lastSn);
// diskBitTable = lv (diskKd >> KD.diskBitTable);
// This string cannot be longer than 40 characters
let sbVer = "BRAVO  Version 7.6  September 25, 1982"
chuckinit(bbs,sbVer);

unless bbs >> BBS.fInstalled then
	[ bbs >> BBS.pArrayF = freee-25
	deactivateomseq("JVQT","JVQT")
	] 
unless ozonel eq 0 then errhlt("ozl")
let cwInitCode2 = (chuckinitselect-chuckinit) & -2
hplay(-cwInitCode2,true)
ozone = chuckinit
// let tcspput1 = findomspace("MG")
// let tcspput2 = findomspace(vmeasurestatus << MEASURESTATUS.p ? "ILJK","IJ")
// vcfreemin = vputomfixedstor + umax(tcspput1,tcspput2);
test bbs >> BBS.fInstalled ifnot
	[ activateomseq("BHJZQT",0,1300)
	let fnState = fnalloc()
	FtyOpen(fnState,"Bravo.State",true,true,vcNewestOrNew)
// moved up so we can toss init code
// 	deactivateomseq("JV","V")
	CreateVrldsys()
	deactivateomseq("BHJZQT","BHJZQT")
	activateomseq("M",0,0)
	Init2Rls()
	deactivateomseq("M","M")
	createdocm(docuserfirst);
	createdocm(docuserfirst+1);
	activateomseq("BJbTQ",0,4500)
	let tfreee = freee;
	let tfddlMax = FddlMaxInit()
	vcwFontFixed = CwFddl(tfddlMax)+10;
	unless ugt(tfreee-freet, vcwFontFixed) then errhlt("fin");
	vfontFixed = freet;
	let fddl = vfddfirst
	until fddl eq 0 do
		[ fillinfdd(fddl)
		fddl = fddl >> FDD.fddnext
		] 
	InitFdh();
	deactivateomseq("BJbTQ","BJbTQ")
	flushvm()
	while removefont(true) do
		loop
	vfddlFixed>>FDD.font = 0;
	vfddlFixed = 0;
	let rgFn = table [ fnscr; fnscrfs ] 
	for i = 0 to 1 do
		[ let of = mpfnof ! (rgFn ! i)
		let pgn = ((of >> OF.macpos) rshift 9)+1+1
		let rgda = lv of >> OF.rgda
		for i = 0 to of >> OF.macfp do
			[ if rgda ! pgn eq eofDA then
				rgda ! pgn = fillInDA
			if rgda ! pgn eq fillInDA then
				break
			]
		] 
	let macBiScrInit = (mpfnof ! fnscr) >> OF.macbi
	let fb = getbin(fnscr,macBiScrInit-1)
	vmacFrScrInit = fb >> FB.macfr
	activateomseq("[QT",0,0)
	ReadDiskKd();
	let stm = ozone+ozonel
	let cwMax = vpzone >> ZONE.cfree-50
	hplay(cwMax,true)
	OutSims(EnuSimInit,0,stm,cwMax);
	(mpfnof ! fnState) >> OF.pos = 0;
	bbs >> BBS.cwStm = stm >> STM.lnmod
	for iw = 0 to stm >> STM.lnmod-1 do
		puts(fnState, stm ! iw);
	flushfn(fnState);
// 	dumpcore(fnState,stm,stm >> STM.lnmod lshift 1)
	hplay(-cwMax,true)
	WriteDiskKd();
	deactivateomseq("[QT","[QT")
	activateomseq("JQT",0,0)
	let ofState = mpfnof ! fnState
	bbs >> BBS.aCfa.fp.version = ofState >> OF.version
	move(lv (ofState >> OF.sn1),lv (bbs >> BBS.aCfa.fp.serialNumber),2)
	bbs >> BBS.aCfa.fp.leaderVirtualDa = VirtualDA(ofState >> OF.rgda)
	bbs >> BBS.aCfa.fa.da = ofState >> OF.rgda
// things that should be 0 are not set explicitely
	bbs >> BBS.fInstalled = true
// write it out on bravo.run
	FtyOpen(fnput,"bravo.run",false,true,vcNewest,vCfaEndRes,true)
	let of = mpfnof ! fnput;
	of >> OF.wf = true; of >> OF.macpos = -1;
	of >> OF.pos = (vwaCode+(bbs-vcaCode)) lshift 1
	for i = 0 to lbbs-1 do
		puts(fnput,bbs ! i)
	flushfn(fnput)
	deallocfn(fnput)
	deactivateomseq("JQT","JQT")
	] 
ifso	[ ]
//	if vmeasurestatus ne 0 then
//	[ vbttoti = array1(2,0);
//	vbttotd = array1(2,0);
//	vcpagehc = 0;
//	vtodstart = array1(lntod);
//	DAYTIME(vtodstart);
//	FindMacPos(fnmeasure)
//	(mpfnof ! fnmeasure) >> OF.pos = (mpfnof ! fnmeasure) >> OF.macpos;
//	] 
vFNoLog = false
voverlay = true
// fillinfdd(vfdd0)
InitVrldsys()
(mpWwWwd ! 1)>>WWD.doc = docuserfirst;
(mpWwWwd ! 1)>>WWD.cpFDispl = 0;
(mpWwWwd ! 0)>>WWD.cpFDispl = 0;
(mpWwWwd ! 2)>>WWD.doc = docuserfirst+1;
(mpWwWwd ! 2)>>WWD.cpFDispl = 0;
SetRegionW(vrlwsys,0,sbVer);
// let trid = 0
// trid<<RID.nrl = 1
// trid<<RID.ri = 0
let trid = 1 lshift 11
SetRegionSys(risyspast,trid)
(mpWwWwd ! 0)>>WWD.cpFDispl = 0;
invalidatewindow(0)
invalidatewindow(1)
invalidatewindow(2)
]