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

   %ownstring(255)s="", outfile="",eventmess=""
   %integer threshhold, no of dirs, i, j,part
   %string (255) File,a,b
   %string (7) %array dirs(0:511)
   %integerarray filsiz(-1:3000)
   %integerarray dirsiz(-1:3000)
   

%routine get dirs(%integer partition,
%string (255) mask, %integername No of Dirs, %string (7) %arrayname dirs)
  !Get the list of directories on the specified partition

%integerfn  part enquiry(%integer part, bit, address, %integername size)
   %integer blockno
   %string (255) blk

   blockno = part<<1+bit
   blk = tostring((blockno&255)+'0')
   size = fcommr('\'<<8,blk, byteinteger(address), 532)
!t!printstring("SI "); phex(size); newline
   %result=0
%end

%routine move(%integer bytes, %bytename from, to)
   *Subq.l #1, d0
f loop:
   *move.b (a0)+, (a1)+
   *dbra   d0, f loop
%end

%recordformat DINFO FM(%string (7) name, %shortinteger partno, dirno)

  %routine do bit(%integer bit)
     %string (7) dir
     %integer i, len
     %bytearray buff(0:511)

     len = 512
     i = part enquiry(partition, bit, addr(buff(0)), len)
!t!  printstring("PE"); write(i, 1); newline
     %if i = 0 %start
!t!     write(len, 3); newline
!t!     %for i = 0, 1, len-1 %cycle; printsymbol(buff(i)); %repeat
!t!     newline
        %for i = 0, 1, len>>3-1 %cycle
           move(7, buff(i<<3), charno(dir, 1)); length(dir) = 7
           length(dir) = length(dir) - 1 %while dir # "" %and %c
           charno(dir, length(dir)) = ' '
!t!        printstring(dir); newline
           %if dir # "---" %and (mask = "" %or matches(dir, mask)) %start
              dirs(no of dirs) = dir; no of dirs = no of dirs + 1
           %finish
        %repeat
     %finish
!t!  printstring("DI"); write(no of dirs, 1); newline
  %end

  do bit(0); do bit(1)
%end

%routine part unpack finfo(%string(127)s,%record(finfof)%name r)
%integer pos=1
  %routine scan
    pos = pos+1 %while pos<=length(s) %and charno(s,pos)=' '
  %end
  %integerfn d
  %integer n=0,k
    scan
    %cycle
      %result = n %if pos>length(s); pos = pos+1; k = charno(s,pos-1)-'0'
      %result = n %if k<0 %or k>9; n = n*10+k
    %repeat
  %end
  %routine w(%string(*)%name t,%integer max)
  %integer k
    scan; t = ""
    %cycle
      %returnif pos>length(s) %or max<=0
      k = charno(s,pos); pos = pos+1; max = max-1
      %returnif k=' '
      t = t.tostring(k)
    %repeat
  %end
  r = 0
  w(r_name,23); pos = pos + 15
  r_blocks = d; r_extents = d
%end

   %routine check files(%string (15) directory)
      %string (127) Line, Temp, s
      %integer i, Index
      %record (finfof) fr

      temp = finfo(directory, 0)
      %if temp -> temp.("blocks: ").s %and s -> s.("/").temp %start
         i = stoi(temp)//10
         i=-1 %if i>3000
         dirsiz(i) = dirsiz(i)+1
      %finish

      index=1
      %cycle
         temp = finfo(directory, index)
         part unpack finfo(temp, fr)
         i = fr_blocks//10
         i = -1 %if i>3000
         filsiz(i) = filsiz(i) + 1
         index=index+1
      %repeatuntil fr_name = ""

   %end


   %for i = 0,1,3000 %cycle
      filsiz(i)=0; dirsiz(i)=0
   %repeat

   file = ""; outfile = ""
   define param("DIR", file, pam major)
   define param("OUT", outfile, 0)
   process parameters(cli param)

   %if file -> a.("*").b %start
      no of dirs=0
      %for part=0,1,7 %cycle
      get dirs(part, file, no of dirs, dirs)
      %repeat
   %else
      dirs(0) = file; no of dirs = 1
   %finish

   open output(1, outfile) %and selectoutput(1) %if outfile # ""

   %for i=0,1,no of dirs-1 %cycle
      checkfiles(dirs(i))
   %repeat

   %for i=0,1,149 %cycle
      %for j=0,1,19 %cycle
      write(filsiz(i*20+j), 3)
      %repeat
      newline
   %repeat
   New Line
   write(filsiz(-1),3); newline

   %for i=0,1,149 %cycle
      %for j=0,1,19 %cycle
      write(dirsiz(i*20+j), 3)
      %repeat
      newline
   %repeat
   New Line
   write(filsiz(-1),3); newline

%endofprogram
