#!/bin/sh
# Copyright (C) 2010 - 2019 FARGOS Development, LLC
# $Id$
# Create a work session under a sandbox environment
# workon -sb sandboxName
originDir=`dirname $0`
progName=`basename $0`
thisOS=`uname -s`
thisCPU=`uname -m`
originRoot=`dirname ${originDir}`
# condense PATH to remove redundant and nonexistent directories
PATH=`(PATH=${PATH}:${originRoot}/${thisOS}/${thisCPU}/bin:${originDir} pathCondense.sh -x "${PATH}")`
pathCondenseProg=`which pathCondense 2>/dev/null`
if test -z "${pathCondenseProg}"
then
	colorText -fg -yellow -nulltext -bg -black Performance warning: native version of -fg -blue pathCondense -fg -yellow not found -reset >&2
	pathCondenseProg=`which pathCondense.sh 2>/dev/null`
fi
findFileProg=`which findFile 2>/dev/null`
if test -z "${findFileProg}"
then
	colorText -fg -yellow -nulltext -bg -black Performance warning: native version of -fg -blue findFile -fg -yellow not found -reset >&2
	findFileProg=`which findFile.sh 2>/dev/null`
fi

rcFile="${HOME}/.sandboxrc"
priorSB_NAME="${SB_NAME}"
pathSeparator="${BT_PATH_SEPARATOR:-:}"
useShell="${SB_SHELL:-${SHELL}}"
cmd=""
buildArch=""
targetArch=""
shortRoot=""
noInit=0
traceEnv=0
doSSH="${SB_USE_SSH_AGENT:-0}"
debugTrace=0 # not currently used

while test $# -gt 0
do
	case "${1}" in
	-h | -help | --help)
		printf "usage: %s --sb sandboxName [--ta targetArchitecture] [--bt backingTree] [--ba buildArchitecture] [--rc rcFile] [{--ssh | --nossh}] [[-c ] cmd ....]\n" "${0}" >&2
		printf "  --ta = specify target architecture\n" >&2
		printf "  --rc = override sandbox rc file, default=\"%s\"\n" "${rcFile}" >&2
		printf "  --bt = override backing tree\n" >&2
		printf "  --ba = specify build architecture (not normally used)\n" >&2
		printf "  --ssh = setup ssh keys and start ssh-agent if needed\n" >&2
		exit 1
		;;
	-trace | --trace)
		traceEnv=`expr ${traceEnv} + 1`
		;;
	-sb | --sb)
		sbName="${2}"
		shift
		;;
	-sbroot | --sbroot)
		shortRoot="${2}"
		shift
		;;
	-rc | --rc)
		rcFile="${2}"
		shift
		;;
	-ba | --ba)
		buildArch="${2}"
		shift
		;;
	-ta | --ta)
		targetArch="${2}"
		shift
		;;
	-noinit | --noinit)
		noInit=1
		;;
	-ssh | --ssh)
		doSSH=1
		;;
	-nossh | --nossh)
		doSSH=0
		;;
	-c | -command | --command)
		shift # consume -c flag
		cmd="${1}"
		shift # consume executable name
		break # exit option parsing
		;;
	*)
		cmd="${1}"
		shift # consume executable name
		break # exit option parsing
		;;
	esac
	shift
done

if test -z "${sbName}"
then
	printf "%s: sandbox name was not specified.\n" "${0}" >&2
	exit 1
fi
if test `expr "${sbName}" : '.*/'` -ne 0
then
	printf "%s:  sandbox name cannot contain \"/\": %s\n" `basename $0` "${sbName}" >&2
	exit 1
fi
if test -z "${buildArch}" # unspecified
then
	buildArch=`buildArch`	# use native architecture
fi
# extract operating system from build architecture
buildOS=`echo ${buildArch} | awk -F. '{ print $1; }'`
buildCPU=`echo ${buildArch} | awk -F. '{ print $2; }'`

SB_SHELL="${useShell}"
SB_NAME="${sbName}"
SB_BUILD_ARCH="${buildArch}"
if test -z "${shortRoot}"
then
	shortRoot=`getsbattr -null -rc "${rcFile}" -sb "${sbName}" sbroot`
	if test -z "${shortRoot}"
	then
		printf "%s: Sandbox %s was not defined.\n" `basename $0` ${sbName} >&2
		exit 1
	fi
fi

# Expand "~" into HOME directory path on this machine
ST=`eval echo ${shortRoot}`

