Files
coredesktop/updates.d/95-cleanup.sh
T
davidandClaude Opus 5.5 54f9e12526
shellcheck / shellcheck (push) Failing after 17s
Log each install and update, and keep only the recent logs
Each run's output now goes to ~/.local/state/coredesktop/logs/, so what
failed can be read after the terminal is gone. In a terminal, update runs
itself under script(1), so everything it runs still sees a terminal, then
strips color codes and progress redraws from the log; otherwise it uses
tee. install.sh, usually piped into bash, uses tee, and runs configure
straight on the terminal, outside the log. Both name the log when
something fails. 95-cleanup.sh keeps the newest 10 update logs and 3
install logs.

The sudo keepalive loops no longer share the logged output, so their
sleep can't hold the log open after the run ends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 11:00:31 -07:00

54 lines
2.1 KiB
Bash
Executable File

#!/bin/bash
echo "Removing unused Flatpak runtimes..."
sudo flatpak uninstall --system --unused -y --noninteractive
flatpak uninstall --user --unused -y --noninteractive
if command -v paccache &>/dev/null; then
echo "Trimming pacman package cache..."
sudo paccache -rk2
sudo paccache -ruk0
fi
# Keep the newest 10 update logs and 3 install logs (refresh runs install.sh
# too). Their names sort oldest first.
keep_newest() {
local keep="$1"
shift
(( $# > keep )) && rm -f -- "${@:1:$# - keep}"
return 0
}
LOGS="$HOME/.local/state/coredesktop/logs"
shopt -s nullglob
keep_newest 10 "$LOGS"/update-*.log
keep_newest 3 "$LOGS"/install-*.log
shopt -u nullglob
# makepkg builds pkg-debug alongside an AUR package, and removing the package
# leaves it behind (asdf-vm-debug). Without its package it's no use to anyone,
# so remove it without asking. The exact name: pacman -Q herdr finds herdr-bin
mapfile -t STRAY_DEBUG < <(pacman -Qqm 2> /dev/null | grep -- '-debug$' | while read -r pkg; do
[ "$(pacman -Qq "${pkg%-debug}" 2> /dev/null)" = "${pkg%-debug}" ] || echo "$pkg"
done)
if (( ${#STRAY_DEBUG[@]} > 0 )); then
echo "Removing debug symbols for packages that are gone: ${STRAY_DEBUG[*]}"
sudo pacman -R --noconfirm "${STRAY_DEBUG[@]}" || exit 1
fi
# Offer to remove packages that were installed as dependencies and are no
# longer needed (packages something lists as an optional dependency are kept).
# Ask first: one may be something you use directly. Without a terminal to ask
# on, keep them. To keep one for good: sudo pacman -D --asexplicit <package>
mapfile -t ORPHANS < <(pacman -Qtdq 2>/dev/null)
if (( ${#ORPHANS[@]} > 0 )); then
echo "These packages were installed for something else, and nothing needs them any more:"
printf ' %s\n' "${ORPHANS[@]}"
if { : < /dev/tty; } 2> /dev/null && read -rp "Remove them? [y/N] " answer < /dev/tty \
&& [[ "$answer" == [yY]* ]]; then
sudo pacman -Rns --noconfirm "${ORPHANS[@]}" || exit 1
echo "To get one of these back, reinstall it with: sudo pacman -S <package>"
else
echo "Kept them. To keep one for good, so you're not asked again: sudo pacman -D --asexplicit <package>"
fi
fi