{STARTUP program for spoolers etc}
{New version allowing: spooler code, host process id, queue name and  upto}
{three integer 'START' parameters to be separately specified              }

{STARTUP program is parameterised so that the spooling volume can be selected}
{and the batch limit and batch process name root set appropriately:          }
{      STARTUP  SPL,2,BG       for example                                   }
{If no parameters are supplied, they default to those in the example above   }


!! %OPTION  "DECODE,NOOPEN,STACK=15"
%begin;         !startup program

%owninteger      BATCH LIMIT = 2      {Adjustable 0..26}
%ownstring(7)    SPOOL VOL   = "SPL",
                 BATCH ROOT  = "BG"

{Process ids used for LP, PP etc}
%conststring(7)  potter pid  = "POTTER",
                 LP     pid  = "LP",
                 PP     pid  = "PP",
                 CR     pid  = "CR",
                 INIT   pid  = "INIT",
                 RELAY  pid  = "RELAY"

%include "sysinc:parmfm.inc"
%include "sysinc:command.inc"
%externalstring(31)%fnspec  ItoS(%integer  N,p)


   %shortinteger count, j
   %integer potter proc

   %integerfn  S to I (%string(23)  s)
      %integer  j,N=0
      %for j = 1,1,length(s) %cycle
         %unless '0'<=charno(s,j)<='9' %start
            print string("* * STARTUP:  faulty number ".S.SNL.SNL)
            %result = 0
         %finish
         N = 10*N + charno(S,j)-'0'
      %repeat
      %result = N
   %end
   %record(filefm)%fn  PACKED (%string(31)  fd)
      %record(parmfm)  p
      string(addr(p_sact)) = fd;  svc(17,p)
      %if p_file_owner < 0 %start
         print string("* * * STARTUP:  faulty process/file ident = ".fd.snl.snl)
      %finish
      %result = P_file
   %end
   %routine CREATE (%string(6) process, pass)
      %record(parmfm)  p,q
      P_file = packed( SPOOL VOL."_".process.":" )
      Q_file = packed(pass.":")
      p_dact = 31;  p_p3 = q_p2
      svc(20, p)
      %if p_p6 = 0 %start
         printstring("Process ".process." created")
         newline
      %finish
   %end
   %integerfn  STARTED (%string(31) spooler code, %string(7) process)
      {Start SPOOL:spoolercode.ABS as 'process' and return the process no.}
      %record(parmfm) p
      P_file = packed("SPOOL:" . spooler code . ".ABS")      {stylised names}
      p_p6 = 1<<31;            !privileged
      p_p5 = p_p4
      p_p4 = p_p3
      p_p3 = p_p2
      p_p2 = packed(process.":")_owner
      svc(109, p)
      printstring(process)
      %if p_p6 = 0 %start
         printstring(" started as process")
         write(p_p3, 1)
         newline
         %result = p_p3;         !process number
      %else
         printstring(" -- ")
         printstring(p_text)
         newline
      %finish
      %result = 0
   %end
   %routine KICK (%integer process, %string(7) Qname, %integer X,Y,Z)
      {Send 'process' its startup message which normally consists of a}
      {packed  Queue identifier (P3) optionally followed by three     }
      {integer parameters (P4..P6)                                    }
      %record(parmfm) p
      %return %if process = 0
      p_p1 = 0 {for consistency}
      p_p2 = 0 { . . . . . . . }
      p_p3 = 0 { . . . . . . . }
      p_p3 = packed(Qname.":")_owner %if Qname # ""
      p_p4 = x
      p_p5 = y
      p_p6 = z
      p_dsno = process!64;  p_dact = 1
      p_sact = 0
      svc(115, p)
   %end
   %routine WAIT (%integer time)
      %shortinteger h
      %while time > 0 %cycle
         time = time-1
         h = h+1 %until h = 0
      %repeat
   %end

   { ======= S T A R T    H E R E ======= }
   SPOOL VOL   = COMMAND_in1 %if command_in1 # ""       { ... else default}
   BATCH LIMIT = StoI(COMMAND_in2) %if command_in2 # ""
   BATCH ROOT  = COMMAND_in3 %if command_in3 # ""

   create(potter pid, "bits")
   potter proc = started("potter", potter pid)
   create(lp pid, "bits")
   create(pp pid, "bits")
   create(cr pid, "bits")
   create(init pid, "bits")
   create(relay pid, "bits")
   create(batch root.tostring(j), "bits") %for j = 'a', 1, 'a'-1+batch limit

   kick(potter proc, "",0,0,0)
   wait(3)
   kick( started("spoollp",lp pid), "lp", 0,0,0)
   kick( started("spoolpp",pp pid), "pp", 0,0,0)
   kick( started("spoolcr",cr pid), "cr", 0,0,0)
   kick( started("init", init pid), "batch", batch limit, 0, 0)
   kick( started("relay", relay pid), "", 0,0,0)

   print string("
Spoolers started on ".spool vol."
Max. batch limit is ".ItoS(batch limit,0)." streams
Batch process root is ".batch root.SNL)

%endofprogram
