#!/usr/bin/bash
set -Eeuo pipefail

[[ $(id -u) -eq 0 ]] || exit 1
mountpoint -q /sys/fs/pstore || mount -t pstore pstore /sys/fs/pstore || exit 0
shopt -s nullglob
records=(/sys/fs/pstore/*)
(( ${#records[@]} > 0 )) || exit 0
stamp="$(date -u +%Y%m%dT%H%M%SZ)"
target="/var/log/nabu-suspend-diagnostics/${stamp}-pstore"
install -d -m0700 "$target"
cp -a -- "${records[@]}" "$target/"
sha256sum "$target"/* >"$target/SHA256SUMS"
journalctl -b -1 -k --no-pager >"$target/previous-kernel-journal.txt" 2>/dev/null || :
