!Program relocates user to a new partition.
! In mid-hack.  DON'T USE IT YET!!!
!System password will have to be quoted.
!J. Butler Sep 85

%include "inc:ssi.imp"
%include "inc:fsutil.imp"

%begin

   %ownstring(255)s=""
   %integer newpart, i, st, quota
   %string (255) dir, newdir, prots
   
   %integerfn fcommr(%integer i, %string (255) s, %bytename a, %integer l)
      printstring("Fcommr "); phex4(i); space; printline(s)
      %result=0
   %end

   %integerfn fcomm(%integer i, %string (255) s)
      printstring("Fcomm  "); phex4(i); space; printline(s)
      %result=0
   %end

   %routine do copy(%string (255) olddir, newdir)
      %string (255) Line, Temp, s
      %integer i, Index
      %record (finfof) fr

      %routine copy(%string (255) oldname, newname)
         printstring(oldname); spaces(16-length(oldname))
         printstring(" -> ".newname)
         newline
      %end

      temp = finfo(olddir, 0)

      index=1
      %cycle
         temp = finfo(olddir, index)
         unpack finfo(temp, fr)
         copy(olddir.":".fr_name, newdir.":".fr_name) %unless fr_name=""
         index=index+1
      %repeatuntil fr_name = ""
   %end

   %routine rename user(%string (255) old, new)
   %integer i
     printline("No ownername specifed") %and %return %if old=""
     printline("Invalid length of ownername") %and %return %if length(old)>6
     printline("No new ownername specified") %and %return %if new=""
     printline("Invalid length of new ownername") %and %return %if length(new)>6
     old = old.":" %if charno(old,length(old))#':'
     new = new.":" %if charno(new,length(new))#':'
     i = fcomm('B'<<8,old.",".new)
     printline(old." renamed to ".new." on disc")
   %end
   
   
   %integerfn status(%string(31)s)
   ! Test whether owner S exists.
   ! Result <0 if not
   ! Result 0 if it has no files
   ! Result >0 otherwise
   %bytearray b(1:512)
     %on 3,9 %start
       %result = -1
     %finish
     s = s.",1"
     %result = fcommr('F'<<8,s,b(1),512)
   %end

%conststring (31) default protection = "FRA"
%constinteger default quota = 1984

%routine create user(%string (255) user, %integer partition, quota, %c
   %string (3) prots)
   %integer i, j, exist, ustat
   %string (255) t

   %string (255) %fn trim(%string (*) %name s, %integer len)
      %integer i
      length(s) = length(s) - 1 %while s # "" %and charno(s, length(s)) = ' '
      length(s) = len %if length(s) > len
      %result = s
   %end

   %routine append(%integer x)
     %integer y
     y = x&15+'0'
     x = x>>4; append(x) %unless x=0
     t = t.tostring(y)
   %end

   printline("No ownername specified") %and %return %if user=""
   ustat = status(user)
   %if ustat < 0 %start
      printstring("Create ".user." on partition ".itos(partition,-1))
      printstring("with quota ".itos(quota, -1)." and protection ".prots)
      newline
!!      t = tostring(partition).user
!!      t = t.","; append(quota)
!!      i = fcomm('['<<8,t)
!!      printline(user." created")
!!      t = s.":,".prots
!!      i = fcomm('E'<<8,t)
   %else
      printline(user." already exists on disc")
   %finish
%end

%routine kill user(%string (255) user)
  %integer i

  printline("No ownername specified") %and %return %if user=""
  i = status(user)
  %if i >= 0 %start
    %if i = 0 %start
!!        i = fcomm('D'<<8,user.":")
        printline(user." killed on disc")
    %finishelse printline(user." still has files") %and %return
  %finishelse printline(user." does not exist on disc")
%end

%routine get details(%string (255) dir, %integername quota, %c
%string (255) %name prots)
   %string (255) s,t
   s = finfo(dir, 0)
   s = t %while s -> s.("/").t
   quota = stoi(t)
   prots = "FNA"
%end

! Main Program

   dir = ""; newpart = -1
   define param("DIR", dir, pam major ! pam no default)
   define int param("New Partition", newpart, pam no default)
   process parameters(cli param)
   st = status(dir)

   printline("Move ".dir." to partition ".itos(newpart,-1))

   get details(dir, quota, prots)
   printline("User has quota ".itos(quota,-1)." and will have perms FNA")
   create user("###new", newpart, quota, prots)
   do copy(dir, "###new")
   rename user(dir,"###old")
   rename user("###new", dir)
   kill user("###old")

%endofprogram
