%begin; !RENAME: old1/new1,old2/new2,...
! JGH moded 24-Jan-84 to use PAM options instead of always '/'
! BSC modified to use wildcards 6/8/86

!%include "inc:ssi.imp"
%include "inc:util.imp"
%include "inc:fs.imp"
%include "inc:fsutil.imp"

%byte sep='/', log=0
%string(255)from,to,directory
%string(255) %array(0:500) in,out
%integer index,error

!-------------------------------------------------------------------------------
  %routine destn equals(%string(255) %name wildsource,wilddestn,s,d)
  ! Determines the output filename to give when wildcards are used
  ! eg Rename *.imp/*.mob when s=hello.imp produces result d=hello.mob
  
    %string(255) star1,star2,star3,bit1,bit2,bit3
    
    toupper(wildsource) { Standardise strings - case not significant }
    toupper(wilddestn)

    ! Split up the input wildcards - Max two *  
    ! Star1 contains all text before the first star etc  
    wildsource -> star1.("*").star2
    star3 = "" %unless star2 -> star2.("*").star3
   
    ! Chop up the input filename. bit1 contains the text that the first *
    ! stands for (etc)
    s -> (star1).bit1
    bit1 -> bit1.(star2).bit2
    bit2 -> bit2.(star3).bit3 
    
    ! Chop up the destination name to remove the *s
    wilddestn -> star1.("*").star2
    star3 = "" %unless star2 -> star2.("*").star3
   
    ! Create the output filename by replacing the *s with appropriate text.
    d=star1.bit1.star2.bit2.star3.bit3
    
  %end

!-------------------------------------------------------------------------------

%routine check directory

%string(255) dirout
  directory = current directory %unless from->directory.(":").from
  dirout = "" %unless to->dirout.(":").to
  
  %if (dirout # directory %and dirout # "") %then %c
                   Printline("I think you want to use copy.") %and %stop
%end

!-------------------------------------------------------------------------------

%routine read file
  
  %string(255) source
  %label cont
  
  %on %event 9 {eof} %start
    closeinput
    selectinput(0)
    ->cont
  %finish

  index = 0
  openinput(1,directory.":directory")
  selectinput(1)
  %cycle
    readline(source)
    %if matches(source,from) %then %start
      destn equals(from,to,source,out(index))
      in(index)=directory.":".source
      index=index+1
    %finish
  %repeat 
cont:
  %if index = 0 %then printline("No such file(s)") %and %stop
%end

!-------------------------------------------------------------------------------

%routine pair(%string(255)s)

%integer loop

  %onevent 3,4,9 %start
    printline(event_message); %return
  %finish

  loop = 0
  %returnif s=""
  %unless s -> from.(TO STRING(sep)).to %start
    printline("Rename old".TO STRING(sep)."new"); %return
  %finish
  ! Get all possibilities of 'from' and loop round renaming them all
  %if wildness(from) # wildness(to) %then %c
                   printline("Wildness mismatch error.") %and %stop
  check directory
  %if wildness(from)>0 %then %start
    readfile 

    %for loop = 0,1,index-1 %cycle
      error=0
      %if (out(loop)="" %or in(loop)="") %start
        error=1
        printline("Cannot rename ".in(loop)." to ".out(loop))
      %finish
      rename(in(loop),out(loop)) %unless error= 1
      print line (in(loop)." renamed to ".out(loop)) %if (log=1 %and error = 0)
    %repeat
  %finish %else %start
      rename(directory.":".from,to)
      print line (directory.":".from." renamed to ".directory.":".to) %if log=1
  %finish
%end

%string(255)line,one,rest

  sep = PAM_group sep %if pam_group sep >=' '
  line = cliparam.","
  %if line = "," %then printline("Try Rename old/new,old2/new2.....") %and %stop
  to upper (line)
  log=1 %and line = one.rest %if line -> one.("-L").rest
  %while line -> one.(",").rest %cycle
    pair(one)
    line = rest
  %repeat
%endofprogram
