-- Copyright (C) 1983  by Xerox Corporation. All rights reserved. 
-- PupBooterSlow.mesa, HGM, 23-Sep-83 14:14:30

DIRECTORY
  Ascii USING [CR],
  Put USING [Text],
  Runtime USING [IsBound],
  Stream USING [Handle],
  String USING [AppendChar, AppendString],
  Time USING [AppendCurrent],

  Window USING [Handle],
  Slosh USING [SendFile],
  BootServerDefs USING [WhatHappened, msg, UpdatePicture, statBootNew],
  BootServerFriends USING [
    BootFile, LockFileRead, ReadStreamFromBootFile, UnlockFile],
  PupDefs USING [AppendHostName],
  PupTypes USING [PupAddress],
  Stats USING [StatIncr];

PupBooterSlow: PROGRAM
  IMPORTS
    String, Time, Put, Runtime, Slosh, BootServerDefs, BootServerFriends, PupDefs,
    Stats
  EXPORTS BootServerDefs =
  BEGIN OPEN BootServerDefs;

  verbose: BOOLEAN = TRUE;
  shortSlowBooteeHost: STRING ← [30];
  slowBooteeHost: PUBLIC LONG STRING ← shortSlowBooteeHost;
  slowBootFileName: PUBLIC LONG STRING ← NIL;

  SlowBooter: PUBLIC PROCEDURE [
    bf: BootServerFriends.BootFile, him: PupTypes.PupAddress]
    RETURNS [what: WhatHappened] =
    BEGIN
    sh: Stream.Handle;
    IF ~BootServerFriends.LockFileRead[bf] THEN RETURN[diskBusy];
    slowBooteeHost.length ← 0;
    PupDefs.AppendHostName[slowBooteeHost, him];
    slowBootFileName ← bf.fileName;
    FixupPicture[];
    IF verbose THEN
      BEGIN
      text: STRING = [100];
      Time.AppendCurrent[text];
      String.AppendString[text, "  Booting "L];
      String.AppendString[text, bf.fileName];
      String.AppendString[text, " to "L];
      PupDefs.AppendHostName[text, him];
      LogString[msg, text];
      END;
    sh ← BootServerFriends.ReadStreamFromBootFile[bf];
    what ←
      SELECT Slosh.SendFile[msg, bf.fileName, sh, him] FROM
        ok => slow,
        neverStarted => neverStarted,
        troubles => troubles,
        ENDCASE => troubles;
    IF what = slow THEN Stats.StatIncr[statBootNew];
    slowBooteeHost.length ← 0;
    slowBootFileName ← NIL;
    FixupPicture[];
    BootServerFriends.UnlockFile[bf];
    END;

  FixupPicture: PROCEDURE =
    BEGIN
    IF msg # NIL AND Runtime.IsBound[LOOPHOLE[UpdatePicture]] THEN UpdatePicture[];
    END;

  LogString: PROCEDURE [msg: Window.Handle, text: LONG STRING] =
    BEGIN
    String.AppendChar[text, '.];
    String.AppendChar[text, Ascii.CR];
    Put.Text[NIL, text];
    IF msg # NIL THEN Put.Text[msg, text];
    END;

  END.