#!/bin/sh
#find string  in all File name in sub-directories
USAGE="USAGE: $0 - dirName fileType|all searchString \n fstring . php include"
dirName=$1
fileType=$2
findVal=$3
findVal="$findVal"
#Test cases
#fstring string
if [ $# -eq 3 ] 
then
	echo "Looking for String: ($findVal) with fileType ($fileType) in Directory: ($dirName)"
	set -x
	if [ $fileType = "all" ] 
	then
		find . -type f -print0 | xargs -0 grep -Hi $findVal 2>/dev/null
		#NOT WORKING find . -name $dirName  -type f -print0 | xargs -0 grep -Hi $findVal 2>/dev/null
		#find $dirName -type f -print | xargs grep -iH "$findVal" 2>/dev/null
	else
		#find $dirName -type f -name  "*${fileType}*" -print | xargs grep -iH "$findVal" 2>/dev/null
		find $dirName -type f -name "*${fileType}" -exec grep -i "$findVal"  -- {} +  2>/dev/null
	fi
	set +x
	echo "Looking for String: ($findVal) with fileType ($fileType) in Directory: ($dirName)"
else
	echo $USAGE
fi
# **** NOT USING BELOW CODE ****
exit
#find $dirName -type f -name  $fileType -print | xargs grep -iH "$findVal" 2>/dev/null
echo $*
echo "Looking for string: $findVal type files: $fileType in directory: $dirName"
#find -name your_file_matcher -exec grep $findVal -H {} \;
echo 'find $dirName -type f -name "$fileType" -exec grep -iH $findVal {} \;'
find $dirName -type f -name "$fileType" -exec grep -iH "barchart" {} \;
exit
find $dirName -type f -name "$fileType" -exec grep -iH $findVal {} \;
#grep -i -r $findVal *
exit
#find $1 -type f  -name "*$2*" -print | xargs grep -iH "$2" 2>/dev/null
exit
dir="$1"
if [ "X$dir" = "Xall"  -o  "X$dir" = "X"  -o  "X$fileName" = "X."  ]
then
	dir="."
else
	dir="$1"
fi
fileName="$2"
if [ "X$fileName" = "Xall"  -o  "X$fileName" = "X"  -o  "X$fileName" = "X."  ]
then
	fileName="*"
else
	fileName="$2"
fi
string="$3"
echo dir=$dir fileName = $fileName string=$string
find="Find string ($string) in files ($2) in directory ($dir) and below:"
echo "$find\n"
USAGE="USAGE: ff directory fileName string\nYou Entered: $find"
if [ "$#" -ne 3 ]
then
	echo "ERROR: $USAGE"
	exit
fi
string="X$3"

if [ $fileName == "X" ] 
then
	fileName=$2
	if [ $string == "X" ] 
	then
		string=$3
	else
		string="ERROR!"
	fi
else
	#remove X
	echo "in else"
	fileName=$2
	string=$3
fi
echo "CMD: find $dir  -type f  -name "*$fileName*" -print | xargs grep -iH "$string" 2>/dev/null"
find $dir  -type f  -name "*$fileName*" -print | xargs grep -iH "$string" 2>/dev/null
rc=$?
set +x
if [ $rc -eq 0 ]
then
	echo "Successful search"
else
	echo "Error: ($rc) \n$USAGE"
fi

#grep -r * 
#May work