if test -z "${targetArch}" # wasn't specified as argument
then
	targetArch=`getsbattr --null --rc "${rcFile}" --sb "${sbName}" defaultTargetArch`

	if test -z "${targetArch}"
	then
		targetArch="${SB_BUILD_ARCH}"
	fi
fi
case "${targetArch}" in
+*) # just modifier specified, so use build architecture as implied base
	targetArch="${SB_BUILD_ARCH}${targetArch}"
	;;
*)
	;;
esac
nameElements=`echo ${targetArch} | awk -F. '{ print NF; }'`
if test ${nameElements} -eq 1
then
	colorText Assuming -fg -cyan .${targetArch} -reset variant of native architecture -fg -cyan ${SB_BUILD_ARCH} -reset
	targetArch="${SB_BUILD_ARCH}.${targetArch}"
fi
osAndCPU=`echo ${targetArch} | sed -e 's/+/ /g' | awk '{ print $1; }'`
# extract target operating system
targetArchOS=`echo ${osAndCPU} | awk -F. '{ print $1; }'`
SB_TARGET_OS="${targetArchOS}"

baseTargetArch=`buildArch --base ${osAndCPU}`
cpuTargetArch=`echo ${baseTargetArch} | awk -F. '{ print $2; }'`
targetArchQualifier=`echo ${targetArch} | sed -e s/${baseTargetArch}//`

# baseTargetArch should be something like Linux.x86_64, Solaris.sun4u,
# AIX.power, Linux.s390x, etc.
# targetArchQualifier should be everything but the baseTargetArch,
#   so something like ".release", ".lto", ".memsanity,local", etc.
# Normally, the targetArchQualifer is a single word, but it may also be a
#   comma-and-plus-separated list.  Comma-separated elements are
#   joined to create a unique target architecture, whereas plus-separated items
#   are treated as distinct target architectures that are merged together.
#   Thus "x64_64.release,local" is a unique target architecture "release,local"
#   but "x64_64.release+memsanity" should load the settings for both "release"
#   and "memsanity".

# phases:  preTreeEnv_sh.txt, targetEnv${arch}, targetDefault${arch}, postTreeEnv_sh.txt
# 

subTargetCount=0
didSpecializedTarget=0
extraSubTargets=""
extraSubDefaults=""
subTargetList=""

if test -n "${targetArchQualifier}"
then
	colorText 'Using specialized variant' -fg -cyan "${targetArchQualifier}" -reset 'of base architecture' -fg -cyan "${baseTargetArch}" -reset
	didSpecializedTarget=1
	# drop leading period, convert commas and plus to spaces
	# "+" converted to " @ " to act as separator AND state change marker
	subTargets=`echo ${targetArchQualifier} | sed -e 's/\.//' -e 's/,/ /g' -e 's/+/ @ /g'`
#	echo subTargets=${subTargets} qualifier=${targetArchQualifier}

	addArch=1
	archSuffix="${baseTargetArch}"
	sepChar="."
	for subTgt in ${subTargets}
	do
		if test "${subTgt}" = "@"
		then # was "+" suffix
			addArch=0
			continue # done with "@" character
		fi
		subTargetCount=`expr ${subTargetCount} + 1`
		subTargetList="${subTargetList} ${subTgt}"
#			extraSubTargets="${extraSubTargets} targetEnv_${baseTargetArch}.${subTgt}_sh.txt"
#			extraSubDefaults="${extraSubDefaults} targetDefault_${targetArchOS}.ANY.${subTgt}_sh.txt"
		if test ${addArch} -eq 1
		then
			archSuffix="${archSuffix}${sepChar}${subTgt}"
			sepChar=","
		fi
		addArch=1
	done
	if test ${subTargetCount} -ne 0
	then
		echo Add ${subTargetCount} extra targets ${subTargetList}
	fi
	targetArch="${archSuffix}"
	colorText -fg -cyan SB_TARGET_ARCH -reset 'set to' -fg -cyan ${targetArch} -reset
fi # end if subtargets

SB_TARGET_ARCH="${targetArch}"

# Get btpath attribute, expand ~ references into $HOME
btPathData=`getsbattr -null -rc "${rcFile}" -sb "${sbName}" btpath`
BT_PATH=`eval echo ${btPathData}` # expand variables, "~"
btElems=`echo ${BT_PATH} | sed -e "s/${pathSeparator}/ /g"`

