#!/bin/sh
# bds-cli: workspace CLI for bDS2 (issue #25). Runs commands inside the
# release VM against the same settings and cache database as the app.
set -e

SELF_DIR="$(cd "$(dirname "$0")" && pwd)"
RELEASE_ROOT="$(cd "$SELF_DIR/../.." && pwd)"
export RELEASE_ROOT

# The TUI owns the terminal, so it boots the app instead of the one-shot CLI.
if [ "$1" = "tui" ]; then
  BDS_MODE=tui exec "$RELEASE_ROOT/bin/bds" start
fi

# `bin/bds eval` cannot forward arguments, so the argv travels in an
# environment variable, joined with the ASCII unit separator (0x1f).
US="$(printf '\037')"
ARGV=""
for arg in "$@"; do
  ARGV="${ARGV}${arg}${US}"
done

BDS_CLI_ARGV="$ARGV" BDS_MODE=cli exec "$RELEASE_ROOT/bin/bds" eval "BDS.CLI.main()"
