#!/bin/bash
# Universal Turbo CLI Installation Script
# Automatically detects platform and installs the correct binary
#
# Download and run:
# curl -fsSL https://install-turbo.goldsky.com | bash
# or
# curl -fsSL https://install-turbo.goldsky.com/install | bash

set -e

# Detect platform and architecture
detect_platform() {
  case "$(uname -s)" in
    Darwin*)
      echo "macos"
      ;;
    Linux*)
      echo "linux"
      ;;
    MINGW*|MSYS*|CYGWIN*)
      echo "windows"
      ;;
    *)
      echo "unsupported"
      ;;
  esac
}

PLATFORM=$(detect_platform)

if [ "$PLATFORM" = "unsupported" ]; then
  echo "❌ Unsupported platform. This script supports macOS, Linux, and Windows (via PowerShell)."
  exit 1
fi

# For Windows, redirect to PowerShell script
if [ "$PLATFORM" = "windows" ]; then
  echo "📥 Detected Windows. Downloading PowerShell installer..."
  echo ""
  if command -v pwsh &> /dev/null || command -v powershell &> /dev/null; then
    if command -v pwsh &> /dev/null; then
      pwsh -Command "irm https://install-turbo.goldsky.com/scripts/install.ps1 | iex"
    else
      powershell -Command "irm https://install-turbo.goldsky.com/scripts/install.ps1 | iex"
    fi
  else
    echo "❌ PowerShell is required for Windows installation."
    echo "   Please run this command in PowerShell:"
    echo "   irm https://install-turbo.goldsky.com/scripts/install.ps1 | iex"
    exit 1
  fi
  exit 0
fi

# For macOS and Linux, proceed with installation
DOWNLOAD_DOMAIN="https://install-turbo.goldsky.com"
BINARY_NAME="turbo"
INSTALL_DIR="${INSTALL_DIR:-$HOME/.goldsky/bin}"

# Determine binary URL
if [ "$PLATFORM" = "macos" ]; then
  BINARY_URL="${DOWNLOAD_DOMAIN}/macos/${BINARY_NAME}-latest"
elif [ "$PLATFORM" = "linux" ]; then
  BINARY_URL="${DOWNLOAD_DOMAIN}/linux/${BINARY_NAME}-latest"
fi

echo "📥 Detected ${PLATFORM}. Downloading Turbo CLI..."
echo "   From: ${BINARY_URL}"

# Create temp directory
TMP_DIR=$(mktemp -d)
trap "rm -rf $TMP_DIR" EXIT

# Download binary
if ! curl -f -L -o "${TMP_DIR}/${BINARY_NAME}" "${BINARY_URL}"; then
  echo "❌ Failed to download binary. Please check your internet connection."
  exit 1
fi

# Make executable
chmod +x "${TMP_DIR}/${BINARY_NAME}"

# Remove macOS quarantine attribute if on macOS
if [ "$PLATFORM" = "macos" ]; then
  echo "🔓 Removing macOS quarantine attribute..."
  xattr -d com.apple.quarantine "${TMP_DIR}/${BINARY_NAME}" 2>/dev/null || true
fi

# Create install directory if it doesn't exist
mkdir -p "${INSTALL_DIR}"

# Install to target directory
echo "📦 Installing to ${INSTALL_DIR}..."
mv "${TMP_DIR}/${BINARY_NAME}" "${INSTALL_DIR}/${BINARY_NAME}"

# Add to PATH if not already present
add_to_path() {
  if echo "$PATH" | tr ':' '\n' | grep -qx "$INSTALL_DIR"; then
    return
  fi

  SHELL_NAME=$(basename "$SHELL")
  EXPORT_LINE="export PATH=\"${INSTALL_DIR}:\$PATH\""

  case "$SHELL_NAME" in
    zsh)
      PROFILE="$HOME/.zshrc"
      ;;
    bash)
      if [ -f "$HOME/.bash_profile" ]; then
        PROFILE="$HOME/.bash_profile"
      else
        PROFILE="$HOME/.bashrc"
      fi
      ;;
    fish)
      EXPORT_LINE="fish_add_path ${INSTALL_DIR}"
      PROFILE="$HOME/.config/fish/config.fish"
      ;;
    *)
      PROFILE="$HOME/.profile"
      ;;
  esac

  if [ -n "$PROFILE" ]; then
    if [ ! -f "$PROFILE" ] || ! grep -qF "$INSTALL_DIR" "$PROFILE"; then
      echo "" >> "$PROFILE"
      echo "# Added by Turbo CLI installer" >> "$PROFILE"
      echo "$EXPORT_LINE" >> "$PROFILE"
      echo "🔧 Added ${INSTALL_DIR} to PATH in ${PROFILE}"
    fi
  fi

  export PATH="${INSTALL_DIR}:$PATH"
}

add_to_path

# Verify installation
if command -v "${BINARY_NAME}" >/dev/null 2>&1; then
  INSTALLED_VERSION=$("${BINARY_NAME}" --version 2>/dev/null || echo "unknown")
  echo "✅ Turbo CLI installed successfully!"
  echo "   Location: $(command -v ${BINARY_NAME})"
  echo "   Version: ${INSTALLED_VERSION}"
  echo ""
  echo "Run '${BINARY_NAME} --help' to get started."
else
  echo "⚠️  Installation completed, but binary not found in PATH."
  echo "   You may need to restart your terminal or run:"
  echo "   export PATH=\"${INSTALL_DIR}:\$PATH\""
  exit 1
fi

