// oinit1.sr


get "BRAVO1.DF";
get "CHAR.DF";
get "MEASURE.DF";
get "ALTOFILESYS.D";
get "VM.DF";
get "FONT.DF";
get "PARAM.DF";
get "BFS.DEF";

// Incoming Procedures

external [
	creates;
	stcompare;
	setreplay;
	stcopy;
	stappend;
	puts;
	stsize;
	slget;
	movec;
	array;
	stget;
	endofs;
	gets;
	slput;
	stput;
	sbwsize;
	sttoint;
	ult;
	divmod;
	ratio;
	errhlt;
	DAYTIME;
	move;
	processtsesc;
	normalizefilesb;
	owritemacro;
	FcFindLabel
	FcGetParam
	FindMacPos
	doDc
	RealDA
	];

// Incoming Statics

external [
	vchterm;
	mpfnof;
	fnts;
	tsmacro;
	mpfunfd;
	vfddfirst;
	quitchar;
	vmeasurestatus;
	vbttotd;
	vbttoti;
	vcpagehc;
	vtodstart;
	USERNAME;
	vfdd0;
	tsread;
	vswitch
	fpUserCm
	vkcb
	];

// Outgoing Procedures

external [
	chuckinitparams;
	createfd;
	chuckinitmeasure;
	inituserparams;
	setprofileparams;
	getparam;
	FCkUserOf;
	]; 

// Outgoing Statics

// Locals Statics

// Local manifest

manifest
	[ 
//	swloc = #1002;
	maxbuf = 5000;
	] 

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

structure KLABEL:
	[ next	word
	previous	word
	blank	word
	numchars	word
	pgn	word
	version	word
	sn	word 2
	] 

manifest	[ lnklabel = (size KLABEL)/16
	DcCkHeaderCkLabelRdData = #44120;
	] 

// O I N I T P A R A M S
//
let chuckinitparams(sbVer) be
[ let tsl = nil;
let sbuserinit = vec 20;
sbuserinit ! 0 = 0;
if vswitch eq -1 then
	[ processtsesc($V,sbVer,true);
	return
	] 
(mpfnof ! fncom) >> OF.pos = 0;
FindMacPos(fncom)
let prm = vec lprmovh+144; prm >> PRM.cchMax = 288
let fcFirst = 0
for i = 1 to 2 do
	fcFirst = FcGetParam(fncom,fcFirst,prm)
if (prm >> PRM.pt eq ptflag) then
 	[ let sb = lv prm >> PRM.astr
	@sb = @sb & (not #40)
	if stcompare(sb,"R") eq 0 then
		[ FindMacPos(fnts); FindMacPos(fnscr); setreplay();
		] 
	] 

(mpfnof ! fnts) >> OF.pos = 0;
processtsesc($V,sbVer,true);
if prm >> PRM.pt eq ptNil then return
unless tsread then
	[ stcopy(sbuserinit,lv prm >> PRM.astr);
	stappend(sbuserinit,".INIT");
	FindMacPos(fnuser)
	let fc = FcFindLabel(sbuserinit,prm,fnuser,0,"BRAVO")
	if prm >> PRM.pt eq ptNil then goto pfret;
	fc = FcGetParam(fnuser,fc,prm,false)
	if prm >> PRM.pt ne ptsl then goto pfret;
	let tpos = (mpfnof ! fnts) >> OF.pos;
	unless owritemacro(fnts,lv prm >> PRM.astr,omacro1) goto pret;
	(mpfnof ! fnts) >> OF.pos = tpos;
	setreplay();
	tsmacro = true;
	] 
pfret:
goto pret;
pret:
return;
] 


// M A C R O 1
//
and omacro1(fn,n) be
[ let prm = vec lprmovh+30; prm >> PRM.cchMax = 60
let fcFirst = 0
n = n+2
for i = 1 to n do
	fcFirst = FcGetParam(fncom,fcFirst,prm)
test prm >> PRM.pt eq ptsl ifso
	[ let sl = lv prm >> PRM.astr;
	for i = 0 to (rv sl)-1 do
		puts(fn,slget(sl,i));
	] 
ifnot	[ let sb = lv prm >> PRM.astr;
	for i = 0 to stsize(sb)-1 do
		puts(fn,stget(sb,i));
	] 
] 

// F C K U S E R H I N T
//
and FCkUserOf() = valof
[ let klabel = vec lnklabel; let ofUser = mpfnof ! fnuser
for i = 1 to 5 do
	[ movec(klabel,klabel+lnklabel-1,0);
	move((lv (ofUser>>OF.fileid)),(lv (klabel>>KLABEL.version)),3)
//	klabel>>KLABEL.pgn = 0; ** done by movec !
// 	klabel>>KLABEL.version = fpUserCm>>FP.version
	doDc(ofUser >> OF.rgda, DcCkHeaderCkLabelRdData, klabel);
	while (rv nextDiskCommand) ne 0 do loop
	if (vkcb>>KCB.status & DSgoodStatusMask) eq DSgoodStatus then
		resultis true
	] 
resultis false;
]