#!/bin/sh
# Visit each sandbox and execute command
# Copyright (C) 2012 - 2018 FARGOS Development, LLC
# $Id$
originDir=`dirname $0`
sandboxList=""
argList=""

while test $# -gt 0
do
	case "${1}" in
	-h | -help | --help)
		printf "usage: %s [{+sb sandbox} ... | +all] [-c] command args ...\n" "${0}" >&2
		exit 1
		;;
	+all)
		list=`${originDir}/lssb`
		sandboxList="${sandboxlist}${sandboxList:+ }${list}"
		;;
	+sb)
		sandboxList="${sandboxlist}${sandboxList:+ }${2}"
		shift
		;;
	-c | -command | -command)
		argList="${2}"
		shift	# consume flag
		shift	# consume executable name
		break
		;;
	*)
		argList="${argList}${argList:+ }${1}"
		;;
	esac
	shift
done

# if any arguments left, these go into the command list
while test $# -gt 0
do
	argList="${argList}${argList:+ }${1}"
	shift
done

if test -z "${argList}"
then
	printf "usage: %s [{+sb sandbox} ... | +all] command [arg] ...\n" `basename $0` >&2
	exit 1
fi

if test -z "${sandboxList}"
then # default to everything
	sandboxList=`${originDir}/lssb`
	if test -z "${sandboxList}"
	then
		echo Error: $0 cannot obtain any sandbox names using lssb >&2
	fi
fi

for sandbox in ${sandboxList}
do
	echo ${originDir}/workon -sb ${sandbox} -c ${argList}
	${originDir}/workon -sb ${sandbox} -c ${argList}
done
