#!/bin/bash
# =============================================================================
# gnome.sh — GNOME Desktop Environment installer
# =============================================================================
# Part of the Dotfiles setup system for Arch Linux.
#
# GNOME is a mature, GTK4-based desktop environment that targets simplicity and
# a touch-friendly workflow.  On Arch it is shipped as the 'gnome' group, which
# pulls in GNOME Shell (the compositor/WM), Mutter, GDM (the display manager),
# and the core GNOME application set.
#
# This script installs GNOME plus the PipeWire audio stack, GNOME's XDG portal
# (required for Flatpak sandboxing and screen sharing under Wayland), and
# enables the essential system services.
#
# Prerequisites: run as a normal user with sudo access; pacman available;
# internet connection active.
# =============================================================================

# Exit on any error; fail on unset variables; propagate pipeline errors.
set -euo pipefail

# Load shared logging helpers (log, warn, err) from the setup library.
source "$(dirname "${BASH_SOURCE[0]}")/../lib/logging.sh"

log "Installing GNOME desktop..."

# Install GNOME and its ecosystem:
#
#   gnome                       — the official GNOME package group; includes
#                                 gnome-shell, mutter, gdm, gnome-control-center,
#                                 nautilus, and other core apps
#
#   gnome-tweaks                — exposes advanced settings not available in
#                                 GNOME Settings (fonts, window buttons, legacy
#                                 GTK theme overrides, extensions toggle)
#
#   xdg-desktop-portal-gnome    — GNOME's implementation of the XDG Desktop
#                                 Portal D-Bus API; required for Flatpak apps to
#                                 open file choosers, request screenshots, and
#                                 share screens under Wayland
#
#   pipewire                    — low-latency audio/video routing daemon
#   wireplumber                 — PipeWire session/policy manager
#   pipewire-alsa               — ALSA compatibility layer on top of PipeWire
#   pipewire-jack               — JACK API emulation for pro-audio software
#   pipewire-pulse              — PulseAudio socket emulation for legacy apps
#
#   networkmanager              — network connection management daemon (wired,
#                                 Wi-Fi, VPN, etc.)
#
#   flatpak                     — sandboxed app delivery; GNOME Software uses it
#                                 to install Flathub applications
sudo pacman -S --noconfirm --needed \
    gnome \
    gnome-tweaks \
    xdg-desktop-portal-gnome \
    pipewire wireplumber pipewire-alsa pipewire-jack pipewire-pulse \
    networkmanager \
    flatpak

log "Enabling services..."

# GDM (GNOME Display Manager) handles the login screen and session startup.
# It is bundled with the 'gnome' group and is the canonical choice for GNOME.
sudo systemctl enable gdm.service

# Start NetworkManager on boot so connections are available before login.
sudo systemctl enable NetworkManager.service

log "GNOME installation complete. Reboot to start."
