#!/bin/bash
#
# Purpose : Describe this CoreDesktop machine as text, so two machines can be
#           compared with diff (see README.md). Runs in the smoketest VM.
#
# Only what CoreDesktop decides goes in: names, not versions, and no times, so
# a fresh install and an updated older install of the same commit match.

COREDESKTOP="$HOME/.local/share/coredesktop"

section() {
  printf '\n## %s\n' "$1"
}

section "Explicitly installed packages"
pacman -Qqe

section "Packages not from the repos (AUR)"
pacman -Qqm

section "Flatpaks"
flatpak list --app --columns=application | sort

section "pipx apps"
pipx list --short 2> /dev/null | cut -d' ' -f1 | sort

section "Enabled system units"
systemctl list-unit-files --state=enabled --no-legend | awk '{print $1}' | sort

section "Enabled user units"
systemctl --user list-unit-files --state=enabled --no-legend | awk '{print $1}' | sort

section "Groups"
id -nG | tr ' ' '\n' | sort

section "Firewall"
sudo ufw status verbose

section "pacman repos"
grep -E '^\[' /etc/pacman.conf

section "SSH server"
sudo sshd -T 2> /dev/null | sort

section "CoreDesktop's files in /etc"
sudo find /etc -path '*coredesktop*' -type f -print0 | sort -z | sudo xargs -0r sha256sum

section "Snapper"
if [ -f /etc/snapper/configs/root ]; then
  sudo snapper -c root get-config | sort
else
  echo "no root config"
fi

section "Migrations run"
ls ~/.local/state/coredesktop/migrations 2> /dev/null

section "Commands in ~/.local/bin"
for cmd in update refresh sibling version configure audit; do
  if cmp -s "$HOME/.local/bin/$cmd" "$COREDESKTOP/$cmd"; then
    echo "$cmd: current"
  else
    echo "$cmd: missing or out of date"
  fi
done

section "Shell"
grep -n coredesktop ~/.bashrc

section "GNOME extensions"
gsettings get org.gnome.shell enabled-extensions
ls ~/.local/share/gnome-shell/extensions 2> /dev/null

# The value of each key gnome-settings.dconf sets, which apply-gnome-settings.sh
# loads under /org/gnome/shell/extensions/
section "GNOME settings"
awk '/^\[/ { dir = substr($0, 2, length($0) - 2); next }
     /=/   { print dir "/" substr($0, 1, index($0, "=") - 1) }' \
  "$COREDESKTOP/gnome/gnome-settings.dconf" \
  | while read -r key; do
      echo "$key = $(dconf read "/org/gnome/shell/extensions/$key")"
    done
