-- GVSwitches.mesa
-- Brenda Hankins       17-Aug-84 12:18:50	(Klamath update)
-- Ted Wobber  		8-Mar-83 11:17:31

DIRECTORY
  Environment USING [bytesPerPage],
  Event,
  GVPServer,
  LogDefs USING [TypescriptOn],
  MFile USING [
    Acquire, Error, Handle, InitializeFileSystem, ReadWrite, Release, SetLength],
  SpecialMFile USING [RegisterWithSupervisor],
  MScavenge USING [StartingScavenge],
  PilotClient,
  ProcessorFace USING [mp, SetMP],
  Supervisor USING [CreateSubsystem, SubsystemHandle],
  System USING [switches, UpDown],
  TTY USING [Create, Destroy, GetDecimal, Handle, PutCR, PutString];

GVSwitches: MONITOR
  IMPORTS
    GVPServer, LogDefs, MFile, SpecialMFile, MScavenge, ProcessorFace, Supervisor,
    System, TTY
  EXPORTS Event, PilotClient =

  BEGIN
  swapping: PUBLIC Supervisor.SubsystemHandle;
  Run: PUBLIC PROCEDURE = {};

  InfiniteWait: ENTRY PROCEDURE =
    BEGIN
    forever: CONDITION;
    endOfNever: BOOLEAN ← FALSE;
    UNTIL endOfNever DO WAIT forever; ENDLOOP;
    END;

  MakeFile: PROC [name: LONG STRING, sizeInPages: CARDINAL ← 0] =
    BEGIN  -- if file already exists, nothing happens, else it's created.
    create: BOOLEAN ← FALSE;
    myfile: MFile.Handle;
    myfile ← MFile.Acquire[
      name, anchor, [], TRUE, sizeInPages, binary !
      MFile.Error => IF code = noSuchFile THEN {create ← TRUE; CONTINUE}];
    IF create THEN
      BEGIN
      sizeInBytes: LONG CARDINAL;
      IF sizeInPages = 0 THEN  -- ask for size
        BEGIN
        tty: TTY.Handle ← TTY.Create[];
        TTY.PutString[
          tty, "Creating Heap File, how many pages (multiple of 6)?  "L];
        sizeInPages ← TTY.GetDecimal[tty];
        TTY.PutCR[tty];
        TTY.Destroy[tty];
        END;
      sizeInBytes ← LONG[sizeInPages] * LONG[Environment.bytesPerPage];
      myfile ← MFile.ReadWrite[name, [], binary, sizeInBytes];
      MFile.SetLength[myfile, sizeInBytes];
      END;
    MFile.Release[myfile];
    END;

  StartTrap: PROCEDURE =
    BEGIN
    savedMP: CARDINAL ← LAST[CARDINAL];
    {
    ENABLE
      MScavenge.StartingScavenge => {
        -- start trap of FileSystem may raise this signal
        savedMP ← ProcessorFace.mp; ProcessorFace.SetMP[9950]; RESUME };
    swapping ← Supervisor.CreateSubsystem[];
    MFile.InitializeFileSystem[];
    -- according to Loretta, this call to Init. must be accomp. by this other
    -- mysterious stuff.  The signal MUST be caught, and not in the call itself.
    SpecialMFile.RegisterWithSupervisor[]};
    -- this must be done separately so that the testbed can run without Tajo's supervisor handles
    IF savedMP # LAST[CARDINAL] THEN ProcessorFace.SetMP[savedMP];
    IF System.switches['i] = down THEN
      BEGIN
      MakeFile["Heap.data"];
      MakeFile["MBX.Mailboxes", 120];
      -- ~67,584 bytes, the current size of Riesling's MBX.Mailboxes
      END;
    IF System.switches['s] = down THEN
      BEGIN
      LogDefs.TypescriptOn[];
      -- can now use LogPrivateDefs.tty to write to screen.
      START GVPServer;  -- this will reboot server.
      InfiniteWait[];
      END;
    END;


  -- Mainline code

  StartTrap[];

  END.