-- Copyright (C) 1984, 1985  by Xerox Corporation. All rights reserved. 
-- PupGatewayWindow.mesa, HGM, 25-Jun-85  7:19:13

DIRECTORY
  FileSW USING [GetFile],
  MFile USING [Handle, SetTimes],
  MStream USING [GetFile, GetLength, SetLogReadLength],
  Put USING [CR, Text],
  Process USING [Detach, Pause, SecondsToTicks, SetPriority, Ticks],
  Stream USING [Handle],
  System USING [GetGreenwichMeanTime],
  Tool USING [Create, MakeSWsProc, MakeFileSW],
  ToolWindow USING [TransitionProcType],
  UserInput USING [
    CancelPeriodicNotify, CreateIndirectStringOut, CreatePeriodicNotify,
    DestroyIndirectStringOut, GetDefaultWindow, PeriodicNotifyHandle, PeriodicProcType],
  Window USING [Handle],

  GateDefs USING [GetVersionText],
  Stats USING [StatStart];

PupGatewayWindow: PROGRAM
  IMPORTS FileSW, MFile, MStream, Put, Process, System, Tool, UserInput, GateDefs, Stats
  EXPORTS GateDefs =
  BEGIN OPEN GateDefs;

  typescript: PUBLIC Window.Handle ← NIL;

  MakeSWs: Tool.MakeSWsProc =
    BEGIN
    typescript ← Tool.MakeFileSW[window: window, name: "PupGateway.Typescript"L];
    END;

  ClientTransition: ToolWindow.TransitionProcType =
    BEGIN
    SELECT TRUE FROM
      old = inactive => NULL;
      new = inactive => typescript ← NIL;
      ENDCASE;
    END;

  SetupWindow: PUBLIC PROCEDURE =
    BEGIN
    Push: PROCEDURE [s: LONG STRING, clientData: LONG POINTER] =
      BEGIN Put.Text[NIL, s]; END;
    version: LONG STRING = GateDefs.GetVersionText[];
    defaultWindow: Window.Handle = UserInput.GetDefaultWindow[];
    Put.CR[NIL];
    Stats.StatStart[Push, version];
    [] ← Tool.Create[
      name: version, makeSWsProc: MakeSWs, clientTransition: ClientTransition,
      cmSection: "PupGateway"L, initialState: active];
    UserInput.DestroyIndirectStringOut[defaultWindow];
    UserInput.CreateIndirectStringOut[from: defaultWindow, to: typescript];
    Process.Detach[FORK UpdateTypescript[]];
    FixNotifier[];
    END;

  
  UpdateTypescript: PROCEDURE =
    BEGIN
    oneMinute: Process.Ticks = Process.SecondsToTicks[60];
    sh: Stream.Handle ← FileSW.GetFile[typescript].s;
    fh: MFile.Handle ← MStream.GetFile[sh];
    DO
      Process.Pause[oneMinute];
      MStream.SetLogReadLength[sh, MStream.GetLength[sh]];
      MFile.SetTimes[file: fh, create: System.GetGreenwichMeanTime[]];
      ENDLOOP;
    END;

  trash: UserInput.PeriodicNotifyHandle;
  FixNotifier: PROCEDURE =
    BEGIN
    trash ← UserInput.CreatePeriodicNotify[SmashNotifiersPriority, typescript, 1];
    END;

  SmashNotifiersPriority: UserInput.PeriodicProcType =
    BEGIN
    Process.SetPriority[1];
    trash ← UserInput.CancelPeriodicNotify[trash];
    END;

  END.