#!/usr/bin/env sh

# Super-simple 0x1 CLI entry point that fixes recursion issues
# This is the most reliable way to run the CLI with minimal dependencies

# Define common paths
HOME_DIR="$HOME"
BUN_PATH="$HOME_DIR/.bun/bin/bun"
NODE_MODULES="$HOME_DIR/.bun/install/global/node_modules/0x1"
CLI_ENTRY="$NODE_MODULES/dist/cli/index.js"

# Check if this was executed via bunx by looking at environment variables
if [ -n "$BUN_INSTALL" ] && [ "$BUNX_COMMAND" = "0x1" ]; then
  # We are being called by bunx - avoid recursion by directly finding the script
  if [ -f "$CLI_ENTRY" ]; then
    exec "$BUN_PATH" "$CLI_ENTRY" "$@"
  else
    # Can't find the script, look in the path where this bin script is located
    SELF_DIR="$(cd "$(dirname "$0")" && pwd)"
    SELF_CLI="$SELF_DIR/../dist/cli/index.js"
    if [ -f "$SELF_CLI" ]; then
      exec "$BUN_PATH" "$SELF_CLI" "$@"
    else
      echo "Error: Cannot find 0x1 CLI entry point" >&2
      exit 1
    fi
  fi
else
  # We are being called directly - try bunx with special env vars to prevent recursion
  export BUNX_COMMAND="0x1"
  exec bunx 0x1 "$@"
fi
