// IfsOvCopy.bcpl -- IFS-specialized portion of overlay init
// Copyright Xerox Corporation 1979, 1980, 1981
// Last modified November 12, 1981  4:55 PM by Taft

get "AltoFileSys.d"
get "Streams.d"
get "Disks.d"
get "Ifs.decl"
get "IfsInit.decl"

external
[
// outgoing procedures
CopyOverlays; DeclareAllOEPs

// incoming procedures
OpenFile; JumpToFa; GetCompleteFa; PositionPtr
ReadBlock; WriteBlock; Closes; ReadLeaderPage
Allocate; Free; Zero; MoveBlock; IFSError; Min
InitDisksOEP; InitDirOEP; InitBTreeOEP; InitStringOEP
InitFtpServOEP; InitFTPOEP; InitRSMgrOEP; InitSwapUtilOEP; InitCopyDiskOEP
InitTelnetOEP; InitCmdScanOEP; InitKeywordOEP; InitBackupOEP; InitPressOEP
InitDiskStreamsOEP; InitMailOEP; InitMiscellaneousOEP; InitLeafOEP
InitGrapevineOEP

// incoming statics
sysZone; isb
]

//----------------------------------------------------------------------------
let CopyOverlays(disk, cfaRunFile, cfaSwapFile) be
//----------------------------------------------------------------------------
// Called with the cfa of the run file on the Diablo disk.
// Copies the overlays to the swap file on "disk" and stores
//  the cfa of the swap file in cfaSwapFile
[
let swapPageSize = 1 lshift disk>>DSK.lnPageSize
let windowPage = Allocate(sysZone, swapPageSize)

let runFile = OpenFile(0, ksTypeReadOnly, 0, 0, lv cfaRunFile>>CFA.fp)
if runFile eq 0 then IFSError(ecDiabloFile, "IFS.Run")
ReadLeaderPage(runFile, windowPage)
MoveBlock(lv isb>>ISB.runFileCreated, lv windowPage>>LD.created, 2)
JumpToFa(runFile, lv cfaRunFile>>CFA.fa)
let swapFile = OpenFile("IFS.Swap", ksTypeReadWrite, 0,
 verLatest, 0, 0, sysZone, 0, disk)
if swapFile eq 0 then IFSError(ecTridentFile, "IFS.Swap")
GetCompleteFa(swapFile, cfaSwapFile)

// copy the overlays
   [
   if ReadBlock(runFile,windowPage,16) ne 16 break  //read header
   PositionPtr(runFile, 0)
   let lenOv = (windowPage!4+255)&-256  //words in overlay
   while lenOv gr 0 do
      [
      ReadBlock(runFile, windowPage, Min(swapPageSize, lenOv))
      WriteBlock(swapFile, windowPage, swapPageSize)
      lenOv = lenOv-swapPageSize
      ]
   isb>>ISB.numOverlays = isb>>ISB.numOverlays+1
   ] repeat

// put page of zeroes after last overlay to stop OverlayScan
Zero(windowPage, swapPageSize)
WriteBlock(swapFile, windowPage, swapPageSize)
Free(sysZone, windowPage)
Closes(runFile)
Closes(swapFile)
]

//----------------------------------------------------------------------------
and DeclareAllOEPs(oev) be
//----------------------------------------------------------------------------
[
InitDisksOEP(oev)
InitDirOEP(oev)
InitBTreeOEP(oev)
InitStringOEP(oev)
InitFTPOEP(oev)
InitFtpServOEP(oev)
InitTelnetOEP(oev)
InitRSMgrOEP(oev)
InitCmdScanOEP(oev)
InitKeywordOEP(oev)
InitSwapUtilOEP(oev)
InitBackupOEP(oev)
InitDiskStreamsOEP(oev)
InitMailOEP(oev)
InitMiscellaneousOEP(oev)
InitLeafOEP(oev)
InitPressOEP(oev)
InitCopyDiskOEP(oev)
InitGrapevineOEP(oev)
]