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

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] [--sb sandboxName] [{--bt backingTree} ...]\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\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 | -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}/:~/:`

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" "${d}" >&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

modifiedTargetArch=0
keysToDelete=""
if test -n "${fullPath}"
then
	keysToDelete="-e /SB_${sbName}_btpath_/d"
fi
if test -n "${targetArch}"
then
	modifiedTargetArch=1 # could be more sophisticated and check for real difference
	keysToDelete="${keysToDelete} -e /SB_${sbName}_defaultTargetArch/d"
fi
if test -n "${description}"
then
	keysToDelete="${keysToDelete} -e /SB_${sbName}_description/d"
fi

trap "rm -f ${tmpFile}" EXIT

sed ${keysToDelete} "${rcFile}" > "${tmpFile}"

if test -n "${fullPath}"
then
	printf "SB_%s_btpath_\t%s\n" "${sbName}" "${fullPath}" >> "${tmpFile}"
fi
if test -n "${targetArch}"
then
	printf "SB_%s_defaultTargetArch_\t%s\n" "${sbName}" "${targetArch}" >> "${tmpFile}"
	printf "SB_%s_SandboxRetargetted_\t1\n" "${sbName}" >> "${tmpFile}"
fi
if test -n "${description}"
then
	printf "SB_%s_description_\t%s\n" "${sbName}" "${description}" >> "${tmpFile}"
fi
sort "${tmpFile}" > "${rcFile}"
exit 0
