#!/usr/bin/env bash
# Shim that forwards `opencode` to the vendored elizaOS/opencode source tree.
# plugin-agent-orchestrator's AcpService points acpx at this shim so we run
# the vendored source with provider-specific compatibility fixes without
# needing a compiled binary.
SHIM_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
OC_DIR="$SHIM_DIR/../vendor/opencode/packages/opencode"
if [ -f "$OC_DIR/src/index.ts" ]; then
  exec bun run --cwd "$OC_DIR" --conditions=browser src/index.ts "$@"
fi

if [ -n "${ELIZA_OPENCODE_BIN:-}" ] && [ -x "$ELIZA_OPENCODE_BIN" ]; then
  exec "$ELIZA_OPENCODE_BIN" "$@"
fi

IFS=:
for DIR in $PATH; do
  [ -n "$DIR" ] || continue
  [ "$(cd "$DIR" 2>/dev/null && pwd)" = "$SHIM_DIR" ] && continue
  if [ -x "$DIR/opencode" ]; then
    exec "$DIR/opencode" "$@"
  fi
done
unset IFS

exec bunx opencode-ai "$@"
