"*************************************************************************"
"replacetext(textfile,inText,outText) - Do a search-and-replace on        "
"                    the named ascii text file. The first argument is     "
"                    the textfile name.  Argument 2 is text to be         "
"                    replaced and Argument 3 is the replacement text.     "
"                                                                         "
"example: updatetext('/home/solids/vnmrsys/maclib/tancpx','pwX90','pwY90')" 
"*************************************************************************"

if ($# > 2) then   
   exists($1,'file'):$e
   if ($e < 0.5) then
      write('error','Textfile %s does not exist',$1)
      abort
   endif
else
   write('error','Format: replacetext(textfile,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

$path = $1

exists($path,'file'):$e
if ($e > 0.5) then 
   $e = 0    
   exists($path,'ascii'):$e
endif
if ($e > 0.5) then      
   write('alpha','  Entry 1: Update %s',$path)
   $e1=0
   $tempfile = userdir + '/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 = $path
   write('alpha','  Entry %s is not an ascii file',$notpar)
endif
