%begin; !Delete file {,file}*
%include "inc:util.imp"
%include "inc:fs.imp"
%constinteger PATHSEP=':'
%string(255) parm,file

%routine OPEN DIRECTORY(%string(255)%name parm)
%integer i
%string(255) dir
  dir = ""
  i = length(parm)
  i = i-1 %while i > 0 %and charno(parm,i) # pathsep
  %if i > 0 %start
    dir = substring(parm,1,i)
    parm = substring(parm,i+1,length(parm))
  %finish
  dir = dir."directory"
  open input(1,dir)
%end

%routine READ FILENAME(%string(255)%name s)
%integer k
  select input(1)
  s = ""
  read symbol(k) %until k > ' '
  %cycle
    k = k+32 %if 'A' <= k <= 'Z'
    s = s.tostring(k) %if k # '?'
    read symbol(k)
  %repeat %until k <= ' '
%end

%routine DELETE IF SURE(%string(255)file)
%bytename j
%integer i,k,kk,e
  %on %event 3,9 %start
    printsymbol('*')
    printstring(event_message)
    newline; %return
  %finish
  e = 0
  %for i = 1,1,length(file) %cycle
    j == charno(file,i)
    j = j+32 %if 'A'<=j<='Z'
    e = e<<8+j %if j = '.' %or e # 0
  %repeat
  k = 'y'
  %if j # '!' %and e # '.mob' %and e # '.lis' %start
    select input(0)
    prompt("Do you REALLY want to delete ".file."? ")
    read symbol(k) %until k > ' '
    read symbol(kk) %until kk < ' '
  %finish
  delete(file) %if k!32 = 'y'
%end

%predicate COVERS(%string(255) template,word)
%integer tl,wl
%predicate MATCHED(%integer ti,wi)
  %cycle
    %if ti = tl %start
      %true %if wi = wl
      %false
    %finish
    ti = ti+1
    %exit %if charno(parm,ti) = '*'
    %false %if wi = wl
    wi = wi+1
    %false %unless charno(template,ti) = '?' %c
           %or charno(template,ti) = charno(word,wi)
  %repeat
  %cycle
    %true %if matched(ti,wi)
    %false %if wi = wl
    wi = wi+1
  %repeat
%end
  
  tl = length(template)
  wl = length(word)
  %true %if matched(0,0)
  %false
%end

%routine DELETE FILES(%string(255) s)
%integer i
%string(255) f
%on %event 3,9 %start
  %return
%finish
  %for i = 1,1,length(s) %cycle
    %if charno(s,i) = '*' %or charno(s,i) = '?' %start
      open directory(s)
      %cycle
        read filename(f)
        delete if sure(f) %if covers(s,f)
      %repeat
    %finish
  %repeat
  delete if sure(s)
%end

  parm = cliparam
  %while parm -> file.(",").parm %cycle
    delete files(file)
  %repeat
  delete files(parm) %unless parm=""
%endofprogram
