#!/usr/bin/env bash

# Compatibility shim for ripgrep invocations coming from external agents/tools.
#
# Some providers use the non-existent `--fixed-string` flag. ripgrep uses
# `--fixed-strings` (plural). Translate and forward to the packaged `rg`.

set -euo pipefail

SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
SELF_RG="$SCRIPT_DIR/rg"
REAL_RG="$SCRIPT_DIR/../../tools/unpacked/rg"

args=()
for arg in "$@"; do
  if [[ "$arg" == "--fixed-string" ]]; then
    args+=("--fixed-strings")
  else
    args+=("$arg")
  fi
done

if ((${#args[@]})); then
  if [[ -x "$REAL_RG" ]]; then
    exec "$REAL_RG" "${args[@]}"
  fi
  IFS=':' read -r -a path_entries <<< "${PATH:-}"
  for entry in "${path_entries[@]}"; do
    [[ -z "$entry" ]] && continue
    candidate="$entry/rg"
    if [[ "$candidate" -ef "$SELF_RG" ]]; then
      continue
    fi
    if [[ -x "$candidate" ]]; then
      exec "$candidate" "${args[@]}"
    fi
  done
  echo "happier: rg shim could not find a usable ripgrep binary" >&2
  echo "Missing packaged ripgrep at: $REAL_RG" >&2
  echo "Fix: reinstall @happier-dev/cli (postinstall unpacks tools) or install ripgrep (rg) on PATH." >&2
  exit 127
fi
if [[ -x "$REAL_RG" ]]; then
  exec "$REAL_RG"
fi
IFS=':' read -r -a path_entries <<< "${PATH:-}"
for entry in "${path_entries[@]}"; do
  [[ -z "$entry" ]] && continue
  candidate="$entry/rg"
  if [[ "$candidate" -ef "$SELF_RG" ]]; then
    continue
  fi
  if [[ -x "$candidate" ]]; then
    exec "$candidate"
  fi
done
echo "happier: rg shim could not find a usable ripgrep binary" >&2
echo "Missing packaged ripgrep at: $REAL_RG" >&2
echo "Fix: reinstall @happier-dev/cli (postinstall unpacks tools) or install ripgrep (rg) on PATH." >&2
exit 127
