#!/bin/zsh
setopt no_unset extended_glob warn_create_global

die() {
	printf >&2 '%s\n' "$@"
	exit 1
}

main() {
	local container sock
	[[ $1 == */* ]] && die "Invalid container name: ${(qqq)1}"
	container=$1
	shift 1 || exit $?
	sock=/run/inbox/$container/run/exec/exec 
	if [[ -S $sock ]]; then
		exec s6-sudo $sock "$@"
	else
		local arg
		local -a quoted
		for arg in "$@"; do
			quoted+=( "$(s6-quote -- "$arg")" )
		done
		powerbox-plumb "container-exec/$container/${(j: :)quoted}"
	fi
	
}
typeset -f -t main

main "$@"