%constinteger true = 1, false = 0

%include "inc:util.imp"
%include "managr:addefs.inc"
%include "inc:fs.imp"
%include "inc:fsutil.imp"
%include "managr:admin.inc"

%begin

%constinteger fln = 255 ;!File and path names, host names
%constinteger uln = 31  ;!User names and passwords
%constinteger dln = 63  ;!Directories
%string (255) printdir  ;! "lp1" or "lp2"

%recordformat DTE FM(%bytearray a(1:6))                                     
%recordformat FS STAT FM(%byte no, %string (255) message)                   

%integerfn FS DEFAULT UTNO                                        
   !Dummy
   %result = 0
%end

%externalstring (255) %fnspec finfo(%string (dln) directory, %integer no)
%routine FS FINFO(%integer utno, %string (dln) directory,         
%integer fileno, %record (fs stat fm) %name status)                         
   !Dummy
   status_no = 0
   status_message = finfo(directory, fileno)
%end

%routine FS TRANSFER FILE (%integer s utno, %string (fln) s file,
%integer d utno, %string (fln) d file, %record (fs stat fm) %name status) 
   %string (fln) s,t
   d file = s.(".").t %while d file -> s.(":").t
   copy(s file, printdir.":".d file)
   !Dummy
%end

%integerfn FS FILESIZE (%integer utno, %string(fln) filename,        
%record (fs stat fm) %name status)                                          
  %on 3, 9 %start
      status_no = 1; %result = -1
  %finish

  status_no = 0
  %result = filesize(printdir.":".filename)
%end

%routine FS REPORT ERROR(%integer tno, %string (255) text,        
%record (fs stat fm) %name status)                                                
   !Dummy
%end

%routine FS LOGON FILESTORE(%string (fln) host, 
%string (uln) user, pass, %integername tno, %record (fs stat fm) %name status)
   !Dummy
!  %integer i
   status_no = 0
!  i = fcomm('Q'<<8, "")
%end

%routine FS LOGOFF FILESTORE(%integer no, %record (fs stat fm) %name status)
   !Dummy
   status_no = 0
%end

%owninteger i, ctno, laser utno, user utno, size, auta, autl

%predicate authorised
   %record (admin fm) data
   %false %unless find dir(current user, data)
   %true %if data_group & 1<<laser bit # 0
   %false
%end

%record (fs stat fm) status
%ownbyteinteger opt = 0, printer=0
%record (dte fm) laser dte
%ownstring (63) pling="",user="", param="", s="", t = ""

define param("File", param, 0)
define enum param("L24,L34 -- Copier Room 2419, Room 3421", printer, 0)
define enum param("NOqueue,Queue", opt, 0)
process parameters(cli param)

user utno = fs default utno
%if printer=1 %then printdir = "lp2" %else printdir="lp1"
fs logon filestore("bravo", printdir, "", laser utno, status)
fs report error(0, "Laser", status)

%if %not authorised %start
   printline("Sorry, you aren't accredited for LASER - see Linda Wilkie"); %return
%finish

%if param # "" %start
   param = param.".lay" %if exists(param.".lay") 
   !Try <filename>.lay first
   size = fs filesize(laser utno, param, status) ;!Size = -1 if file doesn't fs exist
   
   pling = ""
   %if status_no = 0 %and size >= 0 %start
      printstring(param. " is on the queue already. (O)verwrite, (Q)uit or (S)econd copy?")
      readsymbol(i) %until i & 16_5f = 'O' %or i & 16_5f = 'Q' %or i & 16_5f = 'S'
      %return %if i & 16_5f = 'Q'
      pling = "!" %if i & 16_5f = 'S'
   %finish

   %if exists(param) %start
      fs transfer file(user utno, param, laser utno, pling.param, status)
   %else
      printline("No access to ".param)
   %finish
%finish

! Now handle a -QUEUE request. basically just a FILES.

%if opt = 1 %start
   printstring("Laser queue")
   i = 1
   %cycle
      fs finfo(laser utno, printdir, i, status)
      %exit %if status_message = "" %or status_no # 0
      t = status_message %unless status_message -> t.(" ").s
      to lower(t); charno(t, 1) = charno(t, 1) & 16_5f
      printsymbol(':') %and newline %if i = 1
      printstring(t); %if i & 3 = 0 %then newline %else spaces(20-length(t))
      i = i + 1
   %repeat
   printline(" empty") %if i = 1
   newline %if i & 3 # 1
   fs report error(laser utno, "Laser Info", status)
%finish

fs logoff filestore(laser utno, status)
fs report error(0, "Laser", status)

%endofprogram
