#!/bin/ksh

# Start up emacs on all matching files in a tree

TMP1=/tmp/${USER}_emacsall
rm -f $TMP1

if [ "$1" = "" -o "$3" != "" ]
then
  echo "Usage: emacsall <filename> <search string>"
  exit 1

elif [ "$2" = "" ]
then
  echo "Running EMACS on files '$1' ..."
  find . -type f -name "$1" -print | sed "s/^/emacs /" > $TMP1
  chmod +x $TMP1
  $TMP1

else
  echo "Running EMACS on files '$1' which contain '$2' ..."
  find . -type f -name "$1" -exec grep -i -l -e "$2" {} \; | \
    sed "s/^/emacs /" > $TMP1
  chmod +x $TMP1
  $TMP1

fi

rm -f $TMP1
