#!/usr/bin/env bash
set -euo pipefail

# ==============================================================================
# Google Antigravity Suite — Universal Linux Installer & Package Manager
# Installs Antigravity IDE, Antigravity Hub, Antigravity CLI ('agy'),
# and native desktop integrations (MIME schemes, GNOME Nautilus extension).
# Maintained by X3M Industries (https://github.com/x3m-industries/antigravity-packages)
# ==============================================================================

BOLD="\033[1m"
GREEN="\033[1;32m"
BLUE="\033[1;34m"
CYAN="\033[1;36m"
YELLOW="\033[1;33m"
RED="\033[1;31m"
DIM="\033[2m"
RESET="\033[0m"

info() {
    echo -e "${BLUE}==>${RESET} ${BOLD}$*${RESET}"
}

success() {
    echo -e "${GREEN}✓${RESET} ${BOLD}$*${RESET}"
}

warn() {
    echo -e "${YELLOW}Warning:${RESET} $*"
}

error() {
    echo -e "${RED}Error:${RESET} $*" >&2
    exit 1
}

show_banner() {
    echo -e "${CYAN}"
    cat << 'BANNER_EOF'
    ___    _   ___________ ________  ___ _    _____________  __
   /   |  / | / /_  __/  _/ ____/ __ \/   | |  / /  _/_  __/\ \/ /
  / /| | /  |/ / / /  / // / __/ /_/ / /| | | / // /  / /    \  / 
 / ___ |/ /|  / / / _/ // /_/ / _, _/ ___ | |/ // /  / /     / /  
/_/  |_/_/ |_/ /_/ /___/\____/_/ |_/_/  |_|___/___/ /_/     /_/   
BANNER_EOF
    echo -e "${RESET}${BOLD}Google Antigravity Linux Universal Installer & Package Manager${RESET}"
    echo -e "Repository: https://x3m-industries.github.io/antigravity-packages/\n"
}

show_help() {
    echo -e "${BOLD}Usage:${RESET} install.sh [options]"
    echo ""
    echo -e "${BOLD}Installation Modes:${RESET}"
    echo "  -y, --yes, --non-interactive  Run silently without interactive prompts (installs defaults)"
    echo "  --all                         Install all components (IDE, Hub, CLI, desktop integrations)"
    echo "  --cli-only                    Install only the Antigravity CLI ('agy') (no sudo required)"
    echo "  --ide-only                    Install only the Antigravity IDE (code editor)"
    echo "  --hub-only                    Install only the Antigravity Hub (agent platform)"
    echo "  --no-cli                      Skip Antigravity CLI installation"
    echo "  --no-ide                      Skip Antigravity IDE installation"
    echo "  --no-hub                      Skip Antigravity Hub installation"
    echo "  --no-desktop-integrations     Remove the file manager integrations shipped by the IDE package (Nautilus, Dolphin, Nemo, Caja)"
    echo "  --no-nautilus                 Alias for --no-desktop-integrations"
    echo "  --dry-run                     Show what would be installed and exit without making changes"
    echo ""
    echo -e "${BOLD}Inspection & Maintenance:${RESET}"
    echo "  --status                      Show installed components, versions, and repository health"
    echo "  --print-downloads             Print official Google tarballs & package download URLs"
    echo "  --uninstall                   Cleanly remove helper-configured repositories & packages"
    echo "  -h, --help                    Display this help menu"
    echo ""
    echo -e "${BOLD}Examples:${RESET}"
    echo "  curl -fsSL https://x3m-industries.github.io/antigravity-packages/install.sh | bash"
    echo "  curl -fsSL https://x3m-industries.github.io/antigravity-packages/install.sh | bash -s -- -y"
    echo "  curl -fsSL https://x3m-industries.github.io/antigravity-packages/install.sh | bash -s -- --status"
    echo "  curl -fsSL https://x3m-industries.github.io/antigravity-packages/install.sh | bash -s -- --uninstall"
    echo ""
}

# 1. Architecture Check
ARCH="$(uname -m)"
case "${ARCH}" in
    x86_64|amd64|aarch64|arm64)
        ;;
    *)
        error "Unsupported system architecture: ${ARCH}. Google Antigravity packages require x86_64 or aarch64 / arm64."
        ;;
esac

# 2. Prerequisite Check (curl)
ensure_curl() {
    if ! command -v curl > /dev/null 2>&1; then
        if [ "$(id -u)" -eq 0 ]; then
            info "'curl' is not installed. Installing curl..."
            if command -v apt-get > /dev/null 2>&1; then
                apt-get update -qq && apt-get install -y --no-install-recommends curl ca-certificates
            elif command -v dnf > /dev/null 2>&1; then
                dnf install -y curl ca-certificates
            elif command -v yum > /dev/null 2>&1; then
                yum install -y curl ca-certificates
            elif command -v zypper > /dev/null 2>&1; then
                zypper --non-interactive install -y curl ca-certificates
            elif command -v pacman > /dev/null 2>&1; then
                pacman -S --needed --noconfirm curl ca-certificates
            else
                error "'curl' is required but could not be installed automatically. Please install curl."
            fi
        elif command -v sudo > /dev/null 2>&1; then
            info "'curl' is not installed. Attempting to install via sudo..."
            if command -v apt-get > /dev/null 2>&1; then
                sudo apt-get update -qq && sudo apt-get install -y --no-install-recommends curl ca-certificates
            elif command -v dnf > /dev/null 2>&1; then
                sudo dnf install -y curl ca-certificates
            elif command -v yum > /dev/null 2>&1; then
                sudo yum install -y curl ca-certificates
            elif command -v zypper > /dev/null 2>&1; then
                sudo zypper --non-interactive install -y curl ca-certificates
            elif command -v pacman > /dev/null 2>&1; then
                sudo pacman -S --needed --noconfirm curl ca-certificates
            else
                error "'curl' is required but not installed. Please install curl and re-run."
            fi
        else
            error "'curl' is required but not installed. Please install curl (e.g. 'sudo apt install curl' or 'sudo dnf install curl')."
        fi
    fi
}

# 3. Target User & Home Directory Resolution
if [ -n "${SUDO_USER:-}" ] && [ "${SUDO_USER}" != "root" ]; then
    TARGET_USER="${SUDO_USER}"
    TARGET_HOME=$(getent passwd "${TARGET_USER}" 2>/dev/null | cut -d: -f6 || true)
    if [ -z "${TARGET_HOME}" ] || [ "${TARGET_HOME}" = "/" ]; then
        if [ -d "/home/${TARGET_USER}" ]; then
            TARGET_HOME="/home/${TARGET_USER}"
        else
            TARGET_HOME="${HOME:-/root}"
        fi
    fi
