// IfsScavPass2.bcpl
// Copyright Xerox Corporation 1980
// Last modified November 16, 1980  2:38 PM by Boggs

get "IfsScavenger.decl"
get "CmdScan.decl"

external
[
// outgoing procedures
Pass2

// incoming procedures
Pass2Phase1; Pass2Phase2; Pass2Phase3; Scratch
PutTemplate; Ws; Wss; ScavConfirm
Gets; Errors; Closes; OpenFile; ReadBlock
TFSInit; OpenDisk; CloseDisk
OpenLPT; CloseLPT; GetLptFs; GetLptHome
MultEq; IFSError; Free; Dequeue; UnsnarfBuffer

// outgoing statics
tree

// incoming statics
sysZone; keys; dsp; debugFlag; initLptFlag
scratchDisk; scavDisk; pass; lpt; tfsDDMgr
]

static tree

//---------------------------------------------------------------------------
let Pass2(buffPtr, numBuffs) be
//---------------------------------------------------------------------------
[
pass, scavDisk, lpt = 2, 0, 0
let ok, fsAndDrive = true, -1
if initLptFlag then
   [
   Ws("*NI Haven't scanned any packs yet.")
   test ScavConfirm("*NDo you want to re-use an old lpt? ")
      ifso initLptFlag = false
      ifnot ok = false
   ]

if ok & scratchDisk eq 0 then
   unless Scratch(nil) do ok = false

if ok then
   [
   lpt = OpenLPT("IfsScavenger.lpt", false)
   let fsAndDrive = GetLptFs(lpt)!0

   ok = valof
      [  //repeat
      test fsAndDrive eq -1
         ifso
            [
            scavDisk = OpenDisk("*NWhere is logical unit 0? ")
            if scavDisk eq 0 resultis false
            ]
         ifnot
            [
            scavDisk = TFSInit(sysZone, true, fsAndDrive, tfsDDMgr)
            if scavDisk eq 0 then [ fsAndDrive = -1; loop ]
            ]

      let ifsHome = OpenFile("Ifs.home", 0, 0, 0, 0, 0, 0, 0, scavDisk)
      if ifsHome eq 0 then
         [
         scavDisk = CloseDisk(scavDisk, true)
         test fsAndDrive eq -1
            ifso Ws("*NI Can't open Ifs.home on that pack.")
            ifnot fsAndDrive = -1
         loop
         ]

      let home = vec lenHome
      ReadBlock(ifsHome, home, lenHome)
      Closes(ifsHome)
      let lptHome = GetLptHome(lpt)
      test home>>Home.thisUnit eq 0 &
       MultEq(lv home>>Home.created, lv lptHome>>Home.created)
         ifso resultis true
         ifnot
            [
            scavDisk = CloseDisk(scavDisk, true)
            test fsAndDrive eq -1
               ifso Ws("*NThat pack is not logical unit 0.")
               ifnot fsAndDrive = -1
            ]
      ] repeat
   ]

// Finally ready to get down to work
if ok then ok = Pass2Phase1(buffPtr, numBuffs)
for i = 0 to numBuffs-1 do UnsnarfBuffer(buffPtr+i*256)
if ok then ok = Pass2Phase2()
if ok then ok = Pass2Phase3()

// clean up and go away
if scavDisk ne 0 then scavDisk = CloseDisk(scavDisk, true)
if lpt ne 0 then lpt = CloseLPT(lpt, ok)
if ok then Ws("*NPass2 complete*N")
]