#!/bin/bash
# Universal Streamling 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:-/usr/local/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 Streamling 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

# Install to target directory
if [ ! -w "$INSTALL_DIR" ]; then
  echo "📦 Installing to ${INSTALL_DIR} (requires sudo)..."
  sudo mv "${TMP_DIR}/${BINARY_NAME}" "${INSTALL_DIR}/${BINARY_NAME}"
else
  echo "📦 Installing to ${INSTALL_DIR}..."
  mv "${TMP_DIR}/${BINARY_NAME}" "${INSTALL_DIR}/${BINARY_NAME}"
fi

# Verify installation
if command -v "${BINARY_NAME}" >/dev/null 2>&1; then
  INSTALLED_VERSION=$("${BINARY_NAME}" --version 2>/dev/null || echo "unknown")
  echo "✅ Streamling 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 "   Please ensure ${INSTALL_DIR} is in your PATH."
  exit 1
fi