else
    TARGET_USER="${USER:-$(id -un 2>/dev/null || echo "user")}"
    TARGET_HOME="${HOME:-/root}"
fi

# Component selection defaults
INSTALL_IDE=true
INSTALL_HUB=true
INSTALL_CLI=true
INSTALL_NAUTILUS=true
EXPLICIT_COMPONENT_FLAG=false
EXPLICIT_NO_NAUTILUS=false
INTERACTIVE=true
DRY_RUN=false
DO_STATUS=false
DO_UNINSTALL=false
DO_PRINT_DOWNLOADS=false

reset_components_once() {
    if [ "${EXPLICIT_COMPONENT_FLAG}" = false ]; then
        INSTALL_IDE=false
        INSTALL_HUB=false
        INSTALL_CLI=false
        INSTALL_NAUTILUS=false
        EXPLICIT_COMPONENT_FLAG=true
    fi
}

# Parse Command-Line Arguments
while [ "$#" -gt 0 ]; do
    case "$1" in
        --status)
            DO_STATUS=true
            INTERACTIVE=false
            ;;
        --uninstall)
            DO_UNINSTALL=true
            ;;
        --print-downloads)
            DO_PRINT_DOWNLOADS=true
            INTERACTIVE=false
            ;;
        --dry-run)
            INTERACTIVE=false
            DRY_RUN=true
            ;;
        -y|--yes|--non-interactive)
            INTERACTIVE=false
            ;;
        --all)
            INTERACTIVE=false
            EXPLICIT_COMPONENT_FLAG=true
            INSTALL_IDE=true
            INSTALL_HUB=true
            INSTALL_CLI=true
            [ "${EXPLICIT_NO_NAUTILUS}" = false ] && INSTALL_NAUTILUS=true
            ;;
        --cli-only)
            INTERACTIVE=false
            EXPLICIT_COMPONENT_FLAG=true
            INSTALL_IDE=false
            INSTALL_HUB=false
            INSTALL_CLI=true
            INSTALL_NAUTILUS=false
            ;;
        --ide-only)
            INTERACTIVE=false
            EXPLICIT_COMPONENT_FLAG=true
            INSTALL_IDE=true
            INSTALL_HUB=false
            INSTALL_CLI=false
            ;;
        --hub-only)
            INTERACTIVE=false
            EXPLICIT_COMPONENT_FLAG=true
            INSTALL_IDE=false
            INSTALL_HUB=true
            INSTALL_CLI=false
            INSTALL_NAUTILUS=false
            ;;
        --no-cli)
            INTERACTIVE=false
            INSTALL_CLI=false
            ;;
        --no-ide)
            INTERACTIVE=false
            INSTALL_IDE=false
            INSTALL_NAUTILUS=false
            ;;
        --no-hub)
            INTERACTIVE=false
            INSTALL_HUB=false
            ;;
        --no-nautilus|--no-desktop-integrations)
            EXPLICIT_NO_NAUTILUS=true
            INSTALL_NAUTILUS=false
            ;;
        --nautilus|--desktop-integrations)
            EXPLICIT_NO_NAUTILUS=false
            INSTALL_NAUTILUS=true
            ;;
        --cli)
            INTERACTIVE=false
            reset_components_once
            INSTALL_CLI=true
            ;;
        --ide)
            INTERACTIVE=false
            reset_components_once
            INSTALL_IDE=true
            [ "${EXPLICIT_NO_NAUTILUS}" = false ] && INSTALL_NAUTILUS=true
            ;;
        --hub)
            INTERACTIVE=false
            reset_components_once
            INSTALL_HUB=true
            ;;
        -h|--help)
            show_banner
            show_help
            exit 0
            ;;
        *)
            error "Unknown argument: $1. Run 'install.sh --help' for usage."
            ;;
    esac
    shift
done

# Check non-interactive environment variables
if [ -n "${CI:-}" ] || [ -n "${NONINTERACTIVE:-}" ] || [ "${DEBIAN_FRONTEND:-}" = "noninteractive" ]; then
    INTERACTIVE=false
fi

# Detect installed versions for components
INSTALLED_IDE_VER=""
if command -v antigravity-ide > /dev/null 2>&1 || [ -x /usr/bin/antigravity-ide ]; then
    if command -v rpm > /dev/null 2>&1 && rpm -q antigravity-ide > /dev/null 2>&1; then
        INSTALLED_IDE_VER="v$(rpm -q --qf "%{VERSION}" antigravity-ide 2>/dev/null || true)"
    elif command -v dpkg-query > /dev/null 2>&1 && dpkg-query -W -f='${Version}' antigravity-ide > /dev/null 2>&1; then
        INSTALLED_IDE_VER="v$(dpkg-query -W -f='${Version}' antigravity-ide 2>/dev/null || true)"
    elif command -v pacman > /dev/null 2>&1 && pacman -Q antigravity-ide-bin > /dev/null 2>&1; then
        INSTALLED_IDE_VER="v$(pacman -Q antigravity-ide-bin 2>/dev/null | awk '{print $2}' || true)"
    elif command -v pacman > /dev/null 2>&1 && pacman -Q antigravity-ide > /dev/null 2>&1; then
        INSTALLED_IDE_VER="v$(pacman -Q antigravity-ide 2>/dev/null | awk '{print $2}' || true)"
    else
        INSTALLED_IDE_VER="installed"
    fi
fi

INSTALLED_HUB_VER=""
if command -v antigravity > /dev/null 2>&1 || [ -x /usr/bin/antigravity ]; then
    if command -v rpm > /dev/null 2>&1 && rpm -q antigravity > /dev/null 2>&1; then
        INSTALLED_HUB_VER="v$(rpm -q --qf "%{VERSION}" antigravity 2>/dev/null || true)"
    elif command -v dpkg-query > /dev/null 2>&1 && dpkg-query -W -f='${Version}' antigravity > /dev/null 2>&1; then
        INSTALLED_HUB_VER="v$(dpkg-query -W -f='${Version}' antigravity 2>/dev/null || true)"
    elif command -v pacman > /dev/null 2>&1 && pacman -Q antigravity-bin > /dev/null 2>&1; then
        INSTALLED_HUB_VER="v$(pacman -Q antigravity-bin 2>/dev/null | awk '{print $2}' || true)"
    elif command -v pacman > /dev/null 2>&1 && pacman -Q antigravity > /dev/null 2>&1; then
        INSTALLED_HUB_VER="v$(pacman -Q antigravity 2>/dev/null | awk '{print $2}' || true)"
    else
        INSTALLED_HUB_VER="installed"
    fi
