// oinit.sr

get "BRAVO1.DF";
get "HEAP.DF";
get "DISPLAY.DF";
get "MSG.DF";
get "DOC.DF";
get "SELECT.DF";
get "VM.DF";
get "FORMAT.DF";
get "ALTOFILESYS.D";
get "STATE.DF";

// Incoming Procedures

external [
	array;
	array1
	DAYTIME
	FindMacPos
	hpinit;
	SplitWw;
	createdoce;
	createdocm;
	updatedisplay;
	selectwholeww;
	underline;
	errhlta
	movec;
	move;
	stcopy;
	hpalloca;
	ult;
	hplay
	qinitfcode;
	chuckinitparams;
	CreateFd;
	fnalloc;
	estscrrun;
	cpfc;
	chuckinitmeasure;
	processtsesc;
	InitCalc;
	inituserparams;
	lastproc
	initCnDcb
	formaty
	makeroomww
	chaseCnDcb
	replaceCnDcb
	updatewindow
	incCnDcb
	max
	mpDlDld
	fillinfdd
	umax
	InstallParams
	FillInFont
	FtyOpen
	InitCom
	Init1Rls
	];

// Incoming Statics

external [
	vbttoti
	vbttotd
	vcpagehc
	vtodstart
	freet;
	fnts;
	mpfnof;
	tsread;
	vmeasurestatus;
	freee;
	rgyfirst;
	vyorig;
	macww;
	vheightd;
	ppcd;
	rgmaccp;
	doctxcurrent;
	selarg;
	selmain;
	rgmpbifc;
	rgmpbifb;
	vformattedfile;
	vcpadjusteq;
	tstoflush;
	ccommand;
	fpComCm;
	fpUserCm;
	vww
	vdl
	vYdofDl
	mpWwWwd
	vfdd0
	vup
	vpzone
	vytop
	vybot
	vymid
	vxleftmarg 
	vxleftmargstd 
	vxrightmarg 
	vxrightmargstd 
	vxleftmargfstd 
	vffakeether 
	vbbs
	vdxtbStd
	vrgridpast2
	vcridpast2
	];

// Outgoing Procedures

external [
	chuckinit;
	chuckinitselect;
	chuckinitbravocom;
	chuckinitdoc;
	chuckinitdisplay;
	chuckinitformat;
	];

// Outgoing Statics

external [
	vdcbsys;
	vdcborig;
	vysys;
	pzone;
	inword;
	inwordnext;
	vdcbblack;
	parstd;
	vfnpx;
	vllDcb
	]; 

// Local Statics

static [
	vdcbsys;
	vdcborig;
	vysys;
	pzone;
	inword;
	inwordnext;
	vdcbblack;
	parstd;
	spyloc;
	vfnpx;
	vllDcb
	];

// Local structures
// structure SW:
// 	[ ANYCOMCM bit 1;
// 	ANYREMCM bit 1;
// 	blank bit 14
// 	] 

// Local manifest
manifest
	[ 
// 	swloc = #1002;
	maxbuf = 5000;
	vcNewest = 2; vcNewestOrNew = 4
	ftyNil = -1
	cridMax = 10
	] 

