#! /bin/sh
# search	Modified from fp_find by Jerry Rains 6/21/99.
#               search is specialized to search through a filepro filesystem.
#		For a general search or search and replace in the current 
#		working directory use searchr.
#		search will search for string $1 in the directories listed in
#		search.$PFQUAL.  If PFQUAL="" then search.list is used.
#               If $2 exists it will then replace the string $1 with $2 (After
#		asking permission).
# Modified to work without fPmenu's env vars 5/11/00
# Modified to work without any env vars if in a filepro directory 10/3/00
# Modified to check for encrypted prc.'s using Bob Stockler's ck.prc 11/28/00
# You can now set EncodeCK="N" to disable ENCODE checking.  05/27/01

# We need to see which echo works.
nix=`uname -s 2>/dev/null`
# If we are running under any version of Linux ...
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

# 06/24/01 - Make sure tmpdir is set.
[ -z "$tmpdir" ] && tmpdir=/tmp

help() {
    clear
    $ECHO "  Usage: search 'search string' [ 'replace string' ]"
    $ECHO
    $ECHO "  'search' is a program that will find a 'string' in a filePro filesystem."
    $ECHO "  For a general search (and replace) from the command line use searchr."
    $ECHO "  Set PFDIR in the environment or run 'search' from a ./filepro directory."
    $ECHO "  Running from a ./filepro directory has precidence over setting PFDIR."
    $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 $tmpdir/found."
    $ECHO "  Upon completion of the search the $tmpdir/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.  The first time 'search' is run it will"
    $ECHO "  generate a search.PFQUAL file.  If qualifiers are not used the file will be"
    $ECHO "  called 'search.list'.  This file can be customized.  If you don't use"
    $ECHO "  qualifiers it will be overwritten if you change PFDIR.  The 'search.list'"
    $ECHO "  file is kept in the same directory that contains 'search'.  Press Enter. \c"
    read key
    $ECHO
    exit
    }

setup_search() {
    $ECHO "Setting up $fb/search.$PFQUAL file for $PFDATA$PFDIR."
    $ECHO
    $ECHO $PFDATA$PFDIR/filepro > $fb/search.$PFQUAL
    find $PFDATA$PFDIR/filepro/ -name menu$MLF -print > $tmpdir/search
    # Strip off the /[^/]*/menu$MLF
    sed -n '\,/[^/]*/menu.*$,s,,,p' $tmpdir/search | sort -u >> $fb/search.$PFQUAL
    }

# main process

trap "vi $tmpdir/found
      exit" 2

dir=`pwd`

[ $instdir ] || instdir=/usr/bin

# 05/27/01 - Set EncodeCk="N" to turn of ENCODE check.
[ "$EncodeCk" != "N" ] && {
    # Check to see if ck_prc has been run.  If not run it.
    [ -s "$PFDATA$PFDIR/ASCII_ENCODED" ] || {
        $ECHO "Checking for ENCODED prc. tables."
        $instdir/ck_prc -q
        }
    # Now report any ENCODED prc.'s.
    [ -s "$PFDATA$PFDIR/ENCODED" ] && {
        $ECHO
        $ECHO
        $ECHO "The following prc.'s are ENCODED. "
        $ECHO
        cat $PFDATA$PFDIR/ENCODED
        $ECHO
        $ECHO "These ENCODED prc.'s can't be searched."
        $ECHO
        $ECHO "Would you like to decode them? [N] \c"
        read yesno
        [ $yesno ] || yesno="n"
        yesno=`$ECHO $yesno | sed -e "y/YN/yn/"`
        ABE=ASCII;export ABE
        trap "" 2
        [ "$yesno" = "y" ] && {
            $ECHO "
          If you see a message similar to:
    
          *** A filePro Error Has Occurred ***
          ...
          Standard Input Redirected
    
    break out by sending the program a QUIT signal (<ctrl>\).\n"
            $ECHO "Press Enter to continue. \c"
            read key
    trap '' 3
            while read PFNAME PRC
            do
                $instdir/ASCIIfy $PFNAME $PRC
            done < $PFDATA$PFDIR/ENCODED
    trap 3
            rm $PFDATA$PFDIR/ENCODED
            rm $PFDATA$PFDIR/ASCII_ENCODED
            }
        trap "vi $tmpdir/found
          exit" 2
        }
    }

[ $fb ] || {
    hash -r 
    cd /
    is_=`type search 2>&1 | sed -e "s,search is ,,"`
    tis_=`$ECHO $is_ | grep '/'`
    if [ -n "$tis_" ]
    then 
        fb=`dirname $is_`
    elif [ -x search ]
    then
        fb=`pwd`
    elif [ -x $instdir/search ]
    then
        fb=$instdir
    else
        $ECHO "\nIf 'search' is not in the path then run 'search' from the directory "
        $ECHO "where it is located."
        $ECHO "\nPress Enter to continue. \c"
        $ECHO
        read key
        exit
    fi
    }
