#!/bin/sh
# Copyright (C) 2012 - 2018 FARGOS Development, LLC
# Delete sandbox attributes
# $Id$
# delsbattr [--sb sandboxName] [--rc rcFile] attr

rcFile="${HOME}/.sandboxrc"
sbName="${SB_NAME}"	# default is current sandbox
attrList=""

while test $# -gt 0
do
	case "${1}" in
	-h | -help | --help)
		printf "usage: %s [--sb sandboxName] [--rc rcFile] attrName ...\n" "${0}" >&2
		exit 1
		;;
	-sb | --sb)
		sbName="${2}"
		shift
		;;
	-rc | --rc)
		rcFile="${2}"
		shift
		;;
	-*)
		printf "%s: unrecognized option \"%s\"\n" "${0}" "${1}" >&2
		exit 1
		;;
	*)
		attrList="${attrList}${attrList:+ }${1}"
		;;
	esac
	shift
done
if test -z "${attrList}"
then
	printf "%s: no attributes specified for deletion.\n" "${0}" >&2
	exit 1
fi

if test -z "$sbName"
then
	printf "%s: sandbox name was not specified.\n" "${0}" >&2
	exit 1
fi

keysToDelete=""
for attr in ${attrList}
do
	keysToDelete="-e /SB_${sbName}_${attr}_/d "
done
if test -z "${keysToDelete}"
then
	printf "%s: no keys were specified.\n" "${0}" >&2
	exit 1

fi

tmpFile="`dirname $rcFile`/newsbrcfile.txt"
trap "rm -f ${tmpFile}" EXIT

sed ${keysToDelete} "${rcFile}" > "${tmpFile}"
sort "${tmpFile}" > "${rcFile}"
exit 0
