#!/bin/bash
# Find a file within the hierarchy of backing trees.
# Copyright (C) 2012 - 2019 FARGOS Development, LLC
# $Id$
# Has dependency upon findFile, either native or osindependent
# variant in findFile.sh
originDir=`dirname "${0}"`
nativeArch=`${originDir}/buildArch -native`

PATH="${PATH}:${originDir}"
export PATH

# First, try to find (preferrably native) version of findFile utility
defaultFindFile=""
defaultPathCondense=""
for suffix in "" ".sh"
do
	if test -n "${defaultFindFile}" -a -n "${defaultPathCondense}"
	then # both found
		break
	fi
	for f in ${originDir}/../../${nativeArch}/bin ${originDir}/../${nativeArch}/bin ${originDir}/../bin_${nativeArch} ${originDir}/../../bin_${nativeArch} ${originDir}
	do
		if test -z "${defaultFindFile}" -a -x "${f}/findFile${suffix}"
		then
			defaultFindFile="${f}/findFile${suffix}"
			if test -n "${defaultPathCondense}"
			then
				break
			fi
		fi
		if test -z "${defaultPathCondense}" -a -x "${f}/pathCondense${suffix}"
		then
			defaultPathCondense="${f}/pathCondense${suffix}"
			if test -n "${defaultFindFile}"
			then
				break
			fi
		fi
	done
done

findFileProg="${defaultFindFile:-findFile}" # last resort path
pathCondenseProg="${defaultPathCondense:-pathCondense}" # last resort path

# Alternative with "src/" removed for use with backing trees that didn't have
# an explicit "src" subdirectory
pathSuffixNoSrc=`${originDir}/sbrel -suffix NONE 2>/dev/null`
# Path with /src added 
pathSuffix="/src${pathSuffixNoSrc}"
btElems=""
searchPath=""
dontCheckLocal=1
quietMode=0

while test $# -gt 0
do
	searchFor="${1}"
	case "${searchFor}" in
	-h | -help | --help)
		printf 'usage: %s [-q] [-s separator] [-d additionalDir] {--all | --allbt | --sb | --bt1 | --bt2 | ... } ... fileName [...]\n' "${0}" >&2
		exit 1
		;;
	/*) # absolute path name
		echo ${searchFor}
		;;
	\./*) # relative to current directory
		echo ${searchFor}
		;;
#	\.\./*) # relative to parent directory
#		printf "%s\n" ${searchFor}
#		;;
	-allbt | --allbt)
		btElems=`echo ${BT_PATH} | sed -e 's/:/ /g'`
		;;
	-a | -all | --all) 
		btElems=`echo ${BT_PATH} | sed -e 's/:/ /g'`
		dontCheckLocal=0
		;;
	-sb | --sb)
		dontCheckLocal=0
		;;
	-bt1 | -bt | --bt | --bt1)
		btElems="${btElems}${btElems:+ }${BT1}"
		;;
	-bt2 | --bt2)
		btElems="${btElems}${btElems:+ }${BT2}"
		;;
	-bt3 | --bt3)
		btElems="${btElems}${btElems:+ }${BT3}"
		;;
	-bt4 | --bt4)
		btElems="${btElems}${btElems:+ }${BT4}"
		;;
	-d)	# add an additional search directory
		btElems="${btElems}${btElems:+ }${2}"
		shift
		;;
	-s)	# allow path separator to be specified
		pathSep="${2}"
		shift
		;;
	-q)
		quietMode=1
		;;
	-*)
		printf "%s: unrecognized option: \"%s\"\n" "${0}" "${searchFor}" >&2
		exit 1
		;;
	*)
		if test -z "${btElems}"
		then
			dontCheckLocal=0
		fi
		prefixChar="${searchFor:0:1}"
		if test "${prefixChar}" = "@"
		then # ignore any local occurrence
			searchPath=""
			noLocal=1
			searchFor="${searchFor:1}" # drop first character
		else # normal case, search local sandbox
			noLocal=${dontCheckLocal}
			if test ${noLocal} -eq 0
			then
				searchPath=". ${ST}${pathSuffix} ${STS}"
			fi
		fi
		btDirs=`echo ${btElems} | sed -e 's/'${pathSep:-:}'/ /g'`
		for d in ${btDirs}
		do
	       		searchPath="${searchPath} ${d}${pathSuffix} ${d}/src"
		done
		# try without src
		if test ${noLocal} -eq 0
		then # normal case, search local sandbox
			searchPath="${searchPath} ${ST}${pathSuffixNoSrc} ${ST}"
		fi
		for d in ${btDirs}
		do
	       		searchPath="${searchPath} ${d}${pathSuffixNoSrc} ${d}"
		done

		uniquePath=`${pathCondenseProg} ${searchPath}`
		fileName=`${findFileProg} -1 "${searchFor}" "${uniquePath}"`
		if test -z "${fileName}"
		then
			if test ${quietMode} -eq 0
			then
				printf "%s: cannot locate %s under %s\n" "${0}" "${searchFor}" "${uniquePath}" >&2
			fi
		else
			echo ${fileName}
		fi
	esac
	shift
done
exit 0
