#!/bin/sh
set -eu

PATH="${PATH:-/usr/bin:/bin}:/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin"
export PATH

launcher_dir=$(CDPATH= cd "$(dirname "$0")" && pwd)
server_path=$launcher_dir/../mcp/server.mjs
if [ "${1:-}" = "--helper" ]; then
  server_path=$launcher_dir/../mcp/helpers.mjs
fi
launch_node() {
  node_command=$1
  shift
  if [ "${1:-}" = "--helper" ]; then
    shift
    # Preserve POSIX argv and HOME bytes without temporary files or consuming stdin.
    helper_stdin=-
    if (exec 4<&0) 2>&-; then
      exec 4<&0
      helper_stdin=4
    fi
    wait_helper() {
      if wait "$!" 2>&-; then exit 0; else exit $?; fi
    }
    forward_signal() {
      if [ -n "${!:-}" ]; then
        kill -s "$1" "$!" 2>&- || :
        wait_helper
      fi
      exit "$2"
    }
    trap 'forward_signal HUP 129' HUP
    trap 'forward_signal INT 130' INT
    trap 'forward_signal TERM 143' TERM
    printf '%s\000' "${HOME+x}" "${HOME-}" "$@" | od -An -v -tx1 | tr -d ' \n' |
      "$node_command" "$server_path" --helper 3<&0 0<&"$helper_stdin" 4<&- &
    exec 4<&-
    wait_helper
  fi
  exec "$node_command" "$server_path" "$@"
}

cache_root=${XDG_CACHE_HOME:-${HOME:-}/.cache}
codex_resources=
case "${CODEX_CLI_PATH:-}" in
  */*) codex_resources=${CODEX_CLI_PATH%/*} ;;
esac

# An explicitly selected runtime remains authoritative.
if [ -n "${CODEX_MCP_NODE_PATH:-}" ] && [ -x "$CODEX_MCP_NODE_PATH" ]; then
  launch_node "$CODEX_MCP_NODE_PATH" "$@"
fi

first_node=
can_load_native() {
  if [ -z "$first_node" ]; then
    first_node=$1
  fi
  # Some managed macOS runtimes enforce library validation and cannot load the
  # plugin's addon. Check before starting a server or helper with side effects.
  # Capture output without opening a writable file for read-only callers.
  _native_probe_output=$("$1" -e '
    if (process.platform === "darwin") {
      require("node:sqlite");
      require(require("node:path").join(process.argv[1], "native",
        `${process.platform}-${process.arch}`, "unix.node"));
    }
  ' "$launcher_dir/../mcp" </dev/null 2>&1)
}

for candidate in \
  "${CODEX_BROWSER_USE_NODE_PATH:-}" \
  "${CODEX_ELECTRON_RESOURCES_PATH:-}/cua_node/bin/node" \
  "$codex_resources/cua_node/bin/node" \
  "$cache_root/codex-runtimes/codex-primary-runtime/dependencies/node/bin/node"
do
  if [ -n "$candidate" ] && [ -x "$candidate" ] && can_load_native "$candidate"; then
    launch_node "$candidate" "$@"
  fi
done

remaining_path=$PATH
while :; do
  path_entry=${remaining_path%%:*}
  node_path=${path_entry:-.}/node
  if [ -x "$node_path" ] && can_load_native "$node_path"; then
    launch_node "$node_path" "$@"
  fi
  case "$remaining_path" in
    *:*) remaining_path=${remaining_path#*:} ;;
    *) break ;;
  esac
done

if [ -n "$first_node" ]; then
  # Helpers that do not use the addon can still run with the original default.
  launch_node "$first_node" "$@"
fi

printf '%s\n' 'Codex Security could not find a Node runtime. Reinstall or update Codex, or set CODEX_MCP_NODE_PATH to an executable Node runtime.' >&2
exit 127
