#!/bin/bash
set -euo pipefail

echo "正在扫描系统..."
echo "├─ 内存及CPU占用"
echo "├─ 磁盘空间"
echo "├─ 缓存占用"
echo "└─ 卸载残留"
echo ""

# 内存及CPU占用
mem_usage=$(free -m | awk '/Mem/{printf "%.1f", $3*100/$2}')
cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{printf "%.1f", 100 - $1}')

# 磁盘空间
root_free=$(df -h / | awk '/\//{print $4}')
root_usage=$(df -h / | awk '/\//{print $5}')

# 缓存占用
cache_size=$(du -sh ~/.cache 2>/dev/null | cut -f1 || echo "0")

# 卸载残留
deb_residues=$(dpkg -l 2>/dev/null | grep ^rc | wc -l || echo "0")
residue_packages=$(dpkg -l 2>/dev/null | grep ^rc | head -1 | awk '{print $2}' || echo "无")

# 输出结果
echo "├─ 内存及CPU占用：${mem_usage}%，$(if (( $(echo "$mem_usage > 80" | bc -l) )); then echo "偏高 ⚠️"; else echo "正常 ✅"; fi)"
echo "├─ CPU占用：${cpu_usage}%，$(if (( $(echo "$cpu_usage > 80" | bc -l) )); then echo "偏高 ⚠️"; else echo "正常 ✅"; fi)"
echo "├─ 磁盘空间：磁盘仅剩 ${root_free} ${root_usage} $(if [[ "${root_usage}" == *"90%"* ]]; then echo "🔴"; elif [[ "${root_usage}" == *"70%"* ]]; then echo "⚠️"; else echo "✅"; fi)"
echo "├─ 缓存占比：发现 ${cache_size} 可清理缓存"
echo "└─ 卸载残留：「${residue_packages}」已卸载，但存在残留文件"
echo ""
