#!/bin/bash
# Prints diagnostic info related to Regolith for troubleshooting purposes

set -Eeu -o pipefail

print_file() {
  if [ -f "$1" ]; then
    echo "--- $1:"
    cat "$1"
  else
    echo "--- [$1 does not exist]"
  fi
}

print_settings() {
  gsettings list-recursively "$1" 2>/dev/null || echo "--- [schema $1 is not installed]"
}

# Which window manager to describe: the one of the session this runs in. From
# a text console or ssh there is none, so describe each installed Regolith
# session, and i3 (as before) when neither session file is found.
X11_SESSION=/usr/share/xsessions/regolith-x11.desktop
WAYLAND_SESSION=/usr/share/wayland-sessions/regolith-wayland.desktop
report_i3=false
report_sway=false
case "${XDG_SESSION_TYPE-}" in
  wayland) report_sway=true ;;
  x11) report_i3=true ;;
  *)
    if [ -f "$X11_SESSION" ]; then report_i3=true; fi
    if [ -f "$WAYLAND_SESSION" ]; then report_sway=true; fi
    if ! $report_i3 && ! $report_sway; then report_i3=true; fi
    ;;
esac

echo "--- Regolith Diagnostic - $(date)"
print_file /etc/regolith/version
echo "XDG_SESSION_TYPE=${XDG_SESSION_TYPE-}"
echo "DESKTOP_SESSION=${DESKTOP_SESSION-}"
installed=""
for session in "$X11_SESSION" "$WAYLAND_SESSION"; do
  if [ -f "$session" ]; then installed="$installed $(basename "$session" .desktop)"; fi
done
echo "Installed sessions:${installed:- none}"

echo "--- System Info "
uname -a

print_file /etc/lsb-release

echo "--- Xresources"
print_file "$HOME/.Xresources"
print_file "$HOME/.config/regolith3/Xresources"
print_file "$HOME/.cache/regolith3/Xresources-generated"

if $report_sway; then
  trawldb -q || true
  echo # trawldb does not end its output with a newline
fi
if $report_i3; then
  xrdb -query || true
fi

if $report_sway; then
  echo "--- sway"
  sway --version || true
  print_file "/etc/regolith/sway/config"
  print_file "$HOME/.config/regolith3/sway/config"
  ls -l /usr/share/regolith/sway/config.d/ || true
fi
if $report_i3; then
  echo "--- i3"
  i3 --moreversion || true
  print_file "/etc/regolith/i3/config"
  print_file "$HOME/.config/regolith3/i3/config"
  ls -l /usr/share/regolith/i3/config.d/ || true
fi

if [ -d "/usr/share/i3xrocks" ]; then
  echo "--- i3xrocks config"
  find /usr/share/i3xrocks
else
  echo "--- [/usr/share/i3xrocks does not exist]"
fi

echo "--- dconf"
print_settings org.gnome.desktop.interface
print_settings org.gnome.desktop.background
if $report_i3; then
  print_settings org.gnome.gnome-flashback
fi
print_settings org.regolith-linux.ilia
print_settings org.gnome.desktop.wm.keybindings
print_settings org.gnome.settings-daemon.plugins.media-keys
print_settings org.gnome.desktop.wm.preferences