// B R A V O
let chuckinit(bbs,sbVer) be
[

// spyloc = array(#2240);
// let shrinkamount = chuckinitselect-lastproc;
// if shrinkamount << odd then shrinkamount = shrinkamount+1;
// hplay(shrinkamount,true);
unless bbs >> BBS.fInstalled then
	[ FtyOpen(fnts,"bravo.ts",true,false,vcNewestOrNew);
	(mpfnof ! fnts) >> OF.fFullPages = true;
// 	creates(fnts,"bravo.ts",true,false);
// 	(mpfnof ! fnts) >> OF.wf = true;
	FtyOpen(fncom,"COM.CM",false,false,vcNewestOrNew,fpComCm);
	FtyOpen(fnuser,"USER.CM",false,false,vcNewestOrNew,fpUserCm);
	FtyOpen(fnscrfs,"bravo.scratchbin",true,true,vcNewestOrNew)
	FtyOpen(fnscr,"bravo.scratchbravo",true,false,vcNewestOrNew)
	(mpfnof ! fnscr) >> OF.fFullPages = true;
	if FtyOpen(fnfontw,"fonts.widths",false,true,vcNewest) eq ftyNil then
		errhlta(17);
	// (mpfnof ! fnscr) >> OF.wf = true;
	(mpfnof ! fnscr) >> OF.formatted = true;
	rgmpbifc ! fnscr = hpalloca(maxbiscr+1);
	rgmpbifb ! fnscr = hpalloca(maxbiscr);
	movec(rgmpbifb ! fnscr,rgmpbifb ! fnscr+maxbiscr-1,-1);
	InitCom()
	Init1Rls()
	] 
vrgridpast2 = array(cridMax)
vcridpast2 = 0
chuckinitbravocom( );
// vfnpx = fnalloc();
// FtyOpen(vfnpx,"px.temp",true,true,vcNewestOrNew);
tsread = false;
// *** following is some croquery to find space for reading user.cm
unless bbs >> BBS.fInstalled then
	[ CreateFd(array1);
	InstallParams()
	] 
if vmeasurestatus ne 0 then
	[ 
// 	vtodstart = array1(lntod,0);
// 	DAYTIME(vtodstart);
// 	vbttoti = array1(2,0);
// 	vbttotd = array1(2,0);
// 	vcpagehc = 0;
	FindMacPos(fnmeasure)
	(mpfnof ! fnmeasure) >> OF.pos = (mpfnof ! fnmeasure) >> OF.macpos;
	] 

vxleftmarg = vxleftmargstd;
vxrightmarg = vxrightmargstd;
parstd >> PAR.xrightmarg = vxrightmargstd;
parstd >> PAR.xleftmarg = vxleftmargstd;
parstd >> PAR.xleftmargf = vxleftmargfstd;
parstd >> PAR.dxtb = vdxtbStd;
for ww = 0 to maxww-1 do
	(mpWwWwd ! ww)>>WWD.xdUd = vxleftmargstd rshift 5
vffakeether = false;
// stcopy(sbpast,"B R A V O  Version 5.7 June 14, 1976");
chuckinitparams(sbVer);
let shrinkAmount = freet-freee
if shrinkAmount << odd then shrinkAmount = shrinkAmount+1
// 	if freee << odd then freee = freee-1;
// 	if tfreet << odd then tfreet = tfreet+1;
hplay(shrinkAmount,false);
// topofheap = freee;
freet = freee-1;
// rgyfirst ! 0 = tytop;
// rgylast ! 0 = tybot;
// rgdoc ! 0 = 0;
// rgcpfdispl ! 0 = 0;
// rgdlfirst ! 0 = 0; rgdllast ! 0 = -1;
// macww = macww+1;
// rgdoc ! 1 = docuserfirst;
// fillinfdd(vfdd0)
createDisplay(vytop,vybot);
let wgd = vec lwgd
wgd>>WGDTABLE.cww = 2
wgd>>WGDTABLE.doc↑1 = 1
wgd>>WGDTABLE.ydFirst↑1 = vymid
wgd>>WGDTABLE.dyd↑1 = dydsysww
wgd>>WGDTABLE.cpFDispl↑1 = 0
wgd>>WGDTABLE.bonw↑1 = 1
wgd>>WGDTABLE.fHd↑1 = false
wgd>>WGDTABLE.mag↑1 = 100
wgd>>WGDTABLE.xdUd↑1 = xleftmargstd rshift 5
// document window
wgd>>WGDTABLE.doc↑2 = 2
wgd>>WGDTABLE.ydFirst↑2 = vymid+dydsysww
wgd>>WGDTABLE.cpFDispl↑2 = 0
wgd>>WGDTABLE.bonw↑2 = 0
wgd>>WGDTABLE.dyd↑2 =vybot-wgd>>WGDTABLE.ydFirst↑2
wgd>>WGDTABLE.fHd↑2 = false
wgd>>WGDTABLE.mag↑2 = 100
wgd>>WGDTABLE.xdUd↑2 = xleftmargstd rshift 5
(mpWwWwd ! 1)>>WWD.wwgroup = 3		//super hack for splitww
SplitWw(vymid,wgd,3);
(mpWwWwd ! 1)>>WWD.wwgroup = 1
(mpWwWwd ! 2)>>WWD.wwgroup = 1
(mpWwWwd ! 2)>>WWD.fDirty = false;
(mpWwWwd ! 3)>>WWD.wwgroup = 3
// for dl = rgdlfirst ! 1 to rgdllast  ! 1 do
// 	rgxdlast ! dl = -1;
doctxcurrent = 1;
selarg >> SEL.type = snone;
// rgcpfdispl ! 1 = 0;
// rgcpfdispl ! 0 = 0;
(mpWwWwd ! 0)>>WWD.cpFDispl = 0;
(mpWwWwd ! 1)>>WWD.cpFDispl = 0;
(mpWwWwd ! 2)>>WWD.cpFDispl = 0;
updatedisplay();
vformattedfile = true;
] 

// I N I T B R A V O C O M 
// catalogue no. 
and chuckinitbravocom( ) be
[ unless vbbs >> BBS.fInstalled then
	[ createdoce(doctx1,10);
	createdoce(doctx2,10);
	createdoce(doctx3,10);
	createdoce(doctx4,5);
	createdoce(doctx5,5);
	createdoce(docremark,5);
	] 
// doctxcurrent = 1;
vcpadjusteq = 0;
tstoflush = 20;
ccommand = 0;
] 

// C R E A T E D I S P L A Y
// catalogue no.
and createDisplay(ydStart,ydEnd) be
[ ydStart = (ydStart+1) & (-2)
ydEnd = (ydEnd+1) & (-2)
let tCnDcb = vec lCnDcb
initCnDcb(tCnDcb,0,0)
incCnDcb(tCnDcb,0,ydStart,0,0,false)
incCnDcb(tCnDcb,0,2,0,0,true)
let wwd = mpWwWwd ! 0
wwd>>WWD.dcbFirst = tCnDcb>>CNDCB.dcbLast
incCnDcb(tCnDcb,0,ydEnd-ydStart,0,0,false)
incCnDcb(tCnDcb,0,2,0,0,true)
(mpWwWwd ! 1)>>WWD.dcbFirst = tCnDcb>>CNDCB.dcbLast
incCnDcb(tCnDcb,0,2,0,0,false)
@lvCnDcbRoot = 0
vllDcb = tCnDcb>>CNDCB.dcbFirst
macww = 1
rgyfirst ! 0 = ydStart
rgyfirst ! 1 = ydEnd
// rgylast ! 1 = ydEnd
wwd>>WWD.doc = 0;
wwd>>WWD.cpFDispl = 0;
wwd>>WWD.dlFirst = 0; wwd>>WWD.dlLast = -1;
]