#!/bin/bash
# =============================================================================
# cosmic.sh — COSMIC Desktop Environment installer
# =============================================================================
# Part of the Dotfiles setup system for Arch Linux.
#
# COSMIC is a Wayland-native desktop environment developed by System76 in Rust.
# Unlike traditional DEs that layer on top of existing WM infrastructure, COSMIC
# ships its own compositor (cosmic-comp), shell, and settings daemon as a
# tightly integrated suite.
#
# This script installs the full COSMIC suite via pacman, sets up the PipeWire
# audio stack, configures networking and Bluetooth, and enables the appropriate
# display manager (cosmic-greeter preferred, SDDM as fallback).
#
# Prerequisites: run as a normal user with sudo access; pacman must be available;
# the system should already be connected to the internet.
# =============================================================================

# Exit immediately on error; treat unset variables as errors; propagate pipe
# failures so that a failing command early in a pipeline does not go unnoticed.
set -euo pipefail

# Pull in the shared logging helpers (log, warn, err) from the setup library.
# BASH_SOURCE[0] resolves to this file's path even when the script is sourced
# from another directory, making the relative path to lib/ reliable.
source "$(dirname "${BASH_SOURCE[0]}")/../lib/logging.sh"

log "Installing COSMIC desktop..."

# Install the COSMIC meta-package plus supporting infrastructure:
#
#   cosmic              — the COSMIC desktop meta-package (pulls in cosmic-comp,
#                         cosmic-panel, cosmic-settings, cosmic-app-library, etc.)
#
#   pipewire            — modern audio/video routing daemon; replaces PulseAudio
#                         and JACK for most desktop workloads
#   wireplumber         — session/policy manager for PipeWire; decides which
#                         streams get routed where
#   pipewire-alsa       — ALSA compatibility shim so legacy ALSA apps use PipeWire
#   pipewire-jack       — JACK compatibility shim for professional audio software
#   pipewire-pulse      — PulseAudio socket emulation; keeps apps that speak
#                         libpulse working without changes
#
#   networkmanager      — connection management daemon; handles wired, Wi-Fi,
#                         VPN, and mobile broadband with a D-Bus API
#
#   bluez               — the Linux Bluetooth protocol stack
#   bluez-utils         — command-line tools (bluetoothctl) for managing devices
#
#   flatpak             — universal app sandboxing/delivery; lets COSMIC Software
#                         install Flathub apps without distro packaging
sudo pacman -S --noconfirm --needed \
    cosmic \
    pipewire wireplumber pipewire-alsa pipewire-jack pipewire-pulse \
    networkmanager \
    bluez bluez-utils \
    flatpak

log "Enabling services..."

# COSMIC ships its own display manager (cosmic-greeter) built with iced.
# Prefer it over SDDM for a cohesive look-and-feel; fall back to SDDM when
# the package is not present (e.g. on systems where cosmic-greeter is still
# in the AUR or not yet stabilised on this arch build).
#
# pacman -Qi queries the local package database and exits non-zero when the
# package is not installed; &>/dev/null silences both stdout and stderr so
# the check is invisible to the user.
# cosmic-greeter is COSMIC's own display manager; fall back to sddm if absent
if pacman -Qi cosmic-greeter &>/dev/null; then
    sudo systemctl enable cosmic-greeter.service
else
    # SDDM (Simple Desktop Display Manager) is the widely used Qt-based DM;
    # it supports both X11 and Wayland sessions and integrates well with COSMIC.
    sudo pacman -S --noconfirm --needed sddm
    sudo systemctl enable sddm.service
fi

# Enable NetworkManager so the network is managed on every boot.
sudo systemctl enable NetworkManager.service

# Enable the Bluetooth daemon so adapters are powered up on boot and devices
# can reconnect automatically.
sudo systemctl enable bluetooth.service

log "COSMIC installation complete. Reboot to start."