fi

EXISTING_CLI_PATH=""
INSTALLED_CLI_VER=""
if [ -x "${TARGET_HOME}/.local/bin/agy" ]; then
    EXISTING_CLI_PATH="${TARGET_HOME}/.local/bin/agy"
    INSTALLED_CLI_VER=$("${EXISTING_CLI_PATH}" --version 2>/dev/null || true)
elif command -v agy > /dev/null 2>&1; then
    EXISTING_CLI_PATH="$(command -v agy)"
    INSTALLED_CLI_VER=$("${EXISTING_CLI_PATH}" --version 2>/dev/null || true)
fi

# Detect Linux Distribution
DISTRO_ID="unknown"
DISTRO_LIKE=""
PRETTY_NAME="Linux"
if [ -f /etc/os-release ]; then
    # shellcheck source=/dev/null
    . /etc/os-release
    DISTRO_ID="${ID:-unknown}"
    DISTRO_LIKE="${ID_LIKE:-}"
fi

# Sudo / Root Privilege Check
SUDO=""
check_sudo() {
    if [ "$(id -u)" -ne 0 ]; then
        if command -v sudo > /dev/null 2>&1; then
            SUDO="sudo"
        else
            error "Root privileges are required for this action. Please run as root or install sudo."
        fi
    fi
}

# ------------------------------------------------------------------------------
# Action: --status (Show installed components, versions, and repository health)
# ------------------------------------------------------------------------------
show_status() {
    show_banner
    echo -e "${BOLD}System Environment:${RESET}"
    echo -e "  • OS:             ${PRETTY_NAME} (${ARCH})"
    echo -e "  • User:           ${TARGET_USER} (${TARGET_HOME})"
    echo ""

    echo -e "${BOLD}Installed Applications:${RESET}"
    if [ -n "${INSTALLED_IDE_VER}" ]; then
        echo -e "  • ${GREEN}Antigravity IDE:${RESET}       Installed (${BOLD}${INSTALLED_IDE_VER}${RESET}) -> $(command -v antigravity-ide 2>/dev/null || echo '/usr/bin/antigravity-ide')"
    else
        echo -e "  • ${DIM}Antigravity IDE:${RESET}       Not installed"
    fi

    if [ -n "${INSTALLED_HUB_VER}" ]; then
        echo -e "  • ${GREEN}Antigravity Hub:${RESET}       Installed (${BOLD}${INSTALLED_HUB_VER}${RESET}) -> $(command -v antigravity 2>/dev/null || echo '/usr/bin/antigravity')"
    else
        echo -e "  • ${DIM}Antigravity Hub:${RESET}       Not installed"
    fi

    if [ -n "${EXISTING_CLI_PATH}" ]; then
        echo -e "  • ${GREEN}Antigravity CLI ('agy'):${RESET} Installed (${BOLD}${INSTALLED_CLI_VER:-active}${RESET}) -> ${EXISTING_CLI_PATH}"
    else
        echo -e "  • ${DIM}Antigravity CLI ('agy'):${RESET} Not installed"
    fi
    echo ""

    echo -e "${BOLD}Repository Configuration:${RESET}"
    if [ -f /etc/yum.repos.d/antigravity.repo ]; then
        echo -e "  • ${GREEN}RPM / DNF Repo:${RESET}        /etc/yum.repos.d/antigravity.repo ${GREEN}[Configured]${RESET}"
    elif [ -f /etc/apt/sources.list.d/antigravity.sources ] || [ -f /etc/apt/sources.list.d/antigravity.list ]; then
        echo -e "  • ${GREEN}DEB / APT Sources:${RESET}     /etc/apt/sources.list.d/antigravity.sources ${GREEN}[Configured]${RESET}"
        if [ -f /etc/apt/keyrings/antigravity.gpg ]; then
            echo -e "  • ${GREEN}APT GPG Keyring:${RESET}       /etc/apt/keyrings/antigravity.gpg ${GREEN}[Verified]${RESET}"
        fi
    elif command -v zypper > /dev/null 2>&1 && zypper repos antigravity > /dev/null 2>&1; then
        echo -e "  • ${GREEN}Zypper Repo:${RESET}           antigravity ${GREEN}[Configured]${RESET}"
    else
        echo -e "  • ${YELLOW}Package Repositories:${RESET}  No native repository configured yet."
    fi
    echo ""

    echo -e "${BOLD}Short CLI Shortcuts:${RESET}"
    if [ -x /usr/bin/agy-ide ]; then
        echo -e "  • ${GREEN}agy-ide:${RESET}              /usr/bin/agy-ide -> antigravity-ide ${GREEN}[Active]${RESET}"
    fi
    if [ -x /usr/bin/agy-hub ]; then
        echo -e "  • ${GREEN}agy-hub:${RESET}              /usr/bin/agy-hub -> antigravity ${GREEN}[Active]${RESET}"
    fi
    if [ -x "${TARGET_HOME}/.local/bin/agy" ] || command -v agy > /dev/null 2>&1; then
        echo -e "  • ${GREEN}agy (CLI Agent):${RESET}      $(command -v agy 2>/dev/null || echo "${TARGET_HOME}/.local/bin/agy") ${GREEN}[Active]${RESET}"
    fi
    echo ""

    echo -e "${BOLD}Desktop & File Manager Integrations:${RESET}"
    [ -f /usr/share/applications/antigravity-ide.desktop ] && echo -e "  • ${GREEN}IDE Launcher:${RESET}         /usr/share/applications/antigravity-ide.desktop"
    [ -f /usr/share/applications/antigravity-ide-url-handler.desktop ] && echo -e "  • ${GREEN}IDE URL Handler:${RESET}      /usr/share/applications/antigravity-ide-url-handler.desktop"
    [ -f /usr/share/applications/antigravity.desktop ] && echo -e "  • ${GREEN}Hub Desktop Entry:${RESET}    /usr/share/applications/antigravity.desktop"
    if [ -f /usr/share/nautilus-python/extensions/open-in-antigravity-ide.py ]; then
        echo -e "  • ${GREEN}GNOME Files (Nautilus):${RESET} /usr/share/nautilus-python/extensions/open-in-antigravity-ide.py ${GREEN}[Active]${RESET}"
    fi
    if [ -f /usr/share/kio/servicemenus/open-in-antigravity-ide.desktop ]; then
        echo -e "  • ${GREEN}KDE Dolphin Service:${RESET}  /usr/share/kio/servicemenus/open-in-antigravity-ide.desktop ${GREEN}[Active]${RESET}"
    fi
    if [ -f /usr/share/nemo/actions/open-in-antigravity-ide.nemo_action ]; then
        echo -e "  • ${GREEN}Linux Mint (Nemo):${RESET}     /usr/share/nemo/actions/open-in-antigravity-ide.nemo_action ${GREEN}[Active]${RESET}"
    fi
    if [ -f /usr/share/caja-python/extensions/open-in-antigravity-ide.py ]; then
        echo -e "  • ${GREEN}MATE (Caja):${RESET}           /usr/share/caja-python/extensions/open-in-antigravity-ide.py ${GREEN}[Active]${RESET}"
    fi
    if [ -n "${XDG_CURRENT_DESKTOP:-}" ] && echo "${XDG_CURRENT_DESKTOP}" | grep -qi "cosmic"; then
        echo -e "  • ${GREEN}COSMIC Desktop:${RESET}       cosmic-files & cosmic-launcher MIME integration ${GREEN}[Active]${RESET}"
    fi
    if [ -f /usr/share/bash-completion/completions/antigravity-ide ]; then
        echo -e "  • ${GREEN}Shell Completions:${RESET}    Bash & Zsh ${GREEN}[Active]${RESET}"
    fi
    echo ""

    # Legacy / Manual installation conflicts (e.g. from manual /opt extractions)
    local has_legacy=false
    for d in /opt/Antigravity* /opt/antigravity*; do
        if [ -d "${d}" ]; then
            [ "${has_legacy}" = false ] && echo -e "${YELLOW}${BOLD}Legacy / Manual Installation Conflicts Detected:${RESET}"
            echo -e "  • ${YELLOW}Warning:${RESET} Found manual extraction directory ${d} (causes broken auto-updates & SUID sandbox crashes)"
            has_legacy=true
        fi
    done
    if [ -L /usr/local/bin/antigravity ] || [ -L /usr/local/bin/antigravity-ide ] || [ -f "${TARGET_HOME}/.local/share/applications/antigravity.desktop" ]; then
        for b in antigravity antigravity-ide; do
            if [ -L "/usr/local/bin/${b}" ]; then
                local link_tgt
                link_tgt=$(readlink -f "/usr/local/bin/${b}" 2>/dev/null || true)
                if echo "${link_tgt}" | grep -q "/opt/"; then
                    [ "${has_legacy}" = false ] && echo -e "${YELLOW}${BOLD}Legacy / Manual Installation Conflicts Detected:${RESET}"
                    echo -e "  • ${YELLOW}Warning:${RESET} /usr/local/bin/${b} shadows /usr/bin/${b} in \$PATH! (points to ${link_tgt})"
                    has_legacy=true
                fi
            fi
        done
        for d in antigravity.desktop antigravity-ide.desktop; do
            local user_d="${TARGET_HOME}/.local/share/applications/${d}"
            if [ -f "${user_d}" ] && grep -q "/opt/" "${user_d}" 2>/dev/null; then
                [ "${has_legacy}" = false ] && echo -e "${YELLOW}${BOLD}Legacy / Manual Installation Conflicts Detected:${RESET}"
                echo -e "  • ${YELLOW}Warning:${RESET} ${user_d} shadows system desktop menu! (points to /opt/)"
                has_legacy=true
            fi
        done
    fi
    if [ "${has_legacy}" = true ]; then
        echo -e "  ${DIM}Run the installer with '--uninstall' or remove manually to eliminate conflicting launchers & directories.${RESET}\n"
    fi
}

