#!/bin/csh -f

# Use $1 as a specification of a replacement of matches to
# a regexp with something else.  The regexp and the replacement
# should be separated and surrounded by some character not appearing
# in either so that it will be suitable as the main part of an s
# command for ed.  The replacement is performed globally in all the
# files appearing as the other arguments of the command.

if ($#argv < 2) then
  echo Usage: subst replacement-spec file ...
endif

set noglob

#echo -n ",g/" > script.tmp

set expsep = `echo "$1" | cut -c1`
#echo "$1" | cut -d$expsep -f2 | tr -d '\012' >> script.tmp
echo '1,$'"s$1gp" >> script.tmp

echo -n "about to do: "
cat -v script.tmp
echo on files: $argv[2-]
echo "Go ahead? (y or n)"
getanswer:
set answer = $<
switch ($answer)
case n:
  breaksw
case y:
  echo w >>script.tmp
  echo q >>script.tmp
  foreach file ($argv[2-])
    if (! -f $file) then
      echo subst: $file not a plain file
    else
      echo editing "$file"
      ed - $file <script.tmp
    endif
  end
  breaksw
default:
  echo "Please answer y or n for yes or no."
  goto getanswer
endsw

rm script.tmp
