#!/bin/sh
# Copyright (C) 2012 - 2018 FARGOS Development, LLC
# $Id$
# Clone backing directory tree hierarchy under local sandbox

pathSeparator="${BT_PATH_SEPARATOR:-:}"

while test $# -gt 0
do
	case "${1}" in
	-h | -help | --help)
		printf "usage: %s\n" "${0}" "${btRoot}" "${sbRoot}" >&2
		printf "  Will clone associated backing tree structure\n" >&2
		exit 1
		;;
	-*)
		printf "%s: unrecognized option \"%s\"\n" "${0}" "${1}" >&2
		exit 1
		;;
	*)
		printf "%s: unrecognized argument \"%s\"\n" "${0}" "${1}" >&2
		exit 1
		;;
	esac
done


workFile="/tmp/tmp.cdir.$$"
if test -z "${STS}"
then
	printf "%s:  must be invoked under a workon session\n" "${0}" >&2
	exit 1
fi

# auto-delete temporary file
TRAP "rm -f ${workFile}" EXIT

# Split BT_PATH, reverse so that any merged content retains precedence
# NOTE: could do pathCondense --space ${BT_PATH}
btElems=`echo ${BT_PATH} | sed -e "s/${pathSeparator}/ /g"`
reversedBTelems=""
for d in ${btElems}
do
	reversedBTelems="${d}${reversedBTelems:+ }${reversedBTelems}"
done


for d in ${reversedBTelems}
do
	(cd ${d}; find -L . -type d -print >> ${workFile})
done

cd "${STS}"
sed -e '/[/][\.]svn/d' -e '/^[\.]$/d' -e 's/[\.][/]//' ${workFile} | sort | uniq | while read dir
do
	echo mkdir -p ${dir}
	mkdir -p "${STS}/${dir}"
done
