"*************************************************************************"
"updatetext(directory,inText,outText) - Do a search-and-replace on        "
"                    all ascii text files in the named directory. The     "
"                    first argument is the directory.  Argument 2 is      "                  
"                    text to be replaced and Argument 3 is the            "
"                    the replacement text.                                "
"                  example: updatetext('~/vnmrsys/maclib','pwX90','pwY90')" 
"*************************************************************************"

if ($# > 2) then 
   exists($1,'directory'):$e
   if ($e < 0.5) then
      write('error','Directory %s does not exist',$1)
      abort
   endif
else
   write('error','Format: updatetext(directory,inText,outText)')
   abort
endif

$inText =''
if (typeof('$2') > 0.5) then 
   $inText = $2
else
  write('error','Format: Argument 2 must be a string type')
  abort
endif

$outText =''
if (typeof('$3') > 0.5) then 
   $outText = $3  
else
   write('error','Format: Argument 3 must be a string type')
   abort
endif

getfile($1):$entrys
write('alpha','directory %s has %d entries',$1,$entrys)
$inode = 0
$filename = ''
$ext = ''
$cmd = ''
$dum='' 

while ($inode < $entrys) do
   $inode = $inode + 1
   getfile($1,$inode):$filename,$ext
   $path = $1 + '/' + $filename
   if ($ext <> '') then 
      $path = $path + '.' + $ext
   endif
   exists($path,'file'):$e
   if ($e > 0.5) then 
      $e = 0      
      exists($path,'ascii'):$e
   endif
   if ($e > 0.5) then      
      write('alpha','  Entry %d: Update %s.%s',$inode,$filename,$ext)
      $e1=0
      $tempfile = $1 + '/updateout'
//    write('alpha','$tempfile = %s\n',$tempfile)
      exists($tempfile,'file'):$e1
      if ($e1 > 0.5) then 
         rm($tempfile):$dum
      endif
      $cmd='sed -i -e \'s/'+$inText+'/'+$outText+'/w ' +$tempfile+ '\' '+$path
//    write('alpha','$cmd = %s\n',$cmd)
      shell($cmd):$dum
      $e1=0
//    write('alpha','$tempfile = %s\n',$tempfile)
      exists($tempfile,'file'):$e1
      if ($e1 > 0.5) then
         cat($tempfile)
         rm($tempfile):$dum
      else
         write('error', 'Abort: Bad search string')
         abort
      endif
   else
      $notpar = $filename
      if ($ext <> '') then
         $notpar = $filename + '.' + $ext
      endif
      write('alpha','  Entry %d:  %s is not an ascii file',$inode,$notpar)
   endif
endwhile