# ------------------------------------------------------------------------------
# Action: --print-downloads (Print official Google tarballs & package URLs)
# ------------------------------------------------------------------------------
print_downloads() {
    show_banner
    echo -e "${BOLD}Google Antigravity Upstream & Distribution Assets:${RESET}\n"
    echo -e "  ${BOLD}Architecture Detected:${RESET} ${ARCH}\n"

    echo -e "  ${BOLD}1. Official Google Antigravity Hub (Agent Platform):${RESET}"
    echo -e "     • x86_64:   ${CYAN}https://storage.googleapis.com/antigravity-public/antigravity-hub/2.23.1-6348059836284928/linux-x64/Antigravity.tar.gz${RESET}"
    echo -e "     • aarch64:  ${CYAN}https://storage.googleapis.com/antigravity-public/antigravity-hub/2.23.1-6348059836284928/linux-arm/Antigravity.tar.gz${RESET}"
    echo ""

    echo -e "  ${BOLD}2. Official Google Antigravity IDE (AI Code Editor):${RESET}"
    echo -e "     • Web:      ${CYAN}https://antigravity.google/download${RESET}"
    echo ""

    echo -e "  ${BOLD}3. Official Google Antigravity CLI ('agy'):${RESET}"
    echo -e "     • Script:   ${CYAN}https://antigravity.google/cli/install.sh${RESET}"
    echo ""

    echo -e "  ${BOLD}4. X3M Industries Native Linux Packages & Repositories:${RESET}"
    echo -e "     • Releases: ${CYAN}https://github.com/x3m-industries/antigravity-packages/releases/latest${RESET}"
    echo -e "     • DNF Repo: ${CYAN}https://x3m-industries.github.io/antigravity-packages/rpm/${RESET}"
    echo -e "     • APT Repo: ${CYAN}https://apt.x3m.industries/deb/${RESET}"
    echo -e "     • PKGBUILD: ${CYAN}https://github.com/x3m-industries/antigravity-packages/tree/main/pkgbuild${RESET}"
    echo -e "     • LLM Docs: ${CYAN}https://x3m-industries.github.io/antigravity-packages/llms.txt${RESET}"
    echo ""
}

