#!/usr/bin/env bash

# What this machine is, as JSON: {label, value} pairs in display order.
#
# GNOME's About panel answers "what am I running on" in one screen -- model,
# processor, memory, disk, OS, kernel, windowing system. Panama's About listed
# only its own component versions, which answers a different and much narrower
# question.
#
# Graphics is deliberately absent: GraphicsDevices already enumerates GPUs for
# the vitals readout, and naming them again here would be a second source of
# truth that could disagree with the first. The page joins the two.
#
# Anything unreadable is omitted rather than reported as "Unknown". These are
# facts about hardware, and a row saying "Processor: Unknown" is noise where
# simply not having the row is not.

set -uo pipefail

facts=()

emit() {
    [[ -n "${2:-}" ]] || return 0
    facts+=("$(jq -cn --arg label "$1" --arg value "$2" '{label: $label, value: $value}')")
}

# ── Machine ──────────────────────────────────────────────────────────────────
# DMI strings are frequently placeholders ("To Be Filled By O.E.M.", "Default
# string"). Those are worse than nothing, so they are filtered out.
dmi() {
    local value
    value="$(cat "/sys/class/dmi/id/$1" 2>/dev/null)" || return 0
    case "$value" in
        ""|"To Be Filled By O.E.M."*|"Default string"|"System Product Name"|"Unknown"|"None") return 0 ;;
    esac
    printf '%s' "$value"
}

vendor="$(dmi sys_vendor)"
product="$(dmi product_name)"
if [[ -n "$vendor" && -n "$product" ]]; then
    emit "Model" "$vendor $product"
else
    emit "Model" "${product:-$vendor}"
fi

emit "Hostname" "$(hostnamectl hostname 2>/dev/null || hostname 2>/dev/null)"

# ── Processor ────────────────────────────────────────────────────────────────
cpu="$(awk -F': ' '/^model name/ { print $2; exit }' /proc/cpuinfo 2>/dev/null)"
threads="$(nproc 2>/dev/null)"
if [[ -n "$cpu" ]]; then
    # The marketing name usually already says "6-Core", so the thread count is
    # the part that adds information.
    [[ -n "$threads" ]] && cpu="$cpu ($threads threads)"
    emit "Processor" "$cpu"
fi

# ── Memory ───────────────────────────────────────────────────────────────────
# Reported as the kernel sees it, which is a little under the sticker figure
# because firmware and integrated graphics reserve some before Linux starts.
emit "Memory" "$(awk '/^MemTotal:/ { printf "%.1f GiB", $2 / 1048576 }' /proc/meminfo 2>/dev/null)"

# ── Storage ──────────────────────────────────────────────────────────────────
read -r size used avail <<<"$(df -h --output=size,used,avail / 2>/dev/null | tail -1)"
[[ -n "${size:-}" ]] && emit "Disk" "$avail free of $size"

# ── Software ─────────────────────────────────────────────────────────────────
if [[ -r /etc/os-release ]]; then
    # Sourced in a subshell so the variables cannot leak into this script.
    os="$( . /etc/os-release 2>/dev/null && printf '%s' "${PRETTY_NAME:-$NAME}" )"
    emit "Operating system" "$os"
fi
emit "Kernel" "$(uname -r 2>/dev/null)"

case "${XDG_SESSION_TYPE:-}" in
    wayland) emit "Windowing system" "Wayland" ;;
    x11)     emit "Windowing system" "X11" ;;
esac

printf '[%s]\n' "$(IFS=,; printf '%s' "${facts[*]}")"
