#!/bin/sh
#==============================================================
# rgrep 
# This shell script recursively searches files for a pattern.
#
# ARGUMENTS:
#
# OPTION:      
#              options passed to grep, and find
#
# Facility:  SLAC/PEPII/NLCTA
#
# Auth:  13-Mar-1998, Jingchen Zhou   (JINGCHEN)
#--------------------------------------------------------------
# Mod:        
#       17-June-2003, Jingchen Zhou (Jingchen)
#       - Renamed to rgrep. The original name is called "search".
#       - Improved so that options for grep can be used.
#       - Debug added
#       23-June-2003, Jingchen Zhou (Jingchen)
#       - Expressions to find (such as -follow) are supported
#       18-June-2007, Jingchen Zhou (jingchen)
#       - Made it to work for Linux
#==============================================================

help()
{
    echo "rgrep - recursively searches files for a pattern".
    echo ""
    echo "Usage:  `basename $0` [options] directory files pattern" 
    echo ""
    echo "Options "
    echo "  (based on grep):"
    echo "  -b  Precede each line by the block number"
    echo "  -c  Print only a count of the lines that contain the pattern"
    echo "  -h  Prevents the filename from being appended to the line"
    echo "  -i  Ignore upper/lower case distinction during comparisons"
    echo "  -l  Print only the names of files with matching lines"
    echo "  -n  Precede each line by its line number in the file"
    echo "  -s  Suppress error messages about nonexistent or unreadable files"
    echo "  -v  Print all lines except those that contain the pattern"
    echo "  -w  Search for the expression as a word"
    echo ""
    echo "  (based on find): "
    echo "  -f  Follow symbolic links in search"
    echo ""
    echo "Operands:"
    echo "   directory  a path name of a starting point in the directory"
    echo "              hierarchy"
    echo "   files      files to be searched for the pattern"
    echo "   pattern    a regular expressions to match the pattern"
    echo ""
    echo "Example:"
    echo "   `basename $0` /afs/slac/g/cd/soft/dev \"*\" \"oracle*\" "
    echo "   `basename $0` -w /afs/slac/g/cd/soft/dev \"*\" \"oracleSetup\" "
    echo "   `basename $0` -n -s /afs/slac/g/cd/soft/dev \"*.csh\" \"oracleSetup\" "
    echo ""
    echo "Notes:"
    echo "   files and pattern must be quoted "  
}

grepcmd=/bin/grep

if [ $# -eq 0 ]
then 
    help
    exit 1
fi

while [ `echo $1 | cut -c1` = "-" ]
do
    case $1 in 
    -b|-c|-h|-i|-l|-n|-s|-v|-w)
	grepopts="$grepopts $1"
	;;
    -f|-follow)
	findopts="-follow"	
	;;
    *)
	echo " $1 is not a valid option " 
	help
	exit 1
	;;
    esac
    shift	    
done

dirz=$1
filez=$2   
patternz=$3

if [ $# -ne 3 ]
then 
    echo "Invalid syntax"
    help
    exit 1
fi
    
if [ ! -d $dirz ]
then	
    echo "$dirz:  invalid directory"
    help
    exit 1
fi
 
echo " "
echo " SEARCHING $dirz FOR FILES $filez THAT CONTAIN $patternz"
echo " "
 
find $dirz $findopts -name "$filez" -exec $grepcmd $grepopts $patternz {} /dev/null \;

exit 0
























