c all t359i.cat 1.3 i.cat
c all t359n.cati 1.4 n.cat
c all t359o.catin 1.5 o.cat
c all t359e.catino 1.6 e.cat
c all t359g.catinoe 1.7 g.cat
c all t359d.catinoeg 1.8 d.cat
c all t359p.catinoegd 1.9 p.cat
c all t359k.catinoegdp 1.a k.cat
c all t359b.catinoegdpk 1.b b.cat
c all t359m.catinoegdpkb 1.c m.cat
c all t359f.catinoegdpkbm 1.d f.cat
c all t359j.catinoegdpkbmf 1.e j.cat
c all t359c.catinoegdpkbmfj 1.f c.cat

	character*60 f0,f1,f2(16)
	character*1  ic,icc
	integer len(16)
        ik=0	
	k=iargc()
        if(k.lt.1)then
	  write(*,*)
	  write(*,*)'   Usage: getcat batch'
	  stop
	endif
	call system("rm ?.cat")
	call system("rm *.cat?*")
	call getarg(1,f0)
c produce order.dat	
	open(1,file=f0,status='old')
	open(2,file='order.dat',status='unknown')
	ip=0
8	read(1,'(a)',end=9)f1
	ip=ip+1
7	k=index(f1,'.')
	if(k.eq.0)then
          f1=f1(1:lnblnk(f1))//'.fit'
	  goto 7
	endif
	write(2,'(a)')f1(k-1:k-1)
	if(ip.eq.1)ic=f1(k-1:k-1)
	goto 8
9	close(1)
	close(2)
	
c read filenames
	f0='getcat.tmp '
	f1='ls '//char(ichar(f1(1:1))-32)//'*.cat  >'//f0
	call  system(f1)
c get mother color
	j=0
	open(1,file=f0,status='old')
99      read(1,'(a)',end=98)f1
	j=j+1
	if(f1(5:5).eq.ic)goto 98
	goto 99
98	close(1)
	write(*,*)j	
	
100	open(1,file=f0,status='old')
	i=0
11	i=i+1
	read(1,'(a)',end=10)f2(i)
	goto 11
10	m=i-1
	close(1)
	if(m.eq.2)goto 200
	     write(*,*)m
	k=index(f2(j),'.')
	ic=f2(j)(k-1:k-1)
	       if(ik.eq.0)then
	          icc=ic
	          ik=1
	        endif
	f1='all '//f2(j)(1:lnblnk(f2(j))+1)//f0(1:lnblnk(f0)+1)//ic//'.cat !'
	  write(*,*)f1
	call  system(f1)
	f0='getcat.tmp '
	f1='ls *'//f2(j)(k:lnblnk(f2(j)))//ic//' >'//f0
	call  system(f1)
	 j=1
	goto 100
200	call system('mv '//icc//'.cat 0.cat')
        call system('cat ?.cat >getcat.tmp')
	call system('mv 0.cat '//icc//'.cat') 
	call system('del3 getcat.tmp getcat.cat')
	call system("rm *.cat?*")
	end
  
