#!/usr/bin/env bash
# Sudo cleanup script -- review before running.
# Removes dormant game servers (ARK, Valheim, Terraria), trims journald logs,
# caps snap revision retention. Does NOT touch Bitbucket, TeamViewer, or the
# matt-wakeford.co.uk / sijbers.uk / diceheart.com websites.
#
# Run with:  sudo bash /tmp/cleanup-sudo.sh
#
# Set -e: abort on first error so we don't cascade-damage state.
set -euo pipefail

echo "=== Pre-cleanup disk ==="
df -h / | grep -v Filesystem

# ────────────────────────────────────────────────
# 1. ARK server -- install, user, broken systemd unit
# ────────────────────────────────────────────────
echo
echo "=== ARK ==="
if systemctl list-unit-files ark.service &>/dev/null; then
    echo "  Disabling ark.service"
    systemctl disable --now ark.service 2>/dev/null || true
fi
if [[ -f /lib/systemd/system/ark.service ]]; then
    echo "  Removing /lib/systemd/system/ark.service"
    rm -f /lib/systemd/system/ark.service
fi
if [[ -f /etc/systemd/system/ark.service ]]; then
    echo "  Removing /etc/systemd/system/ark.service"
    rm -f /etc/systemd/system/ark.service
fi
if id ark &>/dev/null; then
    # Make sure no processes are running as ark before userdel.
    pkill -u ark 2>/dev/null || true
    sleep 1
    echo "  Removing 'ark' user + home"
    userdel -r ark 2>/dev/null || userdel ark
fi

# ────────────────────────────────────────────────
# 2. Valheim (vhserver) -- LGSM stack
# ────────────────────────────────────────────────
echo
echo "=== Valheim (vhserver) ==="
if id vhserver &>/dev/null; then
    pkill -u vhserver 2>/dev/null || true
    sleep 1
    echo "  Removing 'vhserver' user + home (incl. orphaned LGSM backups)"
    userdel -r vhserver 2>/dev/null || userdel vhserver
fi

# ────────────────────────────────────────────────
# 3. Terraria -- empty home, never used
# ────────────────────────────────────────────────
echo
echo "=== Terraria ==="
if id Terraria &>/dev/null; then
    pkill -u Terraria 2>/dev/null || true
    echo "  Removing 'Terraria' user + home"
    userdel -r Terraria 2>/dev/null || userdel Terraria
fi

# ────────────────────────────────────────────────
# 4. systemd reload to forget the gone units
# ────────────────────────────────────────────────
echo
echo "=== systemctl daemon-reload ==="
systemctl daemon-reload
systemctl reset-failed 2>/dev/null || true

# ────────────────────────────────────────────────
# 5. journald -- cap to 500 MB
# ────────────────────────────────────────────────
echo
echo "=== journald ==="
echo "  Trimming /var/log/journal to 500 MB"
journalctl --vacuum-size=500M

# ────────────────────────────────────────────────
# 6. snap -- only keep current + 1 prior revision
# ────────────────────────────────────────────────
echo
echo "=== snap retention ==="
echo "  Setting refresh.retain=2 (snap will auto-clean older revs over time)"
snap set system refresh.retain=2

# Force-clean orphaned _old.snap files older than 30 days. Snap will redo
# this organically but we can prod it now to reclaim immediately.
echo "  Forcing snap to drop disabled revisions"
for snap in $(snap list --all | awk '/disabled/{print $1, $3}'); do
    if [[ "$snap" == "disabled" ]]; then continue; fi
done
# Easier path: ask snap directly.
LANG=C snap list --all | awk '/disabled/{print $1, $3}' | \
    while read -r name rev; do
        echo "    snap remove --revision=$rev $name"
        snap remove --revision="$rev" "$name" || true
    done

# ────────────────────────────────────────────────
echo
echo "=== Post-cleanup disk ==="
df -h / | grep -v Filesystem
echo
echo "Done. Bitbucket, TeamViewer, and websites untouched."
