#!/bin/sh
# Copyright (C) 2010 - 2018 FARGOS Development, LLC
# $Id$
# mkbt  [--btroot btRoot] --bt backingTree ...
#
# NOTE:  the directory $btRoot/config is used to hold prototypes
# of various files used by workon to setup the sandbox environment.
# Depends upon buildArch, simplestPathTo
# 
# shell_rc.* - shell_rc.${SHELL} will get used by workon
# buildEnv_${arch}_sh.txt - sets up build environment for local host
#  Usually, this would be to locate architecture-specific binaries
# targetEnv_${arch}_sh.txt - sets up target environment
#  Usually, this would be to set up cross-compiler environment

progName=`basename $0`
originDir=`dirname $0`
rcFile="${HOME}/.sandboxrc"
# default is first element of BT_ROOT_PATH
btRoot=`pathedit --space ${BT_ROOT_PATH:-/opt/backingTrees} | awk '{ print $1; }'`
toolsRoot="${SB_TOOLS_ROOT:-/opt/sandboxTools}"

btPath=""
copyLinks=0	# by default, use symbolic links
targetArch=""
interactive=0
existingNotOK=1

while test $# -gt 0
do
	case "${1}" in
	-h | -help | --help)
		printf "usage: %s [--btroot btRoot] [--ta targetArchitecture] [--copy] [--reuse] ... --bt backingTree [...]\n" "${0}" >&2
		printf "  --btroot = specify alternate backing tree root, default is \"%s\"\n" "${btRoot}" >&2
		targetArch=`${originDir}/buildArch`	# default to native architecture
		printf "  --ta = target architecture, default is native (%s)\n" "${targetArch}" >&2
		printf "	\"all\" means prepare for all supported\n" >&2
		printf "	\"avail\" means prepare for all locally supported\n" >&2
		exit 1
		;;
	-bt | --bt)
		btPath="${btPath}${btPath:+:}${2}"
		shift
		;;
	-btroot | --btroot)
		btRoot="${2}"
		shift
		;;
	-ta | --ta)
		targetArch="${targetArch}${targetArch:+ }${2}"
		shift
		;;
	-copy | --copy)
		copyLinks=1
		;;
	-reuse | --reuse)
		existingNotOK=0
		;;
	-*)
		printf "%s: unrecognized option \"%s\"\n" "${0}" "${1}" >&2
		exit 1
		;;
	*)
		printf "%s: unrecognized argument \"%s\"\n" "${0}" "${1}" >&2
		exit 1
		;;
	esac
	shift
done

if test -z "${btPath}"
then
	printf "%s: no backing trees specified.\n" "${0}" >&2
	exit 1
fi

if test -z "${targetArch}"
then
	targetArch=`${originDir}/buildArch`	# default to native architecture
fi

targetList=""
for arch in ${targetArch}
do
	case "${arch}" in
	all)
		targetList=${targetList}${targetList:+ }`${originDir}/buildArch -all`
		;;
	avail)
		targetList=${targetList}${targetList:+ }`${originDir}/buildArch -avail`
		;;
	*)	# use as-is
		targetList=${targetList}${targetList:+ }${arch}
		;;
	esac
done

fullPath=""
parsedPath=`echo ${btPath} | sed -e 's/:/ /g'`
error=0
for d in ${parsedPath}
do
	if test `dirname $d` = "."
	then # short form was used
		d="${btRoot}/${d}"
	fi
		
	if test -e "${d}" -a ${existingNotOK} -eq 1
	then
		printf "%s: %s already exists!\n" "${0}" "${d}" >&2
		error=1
		continue
	fi

	# create architecture-neutral directories
	for dir in src include BuildEnv
	do
		if test ! -e "${d}/${dir}"
		then
			mkdir -p "${d}/${dir}"
		fi
	done
	# copy any shell_rc.{csh|bash|...} files
	for f in "${toolsRoot}"/config/shell_rc.*
	do
		if test -r "${f}"
		then
			if test ${copyLinks} -eq 1
			then
				cp -p "${f}" "${d}/BuildEnv"
			else
				relLink=`${originDir}/simplestPathTo -from "${d}/BuildEnv" "${f}"`
				ln -s "${relLink}" "${d}/BuildEnv"
			fi
		fi
	done
	# create architecture-specific directories
	for arch in ${targetList}
	do
		# these are grouped to permit distribution as-is
		for dir in bin lib
		do
			if test ! -e "${d}/${arch}/${dir}"
			then
				mkdir -p "${d}/${arch}/${dir}"
			fi
		done
		# obj rooted elsewhere so whole tree can be cleaned
		for dir in obj
		do
			if test ! -e "${d}/${dir}/${arch}"
			then
				mkdir -p "${d}/${dir}/${arch}"
			fi
		done
		# establish symbolic links
		for envFile in "${toolsRoot}"/config/*_sh.txt "${toolsRoot}"/config/sbMakeRules.template
		do
			baseFile=`basename "${envFile}"`
			if test -r "${envFile}"
			then
				if test -e "${d}/BuildEnv/${baseFile}"
				then
					printf "%s: %s already exists, not replaced.\n" "${0}" "${d}/BuildEnv/${baseFile}" >&2
					continue
				fi
				if test ${copyLinks} -eq 1
				then
					cp -p "${envFile}" "${d}/BuildEnv"
				else
					relLink=`${originDir}/simplestPathTo -from "${d}/BuildEnv" "${envFile}"`
					ln -s "${relLink}" "${d}/BuildEnv"
				fi
				case "${baseFile}" in
				targetEnv*)
					prefix=`basename ${baseFile} _sh.txt`
					if test ${copyLinks} -eq 1
					then
						(cd "${d}/BuildEnv"; ln "${prefix}_sh.txt" "${prefix}_local_sh.txt")
					else
						(cd "${d}/BuildEnv"; ln -s "${prefix}_sh.txt" "${prefix}_local_sh.txt")
					fi
					;;
				esac

			fi
		done
	done
done

exit 0