# ------------------------------------------------------------------------------
# Action: --uninstall (Cleanly remove helper-configured repos, packages & files)
# ------------------------------------------------------------------------------
do_uninstall() {
    show_banner
    check_sudo

    if [ "${INTERACTIVE}" = true ]; then
        if (exec 3</dev/tty) 2>/dev/null || [ -t 0 ]; then
            echo -e "${YELLOW}${BOLD}Are you sure you want to uninstall Google Antigravity Linux components?${RESET}"
            echo -e "This will remove native RPM/DEB packages, repository configs, GPG keys, and desktop integrations."
            echo -e "User settings and code projects in home directories will remain completely untouched.\n"
            confirm=""
            if (exec 3</dev/tty) 2>/dev/null; then
                printf "Proceed with uninstallation? [y/N]: "
                read -r confirm < /dev/tty || true
            else
                read -r -p "Proceed with uninstallation? [y/N]: " confirm || true
            fi
            case "${confirm}" in
                y|Y|yes|YES) ;;
                *) echo "Uninstallation aborted."; exit 0 ;;
            esac
        fi
    fi

    info "Removing Google Antigravity packages..."
    if command -v dnf > /dev/null 2>&1; then
        ${SUDO} dnf remove -y antigravity antigravity-ide 2>/dev/null || true
    elif command -v yum > /dev/null 2>&1; then
        ${SUDO} yum remove -y antigravity antigravity-ide 2>/dev/null || true
    elif command -v apt-get > /dev/null 2>&1; then
        ${SUDO} apt-get remove -y antigravity antigravity-ide 2>/dev/null || true
    elif command -v zypper > /dev/null 2>&1; then
        ${SUDO} zypper --non-interactive remove -y antigravity antigravity-ide 2>/dev/null || true
    elif command -v pacman > /dev/null 2>&1; then
        ${SUDO} pacman -Rns --noconfirm antigravity antigravity-ide antigravity-bin antigravity-ide-bin 2>/dev/null || true
    fi

    info "Removing repository configurations and GPG keyrings..."
    ${SUDO} rm -f /etc/yum.repos.d/antigravity.repo
    ${SUDO} rm -f /etc/apt/sources.list.d/antigravity.sources
    ${SUDO} rm -f /etc/apt/sources.list.d/antigravity.list
    ${SUDO} rm -f /etc/apt/keyrings/antigravity.gpg
    if command -v zypper > /dev/null 2>&1 && zypper repos antigravity > /dev/null 2>&1; then
        ${SUDO} zypper --non-interactive removerepo antigravity 2>/dev/null || true
    fi

    info "Removing multi-desktop integrations and shortcuts..."
    ${SUDO} rm -f /usr/share/nautilus-python/extensions/open-in-antigravity-ide.py
    ${SUDO} rm -f /usr/share/caja-python/extensions/open-in-antigravity-ide.py
    ${SUDO} rm -f /usr/share/kio/servicemenus/open-in-antigravity-ide.desktop
    ${SUDO} rm -f /usr/share/kservices5/ServiceMenus/open-in-antigravity-ide.desktop
    ${SUDO} rm -f /usr/share/nemo/actions/open-in-antigravity-ide.nemo_action
    ${SUDO} rm -f /usr/share/applications/antigravity-ide-url-handler.desktop
    ${SUDO} rm -f /usr/bin/agy-ide
    ${SUDO} rm -f /usr/bin/agy-hub
    ${SUDO} rm -f /usr/share/bash-completion/completions/agy-ide
    ${SUDO} rm -f /usr/share/zsh/site-functions/_agy-ide

    if [ -x "${TARGET_HOME}/.local/bin/agy" ]; then
        info "Removing Antigravity CLI ('agy') at ${TARGET_HOME}/.local/bin/agy..."
        rm -f "${TARGET_HOME}/.local/bin/agy"
    fi

    # Clean legacy manual /opt extractions and obsolete /usr/local/bin symlinks
    for b in antigravity antigravity-ide; do
        if [ -L "/usr/local/bin/${b}" ]; then
            ${SUDO} rm -f "/usr/local/bin/${b}" 2>/dev/null || rm -f "/usr/local/bin/${b}" 2>/dev/null || true
        fi
    done
    for d in /opt/Antigravity* /opt/antigravity*; do
        if [ -d "${d}" ]; then
            info "Removing legacy manual extraction directory at ${d}..."
            ${SUDO} rm -rf "${d}" 2>/dev/null || true
        fi
    done
    for d in antigravity.desktop antigravity-ide.desktop; do
        rm -f "${TARGET_HOME}/.local/share/applications/${d}.bak" 2>/dev/null || true
        if [ -f "${TARGET_HOME}/.local/share/applications/${d}" ] && grep -q "/opt/" "${TARGET_HOME}/.local/share/applications/${d}" 2>/dev/null; then
            info "Removing legacy user desktop launcher at ${TARGET_HOME}/.local/share/applications/${d}..."
            rm -f "${TARGET_HOME}/.local/share/applications/${d}" 2>/dev/null || true
        fi
    done

    # Refresh desktop & icon caches
    if command -v update-desktop-database > /dev/null 2>&1; then
        ${SUDO} update-desktop-database /usr/share/applications >/dev/null 2>&1 || true
    fi
    if command -v gtk-update-icon-cache > /dev/null 2>&1; then
        ${SUDO} gtk-update-icon-cache -f /usr/share/icons/hicolor >/dev/null 2>&1 || true
    fi

    echo ""
    success "Google Antigravity packages, repositories, and desktop integrations have been cleanly removed."
    info "User configuration files in ${TARGET_HOME}/.config and project files were left intact."
    exit 0
}

# Check explicit action flags
if [ "${DO_STATUS}" = true ]; then
    show_status
    exit 0
fi

if [ "${DO_PRINT_DOWNLOADS}" = true ]; then
    print_downloads
    exit 0
fi

if [ "${DO_UNINSTALL}" = true ]; then
    do_uninstall
    exit 0
fi

show_banner

# 4. Interactive Selection (if TTY is available and interactive mode is not disabled)
if [ "${INTERACTIVE}" = true ]; then
    if ! (exec 3</dev/tty) 2>/dev/null && [ ! -t 0 ]; then
        info "Non-interactive environment detected. Proceeding with default components (all)..."
    else
        echo -e "${BOLD}Select components to install / update:${RESET}\n"

        status_ide="${DIM}[Not installed]${RESET}"
        [ -n "${INSTALLED_IDE_VER}" ] && status_ide="${GREEN}[Installed: ${INSTALLED_IDE_VER}]${RESET}"

        status_hub="${DIM}[Not installed]${RESET}"
        [ -n "${INSTALLED_HUB_VER}" ] && status_hub="${GREEN}[Installed: ${INSTALLED_HUB_VER}]${RESET}"

        status_cli="${DIM}[Not installed]${RESET}"
        [ -n "${INSTALLED_CLI_VER}" ] && status_cli="${GREEN}[Installed: ${INSTALLED_CLI_VER}]${RESET}"

        echo -e "  ${BOLD}[1] Antigravity IDE${RESET}        AI-first standalone code editor (RPM/DEB)     ${status_ide}"
        echo -e "  ${BOLD}[2] Antigravity Hub${RESET}        Antigravity 2.0 desktop agent hub (RPM/DEB)   ${status_hub}"
        echo -e "  ${BOLD}[3] Antigravity CLI (agy)${RESET}  Terminal agent with auto-updating             ${status_cli}"
        echo ""

        user_choice=""
        if (exec 3</dev/tty) 2>/dev/null; then
            printf "Enter selection [Default: 1, 2, 3 (All)] or press ENTER: "
            read -r user_choice < /dev/tty || true
        else
            read -r -p "Enter selection [Default: 1, 2, 3 (All)] or press ENTER: " user_choice || true
        fi
        echo ""

        choice_clean=$(echo "${user_choice}" | tr ',' ' ' | tr -d '\r')
        if [ -n "${choice_clean// /}" ]; then
            INSTALL_IDE=false
            INSTALL_HUB=false
            INSTALL_CLI=false

            for item in ${choice_clean}; do
                case "${item}" in
                    1|ide)
                        INSTALL_IDE=true
                        ;;
                    2|hub)
                        INSTALL_HUB=true
                        ;;
                    3|cli|agy)
                        INSTALL_CLI=true
                        ;;
                    all|a)
                        INSTALL_IDE=true
                        INSTALL_HUB=true
                        INSTALL_CLI=true
                        ;;
                    q|quit|exit|none)
                        echo "Installation aborted by user."
                        exit 0
                        ;;
                    *)
                        warn "Unknown component selection: '${item}' (ignored)"
                        ;;
                esac
            done
        fi
    fi
