#!/bin/sh
# Copyright (C) 2010 - 2018 FARGOS Development, LLC
# $Id$
# mksb [-i] [--rc rcFile] [--btroot btRoot] [--sbroot sbRoot] --sb sandboxName [--bt backingTree][...]
#
progName=`basename $0`
originDir=`dirname $0`
rcFile="${HOME}/.sandboxrc"
# default backing tree is first element of BT_ROOT_PATH
btRoot=`pathedit --space ${BT_ROOT_PATH:-/opt/backingTrees} | awk '{ print $1; }'`
sbRoot="${SB_ROOT_DIR:-${HOME}/sandboxes}"
btPath=""
description=""
interactive=0
force=0
sbName=""

while test $# -gt 0
do
	case "${1}" in
	-h | -help | --help)
		printf "usage: %s [-i] [--rc rcFile] [--btroot btRoot] [--sbroot sbRoot] [--desc description] [--ta targetArch] [--force] [{--bt backingTree} ...] --sb sandboxName\n" "${0}" >&2
		printf "  -i = interactive mode\n" >&2
		printf "  --rc = specify alternate rc file, default is \"%s\"\n" "${rcFile}" >&2
		printf "  --btroot = specify alternate backing tree root, default is \"%s\"\n" "${btRoot}" >&2
		printf "  --sbroot = specify alternate sandbox root, default is \"%s\"\n" "${sbRoot}" >&2
		printf "  --desc = descriptive note for sandbox\n" >&2
		printf "  --ta = desired target architecture, defaults to \"%s\"\n" `buildArch` >&2
		printf "  --force = allow mksb to tolerate existing sandbox directory or missing backing tree\n" >&2
		exit 1
		;;
	-i)
		interactive=1
		;;
	-sb | --sb)
		sbName="${2}"
		shift
		;;
	-bt | --bt)
		btPath="${btPath}${btPath:+:}${2}"
		shift
		;;
	-btroot | --btroot)
		btRoot="${2}"
		shift
		;;
	-sbroot | --sbroot)
		sbRoot="${2}"
		shift
		;;
	-ta | --ta)
		targetArch="${2}"
		shift
		;;
	-force | --force)
		force=1
		;;
	-rc | --rc)
		rcFile="${2}"
		shift
		;;
	-desc | --desc | -description | --description)
		description="${2}"
		shift
		;;
	-*)
		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 "${sbName}"
then
	printf "%s: sandbox name was not specified.\n" "${0}" >&2
	if test ${interactive} -eq 0
	then
		exit 1
	fi
	printf "  Enter name of sandbox: "
	read sbName
	if test -z "${sbName}"
	then
		printf "%s: sandbox name was not specified.\n" "${0}" >&2
		exit 1
	fi
fi
tmpFile=`dirname "${rcFile}"`/newsbrcfile.txt


# replace HOME directory in sandbox path with "~" so that sandboxes can be
# used on multiple machines where the home directory might be mounted
# under a different directory structure.
shortRoot=`echo ${sbRoot}|sed s:${HOME}/:~/:`

existingName=`${originDir}/getsbattr -null -rc ${rcFile} -sb ${sbName} sbname 2>/dev/null`
if test -n "${existingName}"
then
	printf "%s: configuration data for sandbox \"%s\" already exists!\n" "${0}" ${sbName} >&2
	exit 1
fi
if test -e "${sbRoot}/${sbName}"
then
	if test ${force} -eq 0
	then
		printf "%s: sandbox directory %s already exists!\n" "${0}" "${sbRoot}/${sbName}" >&2
		exit 1
	else
		printf "%s: warning - sandbox directory %s already exists!\n" "${0}" "${sbRoot}/${sbName}" >&2
		printf "  Tolerated because force flag is set.\n"
	fi
fi
if test -z "${description}"
then
	description="Sandbox under ${sbRoot}"
fi

if test -z "${btPath}"
then
	btPath=`${originDir}/getsbattr -null -rc "${rcFile}" -sb "${sbName}" btpath | sed -e s:~/:${HOME}/:g`
fi

if test -z "${btPath}"
then
	printf "%s: no backing trees specified.\n" "${0}" >&2
	if test ${interactive} -eq 0
	then
		exit 1
	fi
	availBT=`${originDir}/lsbt -s " " -n -btroot "${btRoot}"`
	printf "Available backing trees: "
	echo ${availBT}
	# prompt for default backing tree
	printf "  Enter backing tree name or path: "
	read btPath
	if test -z "${btPath}"
	then
		printf "%s: no backing tree specified.\n" "${0}" >&2
		exit 1
	fi
fi
fullPath=""
parsedPath=`echo ${btPath} | sed -e 's/:/ /g'`
error=0
for d in ${parsedPath}
do
	if test `dirname "${d}"` = "."
	then # short form was used
		dirPath="${sbRoot}/${d}"
		# allow sandbox to have priority over backing tree
		if test -d "${dirPath}"
		then
			dirAlias="${shortRoot}/${d}"
		else # try backing tree
			dirPath="${btRoot}/${d}"
			dirAlias="${dirPath}"
		fi
	else
		dirPath="${d}"
		dirAlias=`echo ${dirPath} | sed -e "s:${HOME}/:~/:g"`
	fi
		
	if test -d "${dirPath}"
	then
		fullPath="${fullPath}${fullPath:+:}${dirAlias}"
	else
		printf "%s: %s is not a directory\n" "${0}" "${dirPath}" >&2
		error=1
	fi
done
if test ${error} -eq 1
then
	if test ${force} -eq 0
	then
		printf "%s: backing trees not correctly specified.\n" "${0}" >&2
		exit 1
	else
		printf "  Tolerated because force flag is set.\n"
	fi
fi

# mksb - make a brand-new sandbox
if test -z "${fullPath}"
then
	printf "%s: no backing tree specified.\n" "${0}" >&2
	exit 1
fi

# create architecture-neutral directories
for dir in src BuildEnv
do
	if test ! -e "${sbRoot}/${sbName}/${dir}"
	then
		mkdir -p "${sbRoot}/${sbName}/${dir}"
	fi
done

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

case "${targetArch}" in
all)
	targetList=`buildArch -all`
	;;
avail)
	targetList=`buildArch -avail`
	;;
*)
	targetList="${targetArch}"	# use as-is
	;;
esac

# create architecture-specific directories
for arch in ${targetList}
do
	for dir in obj 
	do
		if test ! -e "${sbRoot}/${sbName}/${dir}/${arch}"
		then
			mkdir -p "${sbRoot}/${sbName}/${dir}/${arch}"
		fi
	done
	for dir in bin lib
	do
		if test ! -e "${sbRoot}/${sbName}/${arch}/${dir}"
		then
			mkdir -p "${sbRoot}/${sbName}/${arch}/${dir}"
		fi
	done
done

# populate rc file with initial parameters
cat >> ${rcFile} <<endOfText
SB_${sbName}_sbname_	${sbName}
SB_${sbName}_sbroot_	${shortRoot}/${sbName}
SB_${sbName}_btpath_	${fullPath}
SB_${sbName}_defaultTargetArch_	${targetArch}
SB_${sbName}_description_	${description}
endOfText


exit 0
