#!/bin/sh
set -eu

REPO_ROOT=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
TARGET_USER=${TARGET_USER:-user}
TARGET_UID=${TARGET_UID:-1000}
STATE_DIR=/var/lib/system-monitor

if [ "$(id -u)" != 0 ]; then
  echo "uninstall.sh must run as root through gui-askpass sudo" >&2
  exit 1
fi

TARGET_ENTRY=$(getent passwd "$TARGET_USER")
if [ -z "$TARGET_ENTRY" ]; then
  echo "cannot resolve account for $TARGET_USER" >&2
  exit 1
fi
resolved_uid=$(printf '%s\n' "$TARGET_ENTRY" | cut -d: -f3)
target_gid=$(printf '%s\n' "$TARGET_ENTRY" | cut -d: -f4)
user_home=$(printf '%s\n' "$TARGET_ENTRY" | cut -d: -f6)
if [ "$resolved_uid" != "$TARGET_UID" ] || [ -z "$target_gid" ] || [ -z "$user_home" ] || [ "$user_home" = / ]; then
  echo "account mismatch for $TARGET_USER: expected uid $TARGET_UID, got uid ${resolved_uid:-missing}, home ${user_home:-missing}" >&2
  exit 1
fi

user_systemctl() {
  runuser -u "$TARGET_USER" -- env XDG_RUNTIME_DIR="/run/user/$TARGET_UID" systemctl --user "$@"
}

safe_unlink() {
  relative=$1 target=$2
  python3 "$REPO_ROOT/lib/user-home-files.py" unlink \
    --home "$user_home" --relative "$relative" --target "$target" \
    --uid "$TARGET_UID" --gid "$target_gid"
}

user_systemctl disable --now agent-guard.service pids-guard.service desktop-memory-reserve.service 2>/dev/null || true
safe_unlink ".config/systemd/user/agent-guard.service" "$REPO_ROOT/systemd/user/agent-guard.service"
safe_unlink ".config/systemd/user/guard.slice" "$REPO_ROOT/systemd/user/guard.slice"
safe_unlink ".config/systemd/user/pids-guard.service" "$REPO_ROOT/systemd/user/pids-guard.service"
safe_unlink ".config/systemd/user/desktop-memory-reserve.service" "$REPO_ROOT/systemd/user/desktop-memory-reserve.service"
for relative in \
  ".config/systemd/user/app.slice.d/50-TasksMax.conf" \
  ".config/systemd/user/app.slice.d/override.conf" \
  ".config/systemd/user/app.slice.d/50-oomd-omit.conf"; do
  safe_unlink "$relative" "$REPO_ROOT/systemd/user/app.slice.d/${relative##*/}"
done
for name in pids-guard pids-rescue desktop-memory-reserve cinnamon-safe-recover; do
  safe_unlink ".local/bin/$name" "$REPO_ROOT/bin/$name"
done
user_systemctl daemon-reload

systemctl disable --now netdata 2>/dev/null || true
apt-get remove -y netdata 2>/dev/null || true

rm -f /usr/local/bin/agent-guard /usr/local/bin/agentctl /usr/local/bin/run-agent /usr/local/bin/run-build
rm -rf /opt/system-monitor
rm -f /etc/system-monitor/agent-guard.toml
rmdir /etc/system-monitor 2>/dev/null || true
rm -f /etc/tmpfiles.d/system-monitor-tmp.conf
rm -f /etc/systemd/system/agents.slice /etc/systemd/system/builds.slice
rm -f /etc/netdata/netdata.conf /etc/netdata/stream-and-cloud-disabled.conf /etc/netdata/health_alarm_notify.conf
rm -f /etc/netdata/health.d/mem_trajectory.conf /etc/netdata/health.d/tmpfs_guard.conf /etc/netdata/health.d/cpu_runaway.conf /etc/netdata/health.d/proc_fd.conf /etc/netdata/health.d/disk_fill.conf
rm -f /usr/libexec/netdata/plugins.d/mem_trajectory.plugin /usr/libexec/netdata/plugins.d/tmpfs_guard.plugin /usr/libexec/netdata/plugins.d/cpu_runaway.plugin /usr/libexec/netdata/plugins.d/proc_fd.plugin
rm -f /usr/libexec/netdata/plugins.d/disk_guard.plugin
rm -f /usr/lib/netdata/plugins.d/mem_trajectory.plugin /usr/lib/netdata/plugins.d/tmpfs_guard.plugin /usr/lib/netdata/plugins.d/cpu_runaway.plugin /usr/lib/netdata/plugins.d/proc_fd.plugin /usr/lib/netdata/plugins.d/disk_guard.plugin
systemctl daemon-reload

if [ "${1:-}" = "--restore-retired" ]; then
  python3 "$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)/lib/notifier-retirement.py" restore \
    --user "$TARGET_USER" --uid "$TARGET_UID" --home "$user_home" --state-dir "$STATE_DIR"
fi

echo "system-monitor uninstall complete; root oomd/caps/swappiness settings untouched"