# For convenience, define place backing tree names BT1...BTn
dCount=1
for d in ${btElems}
do
	eval BT${dCount}="${d}"
	export BT${dCount}
	BT_FINAL="${d}" # remember final directory in chain
	dCount=`expr ${dCount} + 1`
done
export BT_FINAL		# expose root of final backing tree

BT_FINAL_SRC="${BT_FINAL}/src"
export BT_FINAL_SRC	# expose root of final source tree

STS="${ST}/src"			# sandbox source tree
STI="${ST}/include"		# installed target

STO="${ST}/obj/${SB_TARGET_ARCH}"	# build objects under here
STL="${ST}/${SB_TARGET_ARCH}/lib"	# installed target 
STB="${ST}/${SB_TARGET_ARCH}/bin"	# installed target

# export sandbox environment variables
for envName in SB_NAME SB_BUILD_ARCH SB_TARGET_ARCH SB_TARGET_OS ST BT_PATH STS STO STL STI STB SB_SHELL
do
	export ${envName}
done

if test false
then # dont do this
# set environment variables:
CMAKE_INCLUDE_PATH=""
CMAKE_LIBRARY_PATH=""
CMAKE_PROGRAM_PATH=""
# CMAKE_FRAMEWORK_PATH=""

for d in $ST $btElems
do
	CMAKE_INCLUDE_PATH="${CMAKE_INCLUDE_PATH}${CMAKE_INCLUDE_PATH:+:}${d}/include"
	CMAKE_LIBRARY_PATH="${CMAKE_LIBRARY_PATH}${CMAKE_LIBRARY_PATH:+:}${d}/${SB_TARGET_ARCH}/lib"
	# architecture-specific, then generic
	CMAKE_PROGRAM_PATH="${CMAKE_PROGRAM_PATH}${CMAKE_PROGRAM_PATH:+:}${d}/${SB_TARGET_ARCH}/bin"
	CMAKE_PROGRAM_PATH="${CMAKE_PROGRAM_PATH}:${d}/bin"
done

# export build framework environment variables
for envName in CMAKE_INCLUDE_PATH CMAKE_LIBRARY_PATH CMAKE_PROGRAM_PATH CMAKE_BINARY_DIR CMAKE_HOME_DIRECTORY
do
	# only retain directories that exist
	p=`eval ${pathCondenseProg} -x \\\$${envName}`
	if test -n "${p}"
	then
		eval ${envName}="${p}"
		export ${envName}
	fi
done

# COMPILE_FLAGS, COMPILE_DEFINITIONS
fi # end dont do this

nativeArch=`buildArch -native`	# get native architecture
# if target architecture name has same prefix as local hardware, we can
# include the sandbox's bin directory in the PATH
if test `expr "${SB_TARGET_ARCH}" : "${nativeArch}"` != "0"
then
	sbBinDir="${ST}/${SB_BUILD_ARCH}/bin ${ST}/${SB_TARGET_ARCH}/bin"
	for d in ${btElems}
	do
		# prepend in preparation of pathedit below
		sbBinDir="${d}/${SB_BUILD_ARCH}/bin ${d}/${SB_TARGET_ARCH}/bin ${sbBinDir}"
	done
else # not compatible with build machine
	colorText -fg -yellow -nulltext -bg -black "Builds for" -fg -cyan ${SB_TARGET_ARCH} -fg -yellow "are not compatible with native architecture" -fg -cyan ${nativeArch} -reset
	sbBinDir="${ST}/${SB_BUILD_ARCH}/bin"
	builtCount=`ls ${sbBinDir} 2>/dev/null | wc -l`
	if test "${builtCount}" -eq 0
	then
		# allow "noIntermediateDependencies" attribute to silence warning
		nothingBuiltNeeded=`getsbattr -null -rc "${rcFile}" -sb "${sbName}" noIntermediateDependencies`
		if test "${nothingBuiltNeeded}" != "1"
		then
			colorText -fg -red -nulltext -bg -black "WARNING: a native architecture build for" -fg -cyan ${SB_BUILD_ARCH} -fg -red "should be done first if executables are needed for build" -reset
			if test -z "${nothingBuiltNeeded}"
			then
				colorText -fg -cyan -nulltext -bg -black "This warning can be disabled by:" -fg -green setsbattr noIntermediateDependencies=1 -reset
			fi
		fi
	fi
