#!/bin/sh
# Claude-Flow Smart Dispatcher - Detects and uses the best available runtime
# Enhanced with NPX cache error handling and retry logic

VERSION="2.7.31"

# Determine the correct path based on how the script is invoked
if [ -L "$0" ]; then
  # Script is a symlink (npm global install)
  REAL_PATH=$(readlink -f "$0" 2>/dev/null || readlink "$0")
  SCRIPT_DIR=$(dirname "$REAL_PATH")
else
  # Script is executed directly
  SCRIPT_DIR=$(dirname "$0")
fi

# Handle global npm installation vs local execution
if [ -f "$SCRIPT_DIR/../src/cli/simple-cli.js" ]; then
  # Local development or properly structured installation
  ROOT_DIR=$(cd "$SCRIPT_DIR/.." && pwd)
else
  # Global npm installation - files might be in different location
  # Try to find the module root
  NODE_ROOT=$(npm root -g 2>/dev/null)
  if [ -n "$NODE_ROOT" ] && [ -f "$NODE_ROOT/claude-flow/src/cli/simple-cli.js" ]; then
    ROOT_DIR="$NODE_ROOT/claude-flow"
  else
    # Fallback to relative path
    ROOT_DIR=$(cd "$SCRIPT_DIR/.." && pwd)
  fi
fi

# Show help if no arguments provided
if [ $# -eq 0 ]; then
  set -- "--help"
fi

# Special handling for help command to ensure it passes through
if [ "$1" = "help" ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
  # Let the actual CLI handle help display
  :  # no-op, just pass through
fi

# Quick version check only - let the actual CLI handle help
for arg in "$@"; do
  if [ "$arg" = "--version" ] || [ "$arg" = "-v" ]; then
    echo "v$VERSION"
    exit 0
  fi
done

# Function to clean NPX cache on ENOTEMPTY errors
cleanup_npx_cache() {
  if [ -d "$HOME/.npm/_npx" ]; then
    echo "⚠️  Detected NPX cache issue, cleaning stale cache..." >&2
    # Only remove directories older than 1 hour to avoid disrupting concurrent operations
    find "$HOME/.npm/_npx" -type d -mmin +60 -exec rm -rf {} + 2>/dev/null || true
  fi
}

# Function to execute with retry logic for npx tsx
execute_with_retry() {
  local cmd="$1"
  local max_retries=3
  local retry_count=0
  local wait_time=2

  while [ $retry_count -lt $max_retries ]; do
    # Try to execute the command
    if eval "$cmd" 2>/tmp/claude-flow-error.log; then
      # Success - clean up error log
      rm -f /tmp/claude-flow-error.log 2>/dev/null
      return 0
    else
      # Check if error is ENOTEMPTY
      if grep -q "ENOTEMPTY" /tmp/claude-flow-error.log 2>/dev/null; then
        retry_count=$((retry_count + 1))

        if [ $retry_count -lt $max_retries ]; then
          echo "⚠️  NPM cache conflict detected (attempt $retry_count/$max_retries), retrying in ${wait_time}s..." >&2
          cleanup_npx_cache
          sleep $wait_time
          wait_time=$((wait_time * 2))  # Exponential backoff
        else
          echo "❌ Failed after $max_retries attempts. Please try:" >&2
          echo "   1. Clear NPX cache: rm -rf ~/.npm/_npx" >&2
          echo "   2. Use global installation: npm install -g claude-flow" >&2
          echo "   3. Report issue: https://github.com/ruvnet/claude-flow/issues/856" >&2
          cat /tmp/claude-flow-error.log >&2
          rm -f /tmp/claude-flow-error.log 2>/dev/null
          return 1
        fi
      else
        # Different error - don't retry
        cat /tmp/claude-flow-error.log >&2
        rm -f /tmp/claude-flow-error.log 2>/dev/null
        return 1
      fi
    fi
  done
}

# Use Node.js with TypeScript support and WASM modules enabled
if [ -f "$ROOT_DIR/src/cli/simple-cli.js" ]; then
  # Use node for JavaScript version with WASM support (no retry needed)
  exec node --experimental-wasm-modules "$ROOT_DIR/src/cli/simple-cli.js" "$@"
elif command -v tsx >/dev/null 2>&1 && [ -f "$ROOT_DIR/src/cli/simple-cli.ts" ]; then
  # Use tsx for TypeScript functionality with WASM support (no retry needed)
  exec tsx --experimental-wasm-modules "$ROOT_DIR/src/cli/simple-cli.ts" "$@"
elif [ -f "$ROOT_DIR/src/cli/simple-cli.ts" ]; then
  # Try to use npx tsx as fallback with WASM support (with retry logic)
  # Use --prefer-offline to reduce cache conflicts and --yes to avoid prompts
  execute_with_retry "npx --yes --prefer-offline tsx --experimental-wasm-modules '$ROOT_DIR/src/cli/simple-cli.ts' \"\$@\""
  exit $?
else
  # No runtime available, show help
  echo "🧠 Claude-Flow v$VERSION - Advanced AI Agent Orchestration System"
  echo ""
  echo "⚠️  No compatible runtime found."
  echo ""
  echo "To install and run:"
  echo "  1. Install tsx: npm install -g tsx"
  echo "  2. Run: claude-flow <command>"
  echo ""
  echo "Or use npx directly:"
  echo "  npx tsx src/cli/simple-cli.ts <command>"
  echo ""
  echo "Documentation: https://github.com/ruvnet/claude-code-flow"
  exit 1
fi
