#!/bin/bash
# -*- mode: sh; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: et sts=2 sw=2

#  SPDX-License-Identifier: LGPL-2.1+
#
#  Copyright © 2023-2024 Valve Corporation.
#
#  This file is part of holo.
##
## Collects useful information about the current state of the system, for
## reporting issues, debug, etc.
##
set -euo pipefail

declare -r HELPER=/usr/bin/steamos-polkit-helpers/steamos-systemreport-privileged

usage() {
    cat <<EOF
Usage: ${0##*/} [--steam-mode]

Print system information, useful for debug.

Options:
 --steam-mode            Skip collection of duplicate information
EOF
}

steam_mode=false

while [[ "$#" -ne 0 ]]
do
  case "$1" in
    -h|--help)
      usage
      exit
      ;;
    --steam-mode)
      steam_mode=true
      ;;
    *)
      usage
      exit 1
      ;;
  esac
  shift
done

# Run a given command, and handle any failure so that this script doesn't fail.
# This allows the script to continue running other commands to dump
# information. It prints a message if the given command fails.
# $1: string, command + args to run
function run {
  echo "---------------------------------------------------"
  echo "Command: $1"
  echo
  bash -c "$1 2>&1" || echo "Unable to run command!"
}

function super () {
  pkexec "$HELPER" $1
}

# [DEPRECATED] --steam-mode flag
if "$steam_mode"; then
  echo "-------------------------------------------------"
  echo "[DEPRECATED] --steam-mode was given as argument but it has no effect,"
  echo "             supported for compatibility only."
  echo
fi

# Check flag set by steamos-readonly when the rootfs has been mounted RW
echo "---------------------------------------------------"
echo -n "Root filesystem has been mounted RW: "
if [ -f /.ROOTFS_RW ]; then
  echo "YES"
  cat /.ROOTFS_RW
else
  echo "NO"
fi

# "|| :" because the command exits with status 1 if read-only is 'disabled'
run '(steamos-readonly status || :)'

super firmware

# show running processes, sorted by RSS since that seems like the most useful
# metric to sort by
run 'ps aux k-rss'

# Used to be run only when --steam-mode was not given, now as it is
# deprecated, always run.
run 'lspci -tv -nn'
run 'lsusb -t -v'
run 'coredumpctl -r --since -10d'
run 'journalctl --no-pager -b 0'
# NOTE: These journals are from _this_ OS slot.
# At present, both slots actually put their journals in a shared offload
# on the /home partition, so this works as a result - but if the offloads
# are ever split by partition set then this will become inaccurate:
run 'journalctl --no-pager -b -1'

# wake-up information
super wakeup-info

super partitions

# filter out the ANSI colour/bold/etc escape sequences
run 'rauc status' | sed -re 's/\x1b\[[0-9;]*[mG]//g'

super bootconfig

super storage-health

# WiFi connection info
run 'iwctl station wlan0 show | grep -E "State|Frequency|RSSI|Mode|MCS|Bitrate"'

# Network connectivity info
run 'ip addr'
run 'ping -4 -c4 1.1.1.1'
run 'ping -4 -c4 8.8.8.8'
run 'ping -6 -c4 2001:4860:4860::8888'
run 'ping -6 -c4 2606:4700:4700::1111'
run 'getent ahostsv4 test.steampowered.com'
run 'getent ahostsv6 test.steampowered.com'

# Audio info
run 'ls -lt /dev/snd/*'
run 'aplay -l'
run 'arecord -l'
run "find /proc/asound/card* -name 'eld*' -print -exec cat '{}' \;"
run "find ~/.config/{pipewire,wireplumber} -type f -print -exec echo '{}' \;"
run "find ~/.local/share/wireplumber -type f -print -exec echo '{}' \;"
run 'pw-dump Client | jq -c'
run 'pw-dump Device | jq -c'
run 'pw-dump Node | jq -c'
run 'pw-dump Port | jq -c'
run 'pw-dump Link | jq -c'
run 'wpctl status'
run 'wpctl settings'
run 'pactl list clients'
run 'pactl list cards'
run 'pactl list sinks'
run 'pactl list sources'
run 'pactl list sink-inputs'
run 'pactl list source-outputs'

# Battery / power info
run 'upower -d'

# Print out changed files in the /etc overlayfs.
# Note: NetworkManager profile files, which contain Wifi SSIDs, are exlcuded
run 'find /var/lib/overlays/etc/upper \
  -path /var/lib/overlays/etc/upper/NetworkManager/system-connections -prune \
  -o -print'

# Display EDID, as hex
while IFS= read -r conn; do
  run "cat $conn | hexdump -Cv | cut -d '|' -f 1"
done < <(find /sys/class/drm/card0/ -iname edid)

super dri-debugfs

run 'drm_info'

# Package check info
super pacman-check

# Fan Control Info
run 'pacman -Q jupiter-fan-control'
run 'systemctl status jupiter-fan-control'

echo 'jupiter-fan-control log, latest:'
run 'cat /var/log/jupiter-fan-control.log'

echo 'jupiter-fan-control log, previous:'
run 'cat /var/log/jupiter-fan-control.old.log'
