#!/usr/bin/env bash
set -euo pipefail

find_real_opencode() {
  local d c
  for d in $(printf '%s' "$PATH" | tr ':' ' '); do
    for c in "$d/opencode" "$d/opencode.exe"; do
      if [ -x "$c" ] && [ "$c" != "$HOME/.local/bin/opencode" ]; then
        printf '%s\n' "$c"
        return 0
      fi
    done
  done
  return 1
}

REAL_OPENCODE="$(find_real_opencode || true)"
if [ -z "$REAL_OPENCODE" ]; then
  echo "Error: real opencode binary not found in PATH" >&2
  exit 1
fi

exec nono run --profile opencode-tinfoil --allow-cwd -- "$REAL_OPENCODE" "$@"
