#!/bin/bash
# ============================================================================
#  uninstall.sh —— 回滚「开机自动满屏系统资源面板」（GPU 机）
#  sudo bash uninstall.sh
# ============================================================================
set -uo pipefail
[ "$(id -u)" -eq 0 ] || { echo "请用 sudo 运行"; exit 1; }

echo "1) 关闭 tty1 自动登录"
rm -rf /etc/systemd/system/getty@tty1.service.d
systemctl daemon-reload
systemctl restart getty@tty1.service 2>/dev/null || true

echo "2) 移除 ~/.bash_profile 中的面板启动块"
BP=/home/zyw/.bash_profile
if [ -f "$BP" ]; then
    cp -a "$BP" "${BP}.bak-$(date +%Y%m%d-%H%M%S)"
    python3 - "$BP" <<'PY'
import sys,re
p=sys.argv[1]
s=open(p).read()
s=re.sub(r"\n# >>> sysmon console dashboard >>>.*?# <<< sysmon console dashboard <<<\n","\n",s,flags=re.S)
open(p,"w").write(s)
PY
    chown zyw:zyw "$BP"
fi

echo "3) 移除脚本与配置（btop/nvtop 保留，可手动使用）"
tmux kill-session -t sysmon 2>/dev/null || true
rm -f /usr/local/bin/sysmon.sh /usr/local/bin/sysmon /usr/local/bin/sysmon-status.sh \
      /usr/local/bin/sysmon-disable /usr/local/bin/sysmon-enable /etc/sysmon.conf
rm -f /home/zyw/.sysmon-off
echo "完成。如需彻底移除 btop：rm -rf /opt/btop /usr/local/bin/btop"
