%include "inc:util.imp"
%begin
! PAM interface

%constinteger max params = 3,max filenames=20

%conststring(1) %array(1:max params) parameters="B","W","L"
%string(255) in,out
%string(1) key flag,group sep
%string(255) %array(1:max filenames) infiles,outfiles,group

%integer loop,group count,legal
%integer %array(1:max params) set params

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

%routine remove spaces(%string(255) %name line)

%string(255) temp
%integer looper
%string(1) char

  temp=""
  %for looper = 1,1,length(line) %cycle
    char = substring(line,looper,looper)
    %if char<>" " %then temp=temp.char
  %repeat
  line=temp

%end

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

%routine extract params(%string(255) %name in,out,
                            %integer %array(1:max params) %name params)

%integer param count,loop,inner
%string(1) %array(1:10) param array
%string(2) temp,temp2

  out=""
  in=in." "
  param count=1
  %for loop = 1,1,length(in) %cycle
    temp = substring(in,loop,loop+1)
    %if temp(1) = key flag %then %start
      temp2=(temp(2))
      toupper(temp2)
      param array(param count) = temp2
      param count=param count + 1
      loop = loop + 1
    %finish %else out=out.temp(1)
  %repeat

  %if param count > max params+1 %then %start
     printline("Too many parameters.")
     %stop
  %finish

  %for loop = 1,1,param count -1 %cycle
    legal = 0
    %for inner = 1,1,max params %cycle
      %if param array(loop)= parameters(inner) %then %c
         params(inner) = 1 %and legal=1
    %repeat
    %if legal=0 %then %c
                printline("Illegal parameter ".param array(loop)) %and %stop
  %repeat

%end

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

%routine split in and out(%string(255) %name line,in,out)

  %return %if line=""
  in=line %and out="" %unless line->in.(group sep).out 

  remove spaces(in)
  remove spaces(out)

%end

!-------------------------------------------------------------------------------
 
%routine split between commas(%string(255) %name in,one)

  %return %if in=""
  one=in %and in="" %unless in->one.(",").in

%end

!-------------------------------------------------------------------------------
!    M  A  I  N    P  R  O  G  R  A  M   
!-------------------------------------------------------------------------------

key flag = "-"
key flag = tostring(pam_key flag) %if pam_key flag >=' '

group sep ="/"
group sep = tostring(pam_group sep) %if pam_group sep >=' '

%for loop = 1,1,max params %cycle
  set params(loop) = 0
%repeat

in=CLIPARAM
extract params(in,out,set params)
in=out

group count=1
%cycle
   split between commas(in,group(group count))
   group count=group count+1
%repeat %until in=""

%for loop = 1,1,group count-1 %cycle
   split in and out(group(loop),infiles(loop),outfiles(loop))
%repeat

newline
printstring("Parameters set are:")
%for loop = 1,1,max params %cycle
  %if set params(loop) = 1 %then printstring(parameters(loop))
%repeat
newline

printline("File transfers required are:")
%for loop = 1,1,group count-1 %cycle
  printline(infiles(loop)." to ".outfiles(loop))
%repeat

%endofprogram
