! Program to send files to the laser printer queue (b::lp1:). Can also be used
! to interrogate the queue and delete files from it.
! For dvi files, appends an optional string of commands to the printer.
%begin

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

%string(31)  filename,ext
%string(255) texparams


%constant %string(4) directory = "lp1:"

%constant %integer queue flag = 1<<31,
                   del flag   = 1<<30

%integer control flags,
         entry,
         local,
         remote


%predicate CONTROL FLAG(%integer n)

  %false %if control flags & n = 0
  %true
%end   { control flag }


%predicate AUTHORISED

   %record (admin fm) data

   %false %unless find dir(current user, data)
   %true %if data_group & 1<<laser bit # 0
   %false
%end   { authorised }


%routine GET FILENAME(%string(7) dir)

  filename = filename.".lay" %if exists(dir.filename.".lay")
  filename = filename.".dvi" %if exists(dir.filename.".dvi")
  filename = filename.".san" %if exists(dir.filename.".san")
  filename = filename.".pdf" %if exists(dir.filename.".pdf")
  to lower(filename)
  charno(filename,1) = charno(filename,1) !! 32
  printstring(filename." not found") %and newline %unless exists(filename)
  %if %not exists(filename) %or filename="" %start
   %cycle                           
      prompt("Filename: ")          
      readline(filename)
      %if filename="" %start
        %stop
      %finish
      filename = filename.".lay" %if exists(dir.filename.".lay")
      filename = filename.".dvi" %if exists(dir.filename.".dvi")
      filename = filename.".san" %if exists(dir.filename.".san")
      filename = filename.".pdf" %if exists(dir.filename.".pdf")
      to lower(filename)
      charno(filename,1) = charno(filename,1) !! 32
      printstring(filename." not found") %and newline %unless exists(filename)
    %repeat %until exists(filename)
  %finish
%end   { get filename }


%routine REMOVE ENTRY

  %string(31) file, paramfile, who
  %integer    flag, x, deleted, pamfile

  %on %event 9 %start
    close input
    select input(0)
    %if flag=0 %start
      printstring("No files found"); newline
    %finish %else %if deleted = 0 %start
      write(flag,0)
      printstring(" of your files in queue, couldn't delete entry ")
      write(entry,0); newline
    %finish
    %stop
  %finish

  flag = 0
  deleted = 0
  open input(1,directory."directory")  
  %cycle
    who = ""
    pamfile = 0
    readline(file)
    x = 1
    %cycle
      who = who.file(x)
      x = x+1
    %repeat %until (charno(file,x) >= '0' %and charno(file,x) <='9') %orc
                   x = length(file)

    %if file -> (".PAM") %start
      pamfile = 1
    %finish
    %if who = current user %and pamfile = 0 %start
      flag = flag+1
      %if flag = entry %start
        delete(directory.file)
        deleted = 1
        printstring(file." deleted"); newline

        %if file -> paramfile.(".DVI") %start  
          paramfile = paramfile.".PAM"         
        %finish                                

        %if exists(paramfile) %start                    { Delete parameters with }
          delete(directory.paramfile)                   { .dvi file.}
          printstring(paramfile." deleted"); newline
        %finish
      %finish
    %finish
  %repeat
%end   { remove entry }


%string(31) %function GET UNIQUE
{ Chooses a unique filename containing the current user and a job number }
{ chosen at random.}

  %string(31) tempfile,number

  %on %event 7 %start
     ext = ""
     -> isoceles
  %finish

  filename -> number.(".").ext

isoceles:
  %cycle
    number = itos(cputime,0)
    length(number) = 2
    tempfile = current user.number."B"
    %if ext # "" %start
      tempfile = tempfile.".".ext
    %finish
  %repeat %until %not exists(directory.tempfile)  
  %result = tempfile
%end   { get unique }


%routine PRINT

   %string(31) newfile,paramfile

   newfile = get unique
   %if ext = "dvi" %and texparams<>"" %start     { Write out parameters to }
     newfile -> paramfile.(".")                  { seperate file to be picked }
     open output(1,directory.paramfile.".pam")   { up by the dvi printer.}
     printstring(texparams); newline
     close output
     select output(0)
     printstring(paramfile.".pam parameter file written"); newline
   %finish
   printstring(newfile." queued for laserprinter");newline
   copy(filename,directory.newfile)
%end   { print }


%routine PRINT QUEUE

%string(31) file
%integer    read flag

  %on %event 9 %start
    close input
    select input(0)
    printstring("Empty") %and newline %if read flag = 0
    %return
  %finish

  read flag = 0
  printstring("Laserprinter queue:"); newline
  open input(1,directory."directory")
  %cycle
    readline(file)
    read flag = 1
    printstring(file); newline
  %repeat
%end   { print queue }


  filename = ""
  define param("File", filename, 0)
  texparams = ""
  define param("DVI", texparams, 0)
  control flags = 0
  define boolean params("Queue+DELete", control flags, 0)
  entry = 1
  define int param("ENTry", entry, 0)
  process parameters(cliparam)

  %if %not authorised %start
    printline("Sorry, you aren't accredited for LASER - see JHB")
    %return
  %finish
  close input
  select input(0)

!  local  = current host
!  remote = connect to host("::B:LP1,")
!  select host(remote)

  %if control flag(del flag) %start
    remove entry
  %finish %elsec
  %if control flag(queue flag) %start
    print queue
  %finish %else %start
    get filename("")
    print
  %finish
%endofprogram
