#! /bin/sh
# searchr	Modified from search by Jerry Rains 03/08/01.
#		search is specialize to a filepro filesystem.  searchr is
#		generalized to search only the current working directory.
#		Otherwise searchr has all the features of search.
#               searchr will search for string $1 in `pwd`.
#               it will then replace the string $1 with $2 if $2 exists.

# Determine form of echo to use.
if [ `echo "x\c"` = "x" ]
then
    # Then the standard command will work just fine.
    ECHO="echo"
else
    # Otherwise we need to enable the 'backslash' processing.
    ECHO="echo -e"
fi
export ECHO
[ $tmpdir ] || tmpdir=/tmp

help() {
    clear
    $ECHO "  Usage: searchr 'search string' [ 'replace string' ]"
    $ECHO
    $ECHO "  'searchr' is a program that will find a 'string' in a file within the"
    $ECHO "  current working directory.  For a specialized search of an entire filePro"
    $ECHO "  filesystem use 'search'.\n"
    $ECHO "  Run 'searchr' from the directory you want to search."
    $ECHO "  Include special characters by enclosing in single quotes.  search '\"7\",\"2\"'"
    $ECHO "  The single quote can be searched for by escapeing it.      search \'"
    $ECHO "  In all modes, any occurance of the 'string' is logged in /tmp/found."
    $ECHO "  Upon completion of the search the /tmp/found file is displayed."
    $ECHO
    $ECHO "  'search' has 5 modes of operation:"
    $ECHO "  Find a 'string' and display. (default)"
    $ECHO "  Find a 'string', display and pause."
    $ECHO "  Find a 'string' and enter vi to edit the file the string is in."
    $ECHO "  Find a 'string', display, pause, then exit."
    $ECHO "  Find a 'string' and replace with another 'string'."
    $ECHO
    $ECHO "  The following file types are excluded from the search: key, index, tok, sel,"
    $ECHO "  map, screen, out, cpio and gz.  Press Enter to continue. \c"
    read key
    $ECHO
    exit
    }

# main process

trap "vi /tmp/found
      exit" 2

dir=`pwd`

[ -z "$1" -o "$1" = "-h" ] && help

FIND=$1

# CLEARS FOUND LIST
> /tmp/found

if [ -n "$2" -a "$2" != "n" -a "$2" != "e" -a "$2" != "p" -a "$2" != "x" ]
then {
    pause=$2
    repl="n"
    $ECHO "g/$1/s//$2/g" > $tmpdir/ex$$
    $ECHO "w" >> $tmpdir/ex$$
    $ECHO "q" >> $tmpdir/ex$$
    chmod 777 $tmpdir/ex$$
    }
elif [ "$2" != "n" -a "$2" != "e" -a "$2" != "p" -a "$2" != "x" ]
then
    $ECHO 
    $ECHO "P)ause when found"
    $ECHO "E)dit file when found"
    $ECHO "N)o pause [default]"
    $ECHO "eX)it when found"
    $ECHO
    $ECHO "Select function: \c";read pause
    $ECHO
    $ECHO 
else
    pause=$2
fi

$ECHO "Searching for $FIND"
# LOOKS FOR FILES            
for j in `echo * 2>/dev/null`
do
    case $j in
        *key*)   : nothing;;
        index*)  : nothing;;
        tok.*)   : nothing;;
        sel.*)   : nothing;;
        map)     : nothing;;
        map.tmp) : nothing;;
        screen*) : nothing;;
        out.*)   : nothing;;
        *.cpio*) : nothing;;
        *.gz)    : nothing;;
        *)        {
        # FINDS STRING
        if [ -r $j ]
        then
            # 06/24/01 - Added -n to grep.
            grep -n "$FIND" $j 2>/dev/null
            [ $? = 0 ] && {
                # CREATES FOUND LIST
                $ECHO $j >> /tmp/found
                # 06/24/01 - Added -n to grep.
                grep -n "$FIND" $j 2>/dev/null >> /tmp/found
                case $pause in
                    e|E)  vi $dir/$j;;
                    p|P)  {
                           $ECHO "\n$dir/$j - Press Enter to continue. \c"
                           read key
                           clear
                           };;
                    x|X)  {
                          vi /tmp/found
                          exit 1
                          };;
                    *)    : nothing;;
                esac

                # PERFORMS REPLACEMENT
                #ed - $j < $wrut/ED.s
                if [ -n "$2" -a "$2" != "n" -a "$2" != "e" -a "$2" != "p" -a "$2" != "x" ]
                then
                    if [ $repl != "a" ] 
                    then
                        $ECHO $dir/$j
                        $ECHO "Want to replace $1 with $2 (y/n/a/q)? \c"
                        read repl 
                    fi
                    case $repl in
                        a)        ex - $dir/$j < $tmpdir/ex$$ 2>/dev/null;;
                        y)        ex - $dir/$j < $tmpdir/ex$$ 2>/dev/null;;
                        q)        exit;;
                        *)        : nothing;;
                    esac
                fi
                }
        else
            $ECHO $dir/$j" - Is not ASCII file" >> /tmp/found
        fi
        };;
    esac 
done
[ -s $tmpdir/ex$$ ] && rm -f $tmpdir/ex$$
[ "$2" ] || vi /tmp/found
