// IfsTelnetSysParam1.bcpl -- Commands to change system parameters
// Copyright Xerox Corporation 1979, 1980, 1981, 1982
// Last modified July 24, 1982  5:26 PM by Taft

get ecBadRName from "Grapevine.decl"
get opReadRemark from "GrapevineProtocol.decl"
get "Ifs.decl"
get "IfsSystemInfo.decl"
get "IfsFiles.decl"
get "IfsRs.decl"
get "IfsTelnet.decl"

external
[
// outgoing procedures
SysParamSubcommand

// incoming procedures
InstallSysParams; SelectKeyword; TelnetCommandQuit
GetNumber; GetString; GetGroupName; SetGroupName; ReadRString
DefaultPhrase; DefaultTemplate; Confirm; AbortCmd
EnableCatch; EndCatch; DisableCatch; CopyString; ExtractSubstring
ResetTimeServ; EnableTimeServ; EnableNameServ; InitCachedDIF
EnableBootServ; EnablePress; EnableLeaf; EnableCopyDisk; EnableLookupFileServ
VFileReadPage; VFileWritePage; LockCell; UnlockCell; Password
Puts; PutTemplate; Ws; Wss; Errors; MoveBlock
FalsePredicate; SysFree; FreePointer; IFSPrintError

// incoming statics
CtxRunning; dsp
maxJobs; lenJobT; infoVMD; entFlag; ClockSecond; leafPresent
enablePasswordLog; enableGrapevineAuth; enableGrapevineGroup; defaultRegistry
]

//---------------------------------------------------------------------------
let SysParamSubcommand(cs, entry, sp) be
//---------------------------------------------------------------------------
[
Puts(cs, $*s)
switchon entry!0 into
   [
   case 1:  // Quit
      TelnetCommandQuit(cs)

   case 2:  // Server-Limit
      [
      DefaultTemplate(cs, "$D", maxJobs)
      let n = GetNumber(cs)
      if n-1 uge lenJobT then Errors(cs, 0)
      sp>>SysParams.maxJobs = n
      endcase
      ]

   case 3:  // Clock-correction
      [
      let corr = sp>>SysParams.clockCorrection
      Wss(cs, "(seconds per day) ")
      DefaultTemplate(cs, "$S$D", (corr ge 0? "+", ""), corr)
      sp>>SysParams.clockCorrection = GetNumber(cs)
      endcase
      ]

   case 4:  // Reset-Time
      if Confirm(cs) then ResetTimeServ()
      endcase

// SysParamSubcommand (cont'd)

   case 5:  // Group-Name
   case 6:  // World
      [
      let group = nil
      test entry!0 eq 5
         ifso
            [
            Wss(cs, "(of group) ")
            group = GetNumber(cs)
            if group uge nGroups then Errors(cs, 0)
            Wss(cs, " (is) ")
            ]
         ifnot
            [ Wss(cs, "(group name is) "); group = size Protection ]
      let name = 0
      if EnableCatch(cs) then [ FreePointer(lv name); EndCatch(cs) ]
      name = GetGroupName(group)
      if name ne 0 then [ DefaultPhrase(cs, name); FreePointer(lv name) ]
      name = GetString(cs, 0, Wss, "Grapevine R-Name for group")
      if enableGrapevineGroup & name>>String.length ne 0 then
         [
         // Read the Remark from Grapevine -- only groups have Remarks
         let ec = nil
         let remark = ReadRString(name, opReadRemark, lv ec)
         test remark eq 0
            ifnot SysFree(remark)
            ifso if ec eq ecBadRName then
               Wss(cs, "*n[Caution: no such name exists in Grapevine]")
         ]
      if Confirm(cs) then
         [
	 let ec = SetGroupName(group, name)
         if ec ne 0 then IFSPrintError(dsp, ec)
         ]
      SysFree(name)
      endcase
      ]

   case 7:  // Default-Registry
      [
      let name = 0
      if EnableCatch(cs) then [ FreePointer(lv name); EndCatch(cs) ]
      DefaultPhrase(cs, lv sp>>SysParams.defaultRegistry)
      name = GetString(cs, 0, Wss, "registry name")
      if name>>String.length ge size SysParams.defaultRegistry/8 then
         Errors(cs, 0)
      if Confirm(cs) then
         [
         CopyString(lv sp>>SysParams.defaultRegistry, name)
         InitCachedDIF()
         ]
      SysFree(name)
      endcase
      ]

// SysParamSubcommand (cont'd)

   case 8:  // Enable
   case 9:  // Disable
      [
      let enable = entry!0 eq 8
      let which = SelectKeyword(cs, "Logins", "Time-server", "Name-server",
       "Boot-server", "Press-printing", "Leaf-server", "CopyDisk-server",
       "New-boot-files", "Password-logging", "Grapevine", "LookupFile-server")
      if (000140B lshift which) ls 0 % Confirm(cs) then
         [
         switchon which into
            [
            case 1:
               entFlag = enable; endcase
            case 2:
               sp>>SysParams.enableTimeServ = enable
               EnableTimeServ(enable)
               endcase
            case 3:
               sp>>SysParams.enableNameServ = enable
               EnableNameServ(enable)
               endcase
            case 4:
               sp>>SysParams.enableBootServ = enable
               EnableBootServ(enable)
               endcase
            case 5:
               sp>>SysParams.enablePress = enable
               EnablePress(enable)
               endcase
            case 6:
               sp>>SysParams.enableLeaf = enable
               unless EnableLeaf(enable) do
                  Ws("*n[Done, but IFS must be restarted before this will take effect.")
               endcase
            case 7:
               sp>>SysParams.enableCopyDisk = enable
               EnableCopyDisk(enable)
               endcase
            case 8:
               sp>>SysParams.noNewBootFiles = not enable
               EnableBootServ(sp>>SysParams.enableBootServ, not enable)
               endcase
            case 9:
               if enable then
                  [
                  Wss(cs, " (password) ")
                  let password = GetString(cs, 0, 0, 0, FalsePredicate)
                  let ok = Password(password, table
                    [
                    -1; 164633B; 6516B; 52503B; 27231B
                    160471B; 140737B; 123164B; 112341B
                    ], false)
                  SysFree(password)
                  unless ok do Errors(cs, 0)
                  ]
               if Confirm(cs) then sp>>SysParams.enablePasswordLog = enable
               endcase

// SysParamSubcommand (cont'd)
// Enable/Disable case

            case 10:
	       [
	       let what = SelectKeyword(cs, "Authentication", "Groups")
               if enable & defaultRegistry eq 0 then
                  AbortCmd(cs, "Please set a Default-Registry first")
	       if Confirm(cs) then
	          [
		  test what eq 1
		     ifso sp>>SysParams.enableGrapevineAuth = enable
		     ifnot sp>>SysParams.enableGrapevineGroup = enable
		  InitCachedDIF()
		  ]
               endcase
	       ]
            case 11:
               sp>>SysParams.enableLookupFileServ = enable
               EnableLookupFileServ(enable)
               endcase
            ]
         ]
      endcase
      ]
   ]

InstallSysParams(false)
]