// SwatInit.bcpl
// Copyright Xerox Corporation 1979, 1982, 1983
// Last modified October 12, 1983  11:22 AM by Taft

get "Swat.decl"
get "SysDefs.d"
get "Streams.d"
get "AltoDefs.d"

external
[
// outgoing procedures
AfterJuntaInit; BeforeJuntaInit

// incoming procedures from Swat
AfterJunta; SysErr; InitDisk; InitFailSys
InitExp; InitRead; InitSym; InitBreak
InitResident; InitInternalTraps
Enable; Disable

// incoming procedures from OS
CreateDisplayStream; ShowDisplayStream
GetLinePos; CharWidth; InvertLine; SetLmarg
InitializeZone; Allocate; Free
Junta; CounterJunta; OutLd; MyFrame
Zero; Puts; Resets; Wss; Ws; PutTemplate

// outgoing statics
swatVersion; state; xmFlag; debugFlag

// incoming statics
sysFont; OsVersion; sysZone; lvSysZone
fpSwat; dsp; keys
]

static
[
swatVersion = 33
lastShownStream; osDsp; state
xmFlag; debugFlag
]

manifest
[
dspLen = 30000b
stateLen = 6000b

stackLimit = 335b

white = 0
black = -1
]

//----------------------------------------------------------------------------
let AfterJuntaInit() = valof
//----------------------------------------------------------------------------
[
// Free storage initialization
let freeBegin = @stackLimit
let freeEnd = MyFrame() -2000
@stackLimit = freeEnd
sysZone = InitializeZone(freeBegin, freeEnd-freeBegin, 0, 0)
@lvSysZone = sysZone

lastShownStream = MakeBar(white, 30)
ShowDisplayStream(lastShownStream, DSalone)
ShowDisplayStream(MakeBar(white, 1), DSbelow, lastShownStream)

let lenDS = lDCB + 38*((sysFont!-2 +1) & -2) +1
let title = CreateDisplayStream(1, Allocate(sysZone, lenDS), lenDS,
 sysFont, 0, 0, sysZone)
PutTemplate(title, "                          Swat.$D - October 12, 1983",
 swatVersion)
InvertLine(title, GetLinePos(title))
MyShowDisplayStream(MakeBar(black, 2))
MyShowDisplayStream(title)
MyShowDisplayStream(MakeBar(black, 1))

dsp = CreateDisplayStream(45, Allocate(sysZone,dspLen), dspLen, sysFont)
SetLmarg(dsp, 32)
MyShowDisplayStream(dsp)
MyShowDisplayStream(MakeBar(black, 1))
MyShowDisplayStream(MakeBar(white, 5))
state = CreateDisplayStream(12, Allocate(sysZone,stateLen), stateLen, sysFont)
MyShowDisplayStream(state)
SetLmarg(state, 32)
MyShowDisplayStream(MakeBar(black, 1))

InitFailSys()
InitRead()
InitExp()
InitSym()
InitBreak()
InitResident()
InitDisk()

Disable()
let outLdResult = OutLd(fpSwat, 0)
Enable()

test outLdResult eq 0
   ifso
      [
      @displayListHead = 0
      CounterJunta(AfterCounterJunta)
      ]
   ifnot
      [
      InitInternalTraps()
      Resets(keys)
      resultis freeBegin
      ]
]

//----------------------------------------------------------------------------
and AfterCounterJunta() be
//----------------------------------------------------------------------------
[
Wss(osDsp, "*N*N*N")
Wss(osDsp, "*N   Swat, Swatee and Dumper.Boot are installed.")
Wss(osDsp, "*N   You may delete InstallSwat.Run if you wish.")
]

//----------------------------------------------------------------------------
and BeforeJuntaInit(upe) be
//----------------------------------------------------------------------------
[
while upe!0 ne 0 do
   [
   if upe>>UPE.type eq globalSwitches then
      for i = 1 to upe>>UPE.length-1 switchon upe!i into
         [
         case $X: case $x: [ xmFlag = true; endcase ]
         case $D: case $d: [ debugFlag = true; endcase ]
         ]
   upe = upe + upe>>UPE.length
   ]
osDsp = dsp  //save for AfterCounterJunta
test OsVersion ge 16
   ifso Junta(levDisplay, AfterJunta)
   ifnot Ws("*NThis SWAT works only with OS Version 16 or later*N")
]

//----------------------------------------------------------------------------
and MyShowDisplayStream(stream) be
//----------------------------------------------------------------------------
[
ShowDisplayStream(stream, DSbelow, lastShownStream)
lastShownStream = stream
]

//----------------------------------------------------------------------------
and MakeBar(background, nLines) = valof
//----------------------------------------------------------------------------
[
structure Bar: [ fdcb word; ldcb word; @DCB ]
manifest lenBar = size Bar/16
let bar = Allocate(sysZone, lenBar, false, true); Zero(bar, lenBar)
bar>>Bar.fdcb = lv bar>>Bar.next
bar>>Bar.ldcb = lv bar>>Bar.next
bar>>Bar.background = background
bar>>Bar.width = 0
bar>>Bar.height = nLines
resultis bar
]