fi
# prepend to path (note that rightmost argument will have priority)
PATH=`pathedit -r "${PATH}" .`
PATH=`pathedit -p "${PATH}" ${originDir} ${sbBinDir} .`
PATH=`${pathCondenseProg} -x "${PATH}"`
export PATH
VPATH="${ST}${pathSeparator}${BT_PATH}"
export VPATH


# NOTE: these environment files are Bourne shell scripts because they are
# included by this script.
# Order is:
# buildEnv_${SB_BUILD_ARCH}_sh.txt to pick up environment for build tools
# buildEnv_${SB_BUILD_ARCH}.${targetArch}_sh.txt


usedBuildEnv=""
usedTargetEnv=""
usedTargetDefault=""
usedCompilerDefault=""
usedCompilerSubDefault=""

for pass in 1 2 3 4 5 6 7 8 9 10
do
	case ${pass} in
	1)
		fileList="buildEnv_${buildOS}.ALL_sh.txt buildEnv_${SB_BUILD_ARCH}.ALL_sh.txt"
		for subTgt in ${subTargetList}
		do
			fileList="${fileList} buildEnv_${targetArchOS}.ALL.${subTgt}_sh.txt"
		done

		;;
	2)
		fileList="buildEnv_${SB_BUILD_ARCH}_sh.txt  buildEnv_${SB_BUILD_ARCH}${targetArchQualifier}_sh.txt"
		for subTgt in ${subTargetList}
		do
#			echo subTgt=${subTgt}
			fileList="${fileList} buildEnv_${targetArchOS}.${subTgt}_sh.txt"
#			eval didTgtEnv_${subTgt}=0
		done
		fileList=`${pathCondenseProg} --space ${fileList} buildEnv_${buildOS}.ANY${targetArchQualifier}_sh.txt buildEnv_${buildOS}.ANY_sh.txt`
		;;
	3)
		fileList="buildEnv_FINAL_sh.txt preTreeEnv_sh.txt targetEnv_${targetArchOS}.ALL_sh.txt targetEnv_${baseTargetArch}.ALL_sh.txt targetEnv_ALL.${cpuTargetArch}_sh.txt"
		for subTgt in ${subTargetList}
		do
			fileList="${fileList} targetEnv_${targetArchOS}.ALL.${subTgt}_sh.txt targetEnv_ALL.${subTgt}_sh.txt"
		done
		fileList=`${pathCondenseProg} --space ${fileList} targetEnv_${SB_TARGET_ARCH}_sh.txt`
		;;
	4)
		fileList=""
		if test "${baseTargetArch}" != "${SB_TARGET_ARCH}"
		then
			fileList="targetEnv_${baseTargetArch}_sh.txt"
			for subTgt in ${subTargetList}
			do
	#			echo subTgt=${subTgt}
				fileList="${fileList} targetEnv_${targetArchOS}.${subTgt}_sh.txt"
	#			eval didTgtEnv_${subTgt}=0
			done
		fi
		;;
	5)
		fileList=""
		for subTgt in ${subTargetList}
		do
			fileList="${fileList} targetEnv_${targetArchOS}.ANY.${subTgt}_sh.txt"
#			eval didTgtEnv_ANY_${subTgt}=0
		done
		;;
	6)
		fileList="targetEnv_ANY.${cpuTargetArch}_sh.txt targetEnv_${targetArchOS}.ANY_sh.txt targetEnv_FINAL_sh.txt"
		;;
	7)
		useCompiler=`getCompilerVersion --showprog | awk '{ print $1; }'`
		fileList="compilerDefaults_${useCompiler}.ALL_sh.txt"
		for subTgt in ${subTargetList}
		do
			fileList="${fileList} compilerDefaults_${useCompiler}.ALL.${subTgt}_sh.txt"
#			eval didTgtEnv_ANY_${subTgt}=0
		done
		for subTgt in ${subTargetList}
		do
			fileList="${fileList} compilerDefaults_ALL.${subTgt}_sh.txt"
#			eval didTgtEnv_ANY_${subTgt}=0
		done

		fileList=`${pathCondenseProg} --space ${fileList} compilerDefaults_${SB_TARGET_ARCH}_sh.txt compilerDefaults_${useCompiler}.${baseTargetArch}_sh.txt compilerDefaults_${useCompiler}.${cpuTargetArch}_sh.txt`
		;;
	8)
		fileList=""
		for subTgt in ${subTargetList}
		do
			fileList="${fileList} compilerDefaults_${useCompiler}.ANY.${subTgt}_sh.txt"
#			eval didTgtEnv_ANY_${subTgt}=0
		done
		for subTgt in ${subTargetList}
		do
			fileList="${fileList} compilerDefaults_ANY.${subTgt}_sh.txt"
#			eval didTgtEnv_ANY_${subTgt}=0
		done
		;;
	9)
		fileList="compilerDefaults_${useCompiler}.ANY_sh.txt compilerDefaults_FINAL_sh.txt"
		;;
	10)
		fileList="postTreeEnv_sh.txt sbEnv_sh.txt"
		;;
	esac
	for f in ${fileList}
	do
		if test ${traceEnv} -gt 0
		then
			echo Pass ${pass} look for file ${f}
		fi
		found=0
		for rootDir in ${ST} ${btElems}
		do # search sandbox and backing trees
			for d in "${rootDir}/src" "${rootDir}"
			do
				fileName="${d}/BuildEnv/${f}"
				
				if test ! -e ${fileName}
				then # skip to next 
					continue
				fi
				found=1
				break
			done
			if test ${found} -ne 0
			then
				break
			fi
		done
		if test ${found} -eq 0
		then # file was not found, try next one
			continue
		fi
		if test "${f}" = "${extraTarget}"
		then
			if test ${didSpecializedTarget} -eq 2
			then
				colorText -fg -cyan "Not processing generic" -fg -blue ${fileName} -fg -cyan "as specialized already used." -reset
				continue;
			else
				printf "Processing generic %s as no specialized version available.\n" "${fileName}"
			fi
		fi

		case "${f}" in
		buildEnv_*ALL_sh.txt)
			printf "BUILD environment for ALL %s\n" "${f}"
			# always consume, does not count as a specialization
			;;
		buildEnv_*ANY*)
			printf "BUILD environment for ANY %s\n" "${f}"
			if test -n "${usedBuildEnv}"
			then # already used a more specific default file
				printf "Skipping %s because already read %s\n" "${f}" "${usedBuildEnv}" >&2
				continue
			fi
			;;
		buildEnv_*)
			usedBuildEnv="${usedBuildEnv}${usedBuildEnv:+ }${f}"
			;;
		compilerDefaults_*ALL*)
			printf "COMPILER default for ALL %s\n" "${f}"
			# always consume, does not count as a specialization
			;;
		compilerDefaults_${useCompiler}*ANY.*)
			printf "COMPILER default for %s ANY %s\n" "${useCompiler}" "${f}"
			if test -n "${usedCompilerSubDefault}"
			then # already used a more specific default file
				printf "Skipping %s because already read %s\n" "${f}" "${usedCompilerSubDefault}" >&2
				continue
			fi
			subType=`echo ${f} | sed -e 's/compilerDefaults_//' -e 's/_sh.txt//'`
#			echo DEFAULT subType=${subType} base=${baseTargetArch}
			usedCompilerSubDefault="${usedCompilerSubDefault}${usedCompilerSubDefault:+ }${f}"
			;;
		compilerDefaults_*ANY*)
			printf "COMPILER default for ANY %s\n" "${f}"
			if test -n "${usedCompilerDefault}"
			then # already used a more specific default file
				printf "Skipping %s because already read %s\n" "${f}" "${usedCompilerDefault}" >&2
				continue
			fi
			subType=`echo ${f} | sed -e 's/compilerDefaults_//' -e 's/_sh.txt//'`
#			echo DEFAULT subType=${subType} base=${baseTargetArch}
			usedCompilerDefault="${usedCompilerDefault}${usedCompilerDefault:+ }${f}"
			;;
		compilerDefaults_${useCompiler}.*)
			subType=`echo ${f} | sed -e 's/compilerDefaults_//' -e 's/_sh.txt//'`
#			echo DEFAULT subType=${subType} base=${baseTargetArch}
			usedCompilerSubDefault="${usedCompilerDefault}${usedCompilerDefault:+ }${f}"
			;;
		compilerDefaults_*)
			subType=`echo ${f} | sed -e 's/compilerDefaults_//' -e 's/_sh.txt//'`