fi

# Ensure at least one component was selected
if [ "${INSTALL_IDE}" = false ] && [ "${INSTALL_HUB}" = false ] && [ "${INSTALL_CLI}" = false ]; then
    warn "No components were selected for installation. Exiting."
    exit 0
fi

info "Installation plan:"
[ "${INSTALL_IDE}" = true ] && echo -e "  • ${CYAN}Antigravity IDE${RESET} (Native package & desktop launcher)"
[ "${INSTALL_HUB}" = true ] && echo -e "  • ${CYAN}Antigravity Hub${RESET} (Antigravity 2.0 agent platform)"
[ "${INSTALL_CLI}" = true ] && echo -e "  • ${CYAN}Antigravity CLI ('agy')${RESET} (Terminal agent with auto-updater)"
[ "${INSTALL_IDE}" = true ] && [ "${INSTALL_NAUTILUS}" = true ] && echo -e "  • ${CYAN}GNOME Nautilus Integration${RESET} ('Open in Antigravity IDE' context menu)"
echo ""

if [ "${DRY_RUN}" = true ]; then
    info "Dry run complete. No changes were made."
    exit 0
fi

ensure_curl

# Sudo check for system package installation
case "${DISTRO_ID}" in
    arch|manjaro|endeavouros)
        # Arch installs CLI into user-space; desktop GUI packages use debtap / AUR
        ;;
    *)
        if echo "${DISTRO_LIKE}" | grep -qE "arch"; then
            : # Arch derivative
        elif [ "${INSTALL_IDE}" = true ] || [ "${INSTALL_HUB}" = true ]; then
            check_sudo
            if [ "$(id -u)" -ne 0 ]; then
                info "Requesting sudo privileges to configure package repositories..."
            fi
        fi
        ;;
esac

# 5. Package Installation Functions
fix_chrome_sandbox() {
    local target="$1"
    if [ -f "${target}" ]; then
        ${SUDO} chown root:root "${target}" 2>/dev/null || true
        ${SUDO} chmod 4755 "${target}" 2>/dev/null || true
    fi
}

clean_legacy_conflicts() {
    # 1. Obsolete symlinks in /usr/local/bin pointing to /opt
    for b in antigravity antigravity-ide; do
        if [ -L "/usr/local/bin/${b}" ]; then
            local target
            target=$(readlink -f "/usr/local/bin/${b}" 2>/dev/null || true)
            if echo "${target}" | grep -q "/opt/"; then
                warn "Detected legacy manual symlink /usr/local/bin/${b} -> ${target}"
                info "Removing obsolete symlink so /usr/bin/${b} takes precedence in \$PATH..."
                ${SUDO} rm -f "/usr/local/bin/${b}"
            fi
        fi
    done

    # 2. Obsolete user desktop entries shadowing system package launchers
    for d in antigravity.desktop antigravity-ide.desktop; do
        local user_d="${TARGET_HOME}/.local/share/applications/${d}"
        if [ -f "${user_d}" ] && grep -q "/opt/" "${user_d}" 2>/dev/null; then
            warn "Detected legacy user desktop file ${user_d} pointing to /opt/."
            info "Renaming to ${user_d}.bak to activate native package desktop entry..."
            mv -f "${user_d}" "${user_d}.bak" 2>/dev/null || ${SUDO} mv -f "${user_d}" "${user_d}.bak" || true
        fi
    done

    # 3. Legacy or one-line APT repository configuration (superseded by DEB822 antigravity.sources)
    local old_list="/etc/apt/sources.list.d/antigravity.list"
    if [ -f "${old_list}" ]; then
        if ! grep -qE "x3m-industries|apt\.x3m\.industries" "${old_list}" 2>/dev/null; then
            warn "Detected legacy Google APT configuration at ${old_list}."
        fi
        info "Removing legacy one-line APT list (${old_list}) in favor of DEB822 antigravity.sources..."
        ${SUDO} rm -f "${old_list}"
    fi

    # 4. Legacy manual extraction directories in /opt
    for d in /opt/Antigravity* /opt/antigravity*; do
        if [ -d "${d}" ]; then
            warn "Detected legacy manual directory at ${d} (manual tarball extraction)."
            info "Note: Native packages install cleanly into /usr/share/ without root permission conflicts."
        fi
    done
}

