!***************************************************************************
!*                                                                         *
!* Program to generate APM front panel labels. Uses NS.DAT database        *
!*                                                                         *
!*                  Version 1.2    8 Dec 1987                              *
!*                                                                         *
!***************************************************************************

!1.2: uses modified NS recordformat (inon and room merged, new bootfs field)
%begin
%include "nsdefs.inc"
%include "inc:util.imp"
%string (255) s,lname,sname,client,server,outfile, ether,serial,location
%string (255) devices, rest,param
%conststring (31) data file = "managr:ns.dat"
%record (ns fm) %name file
%record (ns lfm) %name data
%string (1) esc
%conststring(255) text = "This should come out * "
%integer i,count,dte,first dte,last dte,start,size
%conststring(1) %array hex1(0:15)=
"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"

%string (2) %fn hex2(%integer i)
   %result = hex1(i>>4&15).hex1(i&15)
%end

%routine print sized(%string(255) s, %integer x,y)
   !Print the string, scaled X in the x (horizontal) direction and
   !Y in the vertical direction.   Return to a scale of 1:1 afterwards.
   %integer i
   printstring(esc."0".tostring(x-1+' ')) %if x#1
   printstring(esc."1".tostring(y-1+' ')) %if y#1
   printstring(s)
   printstring(esc."0 ") %if x#1
   printstring(esc."1 ") %if y#1
%end

%routine tidy(%string (255) %name s, %integer n)
   !Delete runs of more than N spaces from the string.
   %string (255) t
   %integer i,m,c
   t=""; m=0
   %return %if s=""
   %for i=1,1,length(s) %cycle
      c=charno(s, i)
      %if c=' ' %start
         %if m=n %start
            !Have a run of N spaces in output string already - discard
         %else
            t=t." "; m=m+1
         %finish
      %else
         t=t.tostring(c); m=0
      %finish
   %repeat
   s=t
%end

%routine pad(%string (255) %name s, %integer len)
   s=" ".s %while length(s)<len
%end

%predicate assigned(%integer dte)
   %integer i
   %true %if file_l(dte)_case # 0
   %for i=1,1,16 %cycle
     %true %if file_l(dte)_bpmap(i) # '.'
   %repeat
   %true %if file_l(dte)_bpmap(17) # '*' %or file_l(dte)_bpmap(18) # '*'
   %false
%end

%on 9 %start; ->eof; %finish
esc = tostring(27)

!Requires parameters "station/outfile"

param = cli param
outfile = "" %unless param -> param.("/").outfile
dte=0 %if param=""
printline("Funny value ".param) %and %stop %if dte<0 %or dte>max addr
%if param="" %start
   first dte = 1 ; last dte = max addr;!everything
%else
!!   first dte = hex(param) ;last dte = first dte
%finish


connect file(data file, 0, start, size)
file == record(start)
!comment line at front - print to user and discard
printline("""".file_comment.""""); newlines(2)
openoutput(2, outfile) %and selectoutput(2) %if outfile#""

count=0
%for dte=first dte,1,last dte %cycle
   data == file_l(dte)
   %if assigned(dte) %start

      !Labels have 12 lines (just) * 48 characters on itoh  (2" * 4")
      client = hex2(dte)
      sname=""
      %for i=1,1,7 %cycle; sname=sname.tostring(file_s(dte)_d(i)); %repeat
      lname=""
      %for i=1,1,31 %cycle; lname=lname.tostring(data_lname(i)); %repeat
      server = tostring(data_bootfs)

      print sized("  Machine :",1,1); print sized(client, 4,4); newline
      %if charno(sname,1) = '@' %start
         print sized("  Server  :",1,1); print sized(server, 4,4)
      %else
         print sized(" ".lname,2,4)
      %finish
      newline
      serial = itos(data_case,3)
      location = ""
      %for i=1,1,4 %cycle; location=location.tostring(data_add(i)); %repeat

      print sized("  Name    :",1,1); print sized(sname,   1,1)
      print sized("  Serial  :",1,1); print sized(serial, 1,1); newline
      print sized("  Location:",1,1); print sized(location,1,1); newline
      print  line("  Report problems to Linda Wilkie on 2783")
      count=count+1
      %if count=5 %then printsymbol(12) %and count=0
   %finish
%repeat

eof: close input; close output
%endofprogram