#			echo DEFAULT subType=${subType} base=${baseTargetArch}
			usedCompilerDefault="${usedCompilerDefault}${usedCompilerDefault:+ }${f}"
			;;
		targetEnv_*ALL*)
			printf "TARGET environment for ALL %s\n" "${f}"
			# always consume, does not count as a specialization
			;;
		targetEnv_ANY*) # any OS
			subType=`echo ${f} | sed -e 's/targetEnv_ANY.//' -e 's/_sh.txt//'`
#			echo ENV subType=${subType} cpubase=${cpuTargetArch}
			usedTargetEnv="${usedTargetEnv}${usedTargetEnv:+ }${f}"
			if test "${subType}" != "${cpuTargetArch}"
			then
				echo Specialized CPU architecture
				didSpecializedTarget=2
			fi
			;;
		targetEnv_*ANY*)
			printf "TARGET environment for ANY %s\n" "${f}"
			if test -n "${usedTargetEnv}"
			then # already used a more specific default file
				printf "Skipping %s because already read %s\n" "${f}" "${usedTargetEnv}" >&2
				continue
			fi
			;;
		targetEnv_*)
			subType=`echo ${f} | sed -e 's/targetEnv_//' -e 's/_sh.txt//'`
#			echo ENV subType=${subType} base=${baseTargetArch}
			usedTargetEnv="${usedTargetEnv}${usedTargetEnv:+ }${f}"
			if test "${subType}" != "${baseTargetArch}"
			then
				didSpecializedTarget=2
			fi
			;;
		esac


		colorText Loading -fg -cyan ${fileName} -reset ...
		if test ${traceEnv} -gt 1
		then
			set -x
		fi
		. ${fileName}
		if test ${traceEnv} -gt 1
		then
			set +x
		fi
	done # end each file name
done # end each pass
colorText "Compiler:" -fg -cyan "${_compilerProg:-unknown}" -reset "version:" -fg -cyan "${_compilerVersion:-unknown}" -reset

if test -z "${SSH_AUTH_SOCKET}" -a ${doSSH} -eq 1
then
	printf "Starting ssh-agent..."
	# we are running as a Bourne-shell, so request Bourne-shell commands
	eval `ssh-agent -s`
	sshAgentPID=${SSH_AGENT_PID}
	trap "kill ${sshAgentPID}" EXIT
fi
if test ${doSSH} -eq 1
then
	identities=`ssh-add -l`
	addStatus=$?
else
	addStatus=0
fi
if test ${addStatus} -eq 1
then
	ssh-add
fi

initScript=""
if test -n "${cmd}"
then # run specified command, overlay this process
	cd ${STS}
	eval exec "${cmd}" "$@"
	# only reached if exec fails
	exit 1
fi

if test -n "${priorSB_NAME}"
then
	colorText -fg -black -nulltext -bg -magenta "CAUTION:  this workon is nested; you were already in sandbox" -fg -cyan "\"${priorSB_NAME}\"" -reset
fi
sbRetargetted=`getsbattr -null -rc ${rcFile} -sb ${sbName} SandboxRetargetted`
if test "${sbRetargetted}" = "1"
then
	colorText -fg -magenta -nulltext -bg -black "Sandbox target architecture was changed.\n" -reset
	sb_retarget
	delsbattr -rc ${rcFile} -sb ${sbName} SandboxRetargetted
fi
colorText "Entering sandbox:" -fg -cyan '"'${SB_NAME}'"' -reset "native:" -fg -cyan ${SB_BUILD_ARCH} -reset "target:" -fg -cyan ${SB_TARGET_ARCH} -reset
shellType=`basename ${useShell}`
#echo findFile BuildEnv/rc.${shellType} "$VPATH"
if test ${noInit} -eq 0 # normal case
then
# NOTE:  the init script is shell-specific and is used by the spawned
# subshell, not this Bourne shell script.
# Only the first instance found will be used (-1 flag).
	initScript=`${findFileProg} -1 BuildEnv/shell_rc.${shellType} ${VPATH}`
#	if test -z "${initScript}"
#	then # fall back to the config directory
#		initScript=`findFile -1 config/shell_rc.${shellType} ${VPATH}`
#	fi
fi
if test -n "${initScript}"
then
	case ${shellType} in
	bash | sh)
		commandOpt="--rcfile ${initScript}"
		;;
	csh)
		commandOpt="-c source\\ ${initScript}"
		;;
	*)
		commandOpt="-c source\\ ${initScript}"
		;;
	esac
fi
cd ${STS}; colorText "Directory:" -fg -cyan `pwd` -reset
	eval ${useShell} "${commandOpt}"
exit