cd $dir

# If in a filepro directory reset if not same as environment
[ $PFDATA$PFDIR ] && {
    if [ $PFDATA$PFDIR != `dirname $dir` -a `basename $dir` = filepro ]
    then
        PFDIR=
        LF=
        PFQUAL=
    else
        cd $PFDATA$PFDIR/filepro
        dir=$PFDATA$PFDIR/filepro
    fi
    }
[ $PFDATA$PFDIR ] || {
    if [ `basename $dir` = "filepro" ]
    then
        PFDIR=`dirname $dir`
        export PFDIR
    else
        clear
        $ECHO
        $ECHO "If PFDIR is not set in the environment then 'search' must be in the PATH"
        $ECHO "and the current working directory must be ./filepro."
        $ECHO "\nPress Enter to continue.\c"
        read key
        help
        exit
    fi
    }

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

FIND=$1
[ $PFQUAL ] || {
    PFQUAL=list
    export PFQUAL
    } 

# CLEARS FOUND LIST
> $tmpdir/found
$ECHO
$ECHO "'search' is running in $PFDATA$PFDIR"

if [ -s "$2" -a "$2" != "n" -a "$2" != "e" -a "$2" != "p" -a "$2" != "x" ]
then {
    pause=$2
    repl="n"
    $ECHO "g/$1/s//$2/g" > $fb/ex$$
    $ECHO "w" >> $fb/ex$$
    $ECHO "q" >> $fb/ex$$
    chmod 777 $fb/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

olddir=`pwd`
cd /
MLF=$LF
[ $LF ] && MLF="."$LF

# Make a list of all MenuMaster menu directories, looking for duplicates
[ -s $fb/search.$PFQUAL ] || {
    setup_search
    }
[ "$olddir" = `cat $fb/search.$PFQUAL` ] || {
    setup_search
    }
cd $olddir

$ECHO "Searching for $FIND"
for h in `uniq $fb/search.$PFQUAL`
    do
    $ECHO $h
    $ECHO $h >> $tmpdir/found
    cd $h

    # LISTS DIRECTORY
    ls -d * 2>/dev/null > $tmpdir/i
    for i in `cat $tmpdir/i`
    do
        #VERIFIES THERE IS A MAP
        ls $h/$i/* 2>/dev/null > $tmpdir/tst
        [ -s $tmpdir/tst ] || continue
        cd $h/$i

        # LOOKS FOR FILES            
        for j in `echo * 2>/dev/null`
        do
            case $j in
                *_*)     : nothing;;
                *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
                grep -n "$FIND" $j 2>/dev/null
                [ $? = 0 ] && {
                    # CREATES FOUND LIST
                    $ECHO `basename $h`"/"$i"/"$j >> $tmpdir/found
                    grep -n "$FIND" $j 2>/dev/null >> $tmpdir/found
                    case $pause in
                        e|E)  vi $h/$i/$j;;
                        p|P)  {
                               $ECHO "\n$i/$j - Press Enter to continue. \c"
                               read key
                               clear
                               };;
                        x|X)  {
                              vi $tmpdir/found
                              exit 1
                              };;
                        *)    : nothing;;
                    esac

                    # PREFORMS REPLACEMENT
                    #ed - $j < $wrut/ED.s
                    if [ -s "$2" -a "$2" != "n" -a "$2" != "e" -a "$2" != "p" -a "$2" != "x" ]
                    then
                        if [ $repl != "a" ] 
                        then
                            $ECHO "Want to replace $1 with $2 (y/n/a/q)? \c"
                            read repl 
                        fi
                        case $repl in
                            a)        ex - $j < $fb/ex$$ 2>/dev/null;;
                            y)        ex - $j < $fb/ex$$ 2>/dev/null;;
                            q)        exit;;
                            *)        : nothing;;
                        esac
                    fi
                    # RECOMPILES WITH RCABE
                    #if [ -f prc.* ] -o [ -f *.prc ]
                    #then
                         #if [ $rcab != "a" ]
                         #then
                         #$ECHO "Want to recompile with rcabe (y/n)? \c"
                         #read rcab
                    #fi
                    #case rcab in
                    #y)        {
                        #cd $i
                        #l=`$ECHO $j | sed 's,^.*prc\.,,'`
                        #$ECHO Compiling process $l for directory $i
                        #rcabe $i -c $l -t 100000 >> $tmpdir/errors
                        #}
                    #*)        : nothing
                    #esac
                    }
                else
                    $ECHO `basename $h`"/"$i"/"$j" - Is not ASCII file" >> $tmpdir/found
                fi
                };;
            esac 
        done
    done
done
[ -s $fb/ex$$ ] && rm -f $fb/ex$$
vi $tmpdir/found
