#!/bin/ksh
usage()
{
 echo "Usage:
 grep.recursive [-n] [-i] [-h] directory pattern1 [pattern2 [pattern3]]

 Searches files in directory structure and writes lines containing
 ALL pattern and then writes file names where ALL pattern matched

    -n  =  no file names
    -i  =  interactive (doesn't work as expected)
    -h  =  help (print this text)"
 exit 1
}

SILENT=false
INTERA=false

while true
do
 case $1 in
 -n) SILENT=true ;;
 -i) INTERA=true ;;
 -h) usage ;;
  *) break ;;
 esac
 shift
done

if [ $# -lt 2 ]; then usage; fi
START=$1
shift

if [ -d ${START} ] ; then START=${START}'/*' ; fi
find ${START} -print | while read DSN dummy
do
 if [ -r ${DSN} -a ! -d ${DSN} ]
 then
   # file ${DSN}
   case $# in
    0) usage ;;  # it's redundant
    1) strings ${DSN} | grep -i $1 ;;
    2) strings ${DSN} | grep -i $1 | grep -i $2 ;;
    3) strings ${DSN} | grep -i $1 | grep -i $2 | grep -i $3;;
    *) usage ;;
    esac
   if [ $? = 0 ]
     then
       if [ "${SILENT}" != "true" ]; then echo "======> ${DSN} =============="; fi
       if [ "${INTERA}"  = "true" ]; then echo "Continue?"; read DUMMY        ; fi
     fi
 fi
done

