#!/bin/sh
# search sandbox tree looking for file containing a pattern.
# Copyright (C) 2012 - 2018 FARGOS Development, LLC
# $Id$
originDir=`dirname $0`
emitFullPath=0
doRelative=0
fileSuffix='*.[hc]'
fileSuffix2='*.[hc]pp'
searchPath=""
outputFile=/dev/null
thisRoot=`${originDir}/sbrel -showroot`

while test $# -gt 0
do
	case "${1}" in
	-h | -help | --help)
		printf "usage: %s [--top | --rel] [--full | --partial] { [--all] | [--allbt] | [--sb] [--bt1] [--bt2] [--bt3] [--bt4] } [-d dir] ... [-o file] [[--suffix fileNamePattern] --suffix2 fileNamePattern] pattern ...\n" "${0}" >&2
		printf "  --top = start search from top of tree\n" >&2
		printf "  --rel = start search from relative position within tree\n" >&2
		printf "  --full = emit full path names\n" >&2
		printf "  --partial = emit partial path below tree root\n" >&2
		printf "  --all = search sandbox and all backing trees\n" >&2
		printf "  --allbt = search all backing trees\n" >&2
		printf "  --sb = search sandbox\n" >&2
		printf "  --btN = search backing tree N in chain\n" >&2
		printf "  -d dir = add an arbitrary tree to be searched\n" >&2
		printf "  -o file = also output result into file\n" >&2
		printf "  --suffix = file name search pattern\n" >&2
		printf "  --suffix2 = additional file name search pattern\n" >&2

		printf "  Default file name pattern is %s and %s\n" "${fileSuffix}" "${fileSuffix2}" >&2
		printf "  Default is to start search from top of tree\n" >&2
		printf "  Default is to emit partial path below tree\n" >&2
		printf "  Default is to just search local sandbox\n" >&2
		exit 1
		;;
	-full | --full)
		emitFullPath=1
		;;
	-partial | --partial)
		emitFullPath=0
		;;
	-top | --top)
		doRelative=0
		;;
	-rel | --rel)
		doRelative=1
		;;
	-a | -all | --all) 
		btElems=`echo ${BT_PATH} | sed -e 's/:/ /g'`
		searchPath="${thisRoot} ${btElems}"
		;;
	-allbt | --allbt) 
		btElems=`echo ${BT_PATH} | sed -e 's/:/ /g'`
		;;
	-sb)
		searchPath="${searchPath}${searchPath:+ }${thisRoot}"
		;;
	-bt1 | -bt)
		searchPath="${searchPath}${searchPath:+ }${BT1}"
		;;
	-bt2)
		searchPath="${searchPath}${searchPath:+ }${BT2}"
		;;
	-bt3)
		searchPath="${searchPath}${searchPath:+ }${BT3}"
		;;
	-bt4)
		searchPath="${searchPath}${searchPath:+ }${BT4}"
		;;
	-d)	# add an additional search directory
		searchPath="${searchPath}${searchPath:+ }${2}"
		shift
		;;
	-o)
		outputFile="${2}"
		shift
		;;
	-suffix | --suffix)
		fileSuffix="${2}"
		fileSuffix2=""
		shift
		;;
	-suffix2 | --suffix2)
		fileSuffix="${2}"
		shift
		;;
	-*)
		printf "%s: unrecognized option \"%s\"\n" "${0}" "${1}" >&2
		exit 1
		;;
	*)
		pattern="${pattern}${pattern:+ }${1}"
		;;
	esac
	shift
done

if test -z "${pattern}"
then
	printf "%s: No pattern specified, nothing to do.\n" "${0}" >&2
	exit 1
fi

if test -z "${searchPath}"
then	# default to searching just local sandbox
	searchPath="${thisRoot}"
fi

if test ${doRelative} -eq 1
then
	pathSuffix=`${originDir}/sbrel`
else
	pathSuffix=""
fi
pathElemCount=`echo ${searchPath} | wc -w`
for d in ${searchPath}
do
	if test ${emitFullPath} -eq 0
	then
		rootPath="."
		cd "${d}${pathSuffix}"
	else
		rootPath="${d}${pathSuffix}"
	fi
	if test ${pathElemCount} -gt 1
	then
		printf "Scanning under %s...\n" ${d}${pathSuffix}
	fi
# NOTE: patterns have to be enclosed in quotes to avoid expansion
# and matching any local file names.  The fileSuffix2 variable
# is tested and prefixed with "-o -name" and output surrounded by quotes.
# This allows it to disappear completely if not set, but not
# be expanded incorrectly if the pattern would have matched files in
# the working directory
	find -L "${rootPath}" '(' -name "${fileSuffix}" ${fileSuffix2:+-o -name "${fileSuffix2}"} ')' -exec egrep -H -n "${pattern}" '{}' ';' | sed -e '/\.svn\//d' -e '/Binary file/d' | tee -a ${outputFile}
done
