#!/bin/sh
# sbrel - report common relative path component in sandbox
# Copyright (C) 2012 - 2019 FARGOS Development, LLC
# $Id$
# Typical usage:  cd $STO`sbrel` or cd $STS`sbrel`
curDir=`pwd`
fromDir="${curDir}"	# use current directory if nothing specified
objDirPath="/obj/${SB_TARGET_ARCH}"
extraSuffix="/src"
showRoot=0
shellVarPrefix=""
shellVarSuffix=""
symbolicName=""
quiet=0
envVarList="HOME"

while test $# -gt 0
do
	case "${1}" in
	-h | -help | --help)
		printf "usage: %s [-q] [{--showrelative | --showroot | --symbolicroot | --symbolicpath}] [{--shell | --simpleshell}] [--envvar var] [--suffix extraSuffix] [completeDirPath]\n" "${0}" >&2
		printf "  -q = quiet\n" >&2
		printf "  --showrelative = default, show relative path\n" >&2
		printf "  --showroot = show just the root path\n" >&2
		printf "  --symbolicroot = show the root path using environment variable names\n" >&2
		printf "  --symbolicpath = show relative path using environment variable names\n" >&2
		printf '  --shell = encapsulate symbolic names with \"${\" and \"}\"\n' >&2
		printf '  --simpleshell = prefix symbolic names with \"$\"\n' >&2
		printf "  --envvar = add name to list of recognized symbolic environment variables.\n" >&2
		printf "    Initially defaults to \"%s\"\n" "${envVarList}" >&2
		printf "  --suffix specifies additional subdirectory, defaults to \"%s\"\n" "${extraSuffix}" >&2
		exit 1;
		;;
	-s | -suffix | --suffix)
		extraSuffix="${2}"
		shift
		;;
	-q)
		quiet=1
		;;
	-showrelative | --showrelative)
		showRoot=0
		;;
	-showroot | --showroot)
		showRoot=1
		;;
	-symbolicroot | --symbolicroot)
		showRoot=2
		;;
	-symbolicpath | --symbolicpath)
		showRoot=3
		;;
	-shell | --shell)
		shellVarPrefix='${'
		shellVarSuffix='}'
		;;
	-simpleshell | --simpleshell)
		shellVarPrefix='$'
		shellVarSuffix=''
		;;
	-envvar | --envvar)
		envVarList="${envVarList}${envVarList:+ }${2}"
		shift
		;;
	\.*)	# translated relative path to absolute
		# NOTE:  (cd ; pwd) is about 7 times faster than readlink
		fromDir=`(cd ${1}; pwd)`
		echo from dir set to ${fromDir} >&2
		;;
	-*)
		printf "%s: unrecognized option: %s\n" "${0}" "${1}" >&2
		exit 1
		;;
	*)
		fromDir="${1}"
		;;
	esac
	shift
done

root=""
# check local sandbox first
case "${fromDir}" in
	${STL:-NOTHING_STL}*)
		root="${STL}"
		symbolicName="${shellVarPrefix}STL${shellVarSuffix}"
		;;
	${STB:-NOTHING_STB}*)
		root="${STB}"
		symbolicName="${shellVarPrefix}STB${shellVarSuffix}"
		;;
	${STO:-NOTHING_STO}*)
		root="${STO}"
		symbolicName="${shellVarPrefix}STO${shellVarSuffix}"
		;;
	${ST:-NOTHING_ST}${extraSuffix}*)
		root="${ST}${extraSuffix}"
		symbolicName="${shellVarPrefix}STS${shellVarSuffix}"
		;;
	${ST:-NOTHING_ST}*)
		root="${ST}"
		symbolicName="${shellVarPrefix}ST${shellVarSuffix}"
		;;
esac

# If not found under sandbox, check backing trees...
if test -z "${root}"
then
	count=1
	eval dir1=\$BT${count}
	while test "${dir1}" != "" -a "${root}" = ""
	do
		dir2=`(cd ${dir1}; pwd)`
		if test "${dir1}" = "${dir2}"
		then # don't examine redundant directories
			dir2=""
		fi
		for d in "${dir1}" "${dir2}"
		do
			case "${fromDir}" in
			${d}${objDirPath}*)
				root="${d}${objDirPath}"
				symbolicName="${shellVarPrefix}BT${count}${shellVarSuffix}${objDirPath}"
				break
				;;
			${d}${extraSuffix}*)
				root="${d}${extraSuffix}"
				symbolicName="${shellVarPrefix}BT${count}${shellVarSuffix}${extraSuffix}"
				break
				;;
			${d}*)
				root="${d}"
				symbolicName="${shellVarPrefix}BT${count}${shellVarSuffix}"
				break
				;;
			esac
		done
		count=`expr ${count} + 1`
		eval dir1=\$BT${count}
	done

	if test -z "${root}"
	then
		for v in ${envVarList}
		do
			# Get value of environment variable
			eval varValue=\$${v}
			if test -n "${varValue}"
			then
				case "${fromDir}" in
				${varValue}*)
					root="${varValue}"
					symbolicName="${shellVarPrefix}${v}${shellVarSuffix}"
					break
					;;
				esac
			fi
		done

		if test -z "${root}"
		then
			newFromDir=`readlink -m ${fromDir} 2>/dev/null`
			if test -z "${newFromDir}"
			then # -m option not recognized by busybox
				# fallback to using -f
				newFromDir=`readlink -f ${fromDir} 2>/dev/null`
			fi
			if test -n "${newFromDir}"
			then
				fromDir="${newFromDir}"
			fi
		
			if test ${quiet} -eq 0
			then
				printf "No match for %s under sandbox tree\n" "${fromDir}" >&2
			fi
			if test ${showRoot} -ne 0
			then # only show the root that matched
				echo ${fromDir}
			else
				echo ""
			fi
			exit 1
		fi # end if 3rd test for root
	fi # end if 2nd test for root
fi # end if 1st test for root

case ${showRoot} in
0 | 3) # Normal case, show relative path
#	base=`expr substr ${fromDir} '(' length ${root} + 1 ')' '(' length ${fromDir} - length ${root} ')' `
	base=`awk -v "fromDir=${fromDir}" -v "root=${root}" 'BEGIN { f = length(fromDir); r = length(root); print substr(fromDir, r + 1, f - r); exit; }'`
	case ${showRoot} in
	0)
		echo ${base}
		;;
	3)
		printf "%s%s\n" "${symbolicName}" "${base}"
		;;
	esac
	;;
1) # only show the root that matched
	echo ${root}
	;;
2)
	echo "${symbolicName}"
	;;
esac
exit 0
