
      program tst
      character*40 metaname,cdum
      write(6,"('Enter metaname: ',$)")
      read *,metaname
      call gopks(6,idum)
      call metafile(metaname,cdum,1,0,0,ier)
      stop
      end

c
c------------------------------------------------------------------
c Begin file /home/sting/foster/timesvid/metafile.f
c------------------------------------------------------------------
c
      subroutine metafile(metaname,rdir,iopn,icls,ircp,ier)
c
c On Input:
c   metaname = character name of meta file
c   rdir = character remote directory to which meta file is to be
c     remote copied (rcp) (include machine path) (no slash at end)
c      e.g., "sting:/d/foster/timesvid","ouray.hao.ucar.edu:/d/roble/times" 
c     (rdir ignored if ircp <= 0)
c     (length of metaname+rdir+1 should be <= 132, if ircp > 0)
c   iopn,icls,ircp are integer flags as described below
c
c On Output:
c   if iopn > 0 on input, then metaname has been opened and activated
c   if icls > 0 on input, then metaname has been deactivated and closed
c   if ircp > 0 on input, then metaname has been rcp'd to rdir
c   if ier != 0, some error has occurred, and error msg written to stdout
c   
      character*(*) metaname,rdir
      character*80 cdum,name80
      character*132 path
      character*140 rcpcom
c
      lenname = lenstr(metaname)
      if (lenname.gt.80) then
        write(6,"('>>> metafile: metaname too long (must be <= 80)')")
        ier = 1
      endif
      name80(1:lenname) = metaname(1:lenname)
c
c Open and activate metaname:
c
      if (iopn.gt.0) then
        write(6,"('metafile: lenname=',i3,' name80=',a)")
     +    lenname,name80(1:lenname)
        call gesc(-1391,1,name80,1,idum,cdum)
        call gopwk(1,2,1)
        call gacwk(1)
      endif
c
c Deactivate and close metaname:
c
      if (icls.gt.0) then
        call gdawk(1)
        call gclwk(1)
      endif
c
c rcp metaname to rdir/metaname:
c
      if (ircp.gt.0) then
        lenrdir = lenstr(rdir)
        if (lenname+lenrdir+1.gt.132) then
          write(6,"('>>> warning metafile: lengths of metaname+rdir+1',
     +      '=',i3,' (should be <= 132)',/,' name80=',a,/,
     +      ' rdir=',a)") lenname+lenrdir+1,name80(1:lenname),
     +      rdir(1:lenrdir)
          ier = 2
        endif
        write(path,"(a,'/',a)") rdir(1:lenrdir),name80(1:lenname)
        lenpath = lenstr(path)
        write(rcpcom,"('rcp ',a,' ',a)") name80(1:lenname),
     +    path(1:lenpath)
        lenrcp = lenstr(rcpcom)
        istat = ishell(rcpcom(1:lenrcp))
        if (istat.ne.0) then
          write(6,"('>>> metafile: bad istat=',i4,' from rcp command:',
     +      /a)") istat,rcpcom(1:lenrcp)
          ier = 3
          return
        else
          write(6,"('metafile: successful rcp of disk file ',a,
     +      ' to ',a)") name80(1:lenname),path(1:lenpath)
        endif
      endif
      return
      end
c
      function lenstr(str)
      character*(*) str
c
c Return index to last non-blank char in str
c
      length = len(str)
      do i=length,1,-1
        if (str(i:i).ne.' ') then
          lenstr = i
          return
        endif
      enddo
      lenstr = 1
      return
      end