install_rpm() {
    info "Configuring DNF/YUM repository..."
    ${SUDO} curl -fsSL https://x3m-industries.github.io/antigravity-packages/rpm/antigravity.repo -o /etc/yum.repos.d/antigravity.repo
    success "Added /etc/yum.repos.d/antigravity.repo"

    local pkgs=()
    [ "${INSTALL_IDE}" = true ] && pkgs+=("antigravity-ide")
    [ "${INSTALL_HUB}" = true ] && pkgs+=("antigravity")

    if [ ${#pkgs[@]} -gt 0 ]; then
        info "Installing ${pkgs[*]}..."
        if command -v dnf > /dev/null 2>&1; then
            ${SUDO} dnf install -y "${pkgs[@]}"
        elif command -v yum > /dev/null 2>&1; then
            ${SUDO} yum install -y "${pkgs[@]}"
        fi
        fix_chrome_sandbox "/usr/share/antigravity-ide/chrome-sandbox"
        fix_chrome_sandbox "/usr/share/antigravity/chrome-sandbox"
    fi
}

install_deb() {
    info "Configuring APT repository..."
    ${SUDO} mkdir -p /etc/apt/keyrings
    ${SUDO} curl -fsSL https://apt.x3m.industries/deb/antigravity.gpg -o /etc/apt/keyrings/antigravity.gpg
    ${SUDO} curl -fsSL https://apt.x3m.industries/deb/antigravity.sources -o /etc/apt/sources.list.d/antigravity.sources
    ${SUDO} rm -f /etc/apt/sources.list.d/antigravity.list
    success "Added /etc/apt/keyrings/antigravity.gpg"
    success "Added /etc/apt/sources.list.d/antigravity.sources"

    local pkgs=()
    [ "${INSTALL_IDE}" = true ] && pkgs+=("antigravity-ide")
    [ "${INSTALL_HUB}" = true ] && pkgs+=("antigravity")

    if [ ${#pkgs[@]} -gt 0 ]; then
        info "Updating package lists..."
        ${SUDO} apt-get update

        info "Installing ${pkgs[*]}..."
        ${SUDO} apt-get install -y "${pkgs[@]}"
        fix_chrome_sandbox "/usr/share/antigravity-ide/chrome-sandbox"
        fix_chrome_sandbox "/usr/share/antigravity/chrome-sandbox"
    fi
}

install_zypper() {
    info "Configuring Zypper repository for openSUSE..."
    if ${SUDO} zypper repos antigravity > /dev/null 2>&1; then
        ${SUDO} zypper --non-interactive modifyrepo --enable --refresh antigravity
    else
        ${SUDO} zypper --non-interactive addrepo -f https://x3m-industries.github.io/antigravity-packages/rpm/ antigravity
    fi
    ${SUDO} rpm --import https://x3m-industries.github.io/antigravity-packages/rpm/RPM-GPG-KEY-antigravity
    success "Configured zypper repository 'antigravity'"

    local pkgs=()
    [ "${INSTALL_IDE}" = true ] && pkgs+=("antigravity-ide")
    [ "${INSTALL_HUB}" = true ] && pkgs+=("antigravity")

    if [ ${#pkgs[@]} -gt 0 ]; then
        info "Installing ${pkgs[*]}..."
        ${SUDO} zypper --non-interactive install -y "${pkgs[@]}"
        fix_chrome_sandbox "/usr/share/antigravity-ide/chrome-sandbox"
        fix_chrome_sandbox "/usr/share/antigravity/chrome-sandbox"
    fi
}

install_desktop_integrations() {
    # 1. Short CLI symlinks (normally shipped by the packages; recreate only if missing)
    if [ -x /usr/bin/antigravity-ide ] && [ ! -L /usr/bin/agy-ide ]; then
        ${SUDO} ln -sf /usr/bin/antigravity-ide /usr/bin/agy-ide
        success "Created short CLI shortcut 'agy-ide' -> /usr/bin/antigravity-ide"
    fi
    if [ -x /usr/bin/antigravity ] && [ ! -L /usr/bin/agy-hub ]; then
        ${SUDO} ln -sf /usr/bin/antigravity /usr/bin/agy-hub
        success "Created short CLI shortcut 'agy-hub' -> /usr/bin/antigravity"
    fi

    [ "${INSTALL_IDE}" = true ] || return 0

    # 2. File manager integrations (GNOME Files / Nautilus, MATE Caja, KDE Dolphin, Linux Mint Nemo)
    #    and shell completions are shipped *inside* the antigravity-ide package, so they are owned by
    #    the package manager and updated with it. This function only handles opt-out and helper packages.
    if [ "${INSTALL_NAUTILUS}" = false ]; then
        info "Skipping file manager integrations (--no-desktop-integrations)..."
        ${SUDO} rm -f /usr/share/nautilus-python/extensions/open-in-antigravity-ide.py
        ${SUDO} rm -f /usr/share/caja-python/extensions/open-in-antigravity-ide.py
        ${SUDO} rm -f /usr/share/kio/servicemenus/open-in-antigravity-ide.desktop
        ${SUDO} rm -f /usr/share/kservices5/ServiceMenus/open-in-antigravity-ide.desktop
        ${SUDO} rm -f /usr/share/nemo/actions/open-in-antigravity-ide.nemo_action
    elif command -v nautilus > /dev/null 2>&1 || [ -d /usr/share/nautilus-python ] || echo "${XDG_CURRENT_DESKTOP:-}" | grep -qi "gnome"; then
        # The Nautilus extension needs the Python bindings (a weak dependency of the package)
        info "Configuring GNOME Files / Nautilus right-click context menu..."
        if command -v apt-get > /dev/null 2>&1 && ! dpkg -s python3-nautilus > /dev/null 2>&1; then
            info "Installing python3-nautilus package for file manager extension..."
            ${SUDO} apt-get install -y --no-install-recommends python3-nautilus 2>/dev/null || true
        fi
        if command -v dnf > /dev/null 2>&1 && ! rpm -q nautilus-python > /dev/null 2>&1; then
            info "Installing nautilus-python package for file manager extension..."
            ${SUDO} dnf install -y nautilus-python 2>/dev/null || true
        fi
        if command -v zypper > /dev/null 2>&1 && ! rpm -q python3-nautilus > /dev/null 2>&1; then
            ${SUDO} zypper --non-interactive install -y python3-nautilus 2>/dev/null || true
        fi
        if command -v pacman > /dev/null 2>&1 && ! pacman -Qi python-nautilus > /dev/null 2>&1; then
            ${SUDO} pacman -S --noconfirm python-nautilus 2>/dev/null || true
        fi
    fi

    # 3. COSMIC Desktop Detection
    if [ -n "${XDG_CURRENT_DESKTOP:-}" ] && echo "${XDG_CURRENT_DESKTOP}" | grep -qi "cosmic"; then
        info "COSMIC Desktop detected: Applications and MIME associations are active in cosmic-launcher & cosmic-files."
    fi

    # Refresh desktop database
    if command -v update-desktop-database > /dev/null 2>&1; then
        ${SUDO} update-desktop-database /usr/share/applications >/dev/null 2>&1 || true
    fi
}

install_cli() {
    info "Checking Antigravity CLI ('agy')..."

    if [ -n "${EXISTING_CLI_PATH}" ]; then
        success "Antigravity CLI is already installed at ${EXISTING_CLI_PATH} (${INSTALLED_CLI_VER:-installed})"
        info "Skipping download (Antigravity CLI automatically self-updates in the background during normal usage)"
        return 0
    fi

    info "Installing Antigravity CLI ('agy') via official Google installer into ${TARGET_HOME}/.local/bin/agy..."
    if [ "$(id -u)" -eq 0 ] && [ -n "${SUDO_USER:-}" ] && [ "${SUDO_USER}" != "root" ]; then
        if command -v su > /dev/null 2>&1; then
            su - "${TARGET_USER}" -c "curl -fsSL https://antigravity.google/cli/install.sh | bash"
        elif command -v runuser > /dev/null 2>&1; then
            runuser -u "${TARGET_USER}" -- env HOME="${TARGET_HOME}" USER="${TARGET_USER}" LOGNAME="${TARGET_USER}" bash -c "curl -fsSL https://antigravity.google/cli/install.sh | bash"
        else
            error "Neither 'su' nor 'runuser' is available to switch to user '${TARGET_USER}'."
        fi
    else
        curl -fsSL https://antigravity.google/cli/install.sh | bash
    fi
    success "Antigravity CLI installed successfully"
}

# 6. Execute System Package Installations (IDE / Hub)
IS_ARCH=false
if [ "${INSTALL_IDE}" = true ] || [ "${INSTALL_HUB}" = true ]; then
    clean_legacy_conflicts
    case "${DISTRO_ID}" in
        fedora|rhel|centos|rocky|almalinux|amzn|nobara)
            install_rpm
            ;;
        ubuntu|debian|linuxmint|pop|elementary|zorin|kali|raspbian)
            install_deb
            ;;
        opensuse*|sles)
            install_zypper
            ;;
        arch|manjaro|endeavouros)
            IS_ARCH=true
            echo ""
            info "Arch Linux / Pacman environment detected (${DISTRO_ID})."
            echo -e "  • ${BOLD}Antigravity CLI ('agy'):${RESET} Installing seamlessly into ${TARGET_HOME}/.local/bin/agy (no root needed)."
            echo -e "  • ${BOLD}Desktop GUI Packages:${RESET} Install via ${CYAN}makepkg -si${RESET} (PKGBUILD) or ${CYAN}debtap${RESET}:"
            echo -e "    ${CYAN}debtap antigravity-ide_2.5.5-4923483625488384.3_amd64.deb && sudo pacman -U antigravity-ide-*.pkg.tar.zst${RESET}"
            echo -e "    ${CYAN}https://github.com/x3m-industries/antigravity-packages/tree/main/pkgbuild${RESET}\n"
            ;;
        *)
            if echo "${DISTRO_LIKE}" | grep -qE "fedora|rhel|centos"; then
                install_rpm
            elif echo "${DISTRO_LIKE}" | grep -qE "debian|ubuntu"; then
                install_deb
            elif echo "${DISTRO_LIKE}" | grep -qE "suse"; then
                install_zypper
            elif echo "${DISTRO_LIKE}" | grep -qE "arch"; then
                IS_ARCH=true
                echo ""
                info "Arch-based distribution detected (${DISTRO_ID})."
                echo -e "  • Desktop GUI packages can be installed via PKGBUILD (makepkg -si) or debtap."
                echo -e "  • Proceeding with Antigravity CLI ('agy') in ~/.local/bin/agy...\n"
            else
                error "Unsupported distribution: '${DISTRO_ID}'. You can download raw RPM or DEB packages from https://github.com/x3m-industries/antigravity-packages/releases"
            fi
            ;;
    esac

    # Install multi-desktop integrations & shortcuts
    if [ "${IS_ARCH}" != true ]; then
        install_desktop_integrations
    fi
fi

# 7. Execute CLI Installation (User-space)
if [ "${INSTALL_CLI}" = true ]; then
    install_cli
fi

# 8. Post-Install Summary & Guidance
echo ""
success "Installation completed successfully!"
if [ "${IS_ARCH}" = true ]; then
    echo -e "\n${BOLD}Arch Linux Quick Start:${RESET}"
    [ "${INSTALL_CLI}" = true ] && echo -e "  • Launch Antigravity CLI:     ${CYAN}agy${RESET}"
    echo -e "  • Desktop GUI with debtap:    ${CYAN}curl -fsSLO https://github.com/x3m-industries/antigravity-packages/releases/latest/download/antigravity-ide_2.5.5-4923483625488384.3_amd64.deb && debtap antigravity-ide_2.5.5-4923483625488384.3_amd64.deb && sudo pacman -U antigravity-ide-*.pkg.tar.zst${RESET}"
    echo -e "  • AUR PKGBUILD templates:     ${CYAN}https://github.com/x3m-industries/antigravity-packages/tree/main/pkgbuild${RESET}"
else
    echo -e "\n${BOLD}Quick Start (Commands & Shortcuts):${RESET}"
    [ "${INSTALL_IDE}" = true ] && echo -e "  • Launch Antigravity IDE:     ${CYAN}agy-ide ./my-project${RESET} (or ${CYAN}antigravity-ide${RESET})"
    [ "${INSTALL_HUB}" = true ] && echo -e "  • Launch Antigravity Hub:     ${CYAN}agy-hub${RESET} (or ${CYAN}antigravity${RESET})"
    if [ "${INSTALL_CLI}" = true ]; then
        echo -e "  • Launch Antigravity CLI:     ${CYAN}agy${RESET}"
        if [[ ":${PATH}:" != *":${TARGET_HOME}/.local/bin:"* ]]; then
            echo -e "\n${YELLOW}Note:${RESET} Ensure ${BOLD}${TARGET_HOME}/.local/bin${RESET} is in your shell PATH. If 'agy' is not found, add to your shell profile:"
            echo -e "  ${CYAN}export PATH=\"\$HOME/.local/bin:\$PATH\"${RESET}"
        fi
    fi
    if [ "${INSTALL_IDE}" = true ]; then
        echo -e "\n${BOLD}Multi-Desktop File Manager Integrations:${RESET}"
        echo -e "  • GNOME Files / Nautilus:     Restart Nautilus ('nautilus -q') to see the right-click menu."
        echo -e "  • KDE Plasma / Dolphin:       Right-click any folder/file -> 'Open in Antigravity IDE'."
        echo -e "  • Linux Mint / Nemo:          Right-click any folder/file -> 'Open in Antigravity IDE'."
        echo -e "  • MATE / Caja:                Right-click any folder/file -> 'Open in Antigravity IDE'."
        echo -e "  • COSMIC Desktop:             MIME & 'Open With' active in cosmic-files and cosmic-launcher."
    fi
fi
echo -e "\n${BOLD}Maintenance & Inspection:${RESET}"
echo -e "  • Check Status:               ${CYAN}curl -fsSL https://x3m-industries.github.io/antigravity-packages/install.sh | bash -s -- --status${RESET}"
echo -e "  • System Updates:             Run ${CYAN}sudo dnf update${RESET} or ${CYAN}sudo apt update && sudo apt upgrade${RESET}"
echo -e "\n${YELLOW}⭐ If this saved you time, please star the project on GitHub:${RESET}"
echo -e "  ${BOLD}https://github.com/x3m-industries/antigravity-packages${RESET}\n"
