#!/bin/bash

# 服务器一键清理脚本
# 警告：此脚本会彻底清除Docker、Nginx及相关数据，请谨慎使用！

set -e

echo "=========================================="
echo "        服务器数据清理脚本"
echo "=========================================="
echo "警告：此操作不可逆，将删除："
echo "- 所有Docker容器、镜像、卷"
echo "- Nginx及其配置"
echo "- 命令历史记录"
echo "- 系统日志"
echo "- 临时文件"
echo "=========================================="

# 询问确认
read -p "确定要继续吗？输入 'YES' 继续: " confirm
if [ "$confirm" != "YES" ]; then
    echo "操作已取消"
    exit 1
fi

echo "开始清理，请稍候..."

# ==================== Docker 清理 ====================
echo "[1/8] 清理Docker..."
if command -v docker &> /dev/null; then
    echo "  - 停止所有容器..."
    docker stop $(docker ps -aq) 2>/dev/null || true
    
    echo "  - 删除所有容器..."
    docker rm $(docker ps -aq) 2>/dev/null || true
    
    echo "  - 删除所有镜像..."
    docker rmi $(docker images -q) 2>/dev/null || true
    
    echo "  - 删除所有卷..."
    docker volume rm $(docker volume ls -q) 2>/dev/null || true
    
    echo "  - 清理系统..."
    docker system prune -a --volumes -f 2>/dev/null || true
    
    echo "  - 停止Docker服务..."
    sudo systemctl stop docker 2>/dev/null || true
    sudo systemctl disable docker 2>/dev/null || true
    
    echo "  - 卸载Docker..."
    sudo apt remove --purge docker-ce docker-ce-cli containerd.io docker-compose-plugin docker-buildx-plugin -y 2>/dev/null || true
    sudo apt remove --purge docker docker-engine docker.io containerd runc -y 2>/dev/null || true
    
    echo "  - 删除Docker文件..."
    sudo rm -rf /var/lib/docker
    sudo rm -rf /var/lib/containerd
    sudo rm -rf /etc/docker
    sudo rm -rf /usr/local/bin/docker-compose
    sudo rm -rf /usr/bin/docker-compose
    sudo groupdel docker 2>/dev/null || true
else
    echo "  - Docker未安装，跳过"
fi

# ==================== Nginx 清理 ====================
echo "[2/8] 清理Nginx..."
if command -v nginx &> /dev/null; then
    echo "  - 停止Nginx服务..."
    sudo systemctl stop nginx 2>/dev/null || true
    sudo systemctl disable nginx 2>/dev/null || true
    
    echo "  - 卸载Nginx..."
    sudo apt remove --purge nginx nginx-common nginx-core nginx-full nginx-light -y 2>/dev/null || true
    
    echo "  - 删除Nginx文件..."
    sudo rm -rf /etc/nginx
    sudo rm -rf /var/log/nginx
    sudo rm -rf /var/www/html
    sudo rm -rf /usr/share/nginx
    sudo rm -rf /var/cache/nginx
else
    echo "  - Nginx未安装，跳过"
fi

# ==================== 清理命令历史 ====================
echo "[3/8] 清理命令历史..."
# 当前用户历史
history -c 2>/dev/null || true
history -w 2>/dev/null || true
> ~/.bash_history 2>/dev/null || true
rm -f ~/.bash_history 2>/dev/null || true
rm -f ~/.zsh_history 2>/dev/null || true
rm -f ~/.fish_history 2>/dev/null || true

# Root用户历史
sudo rm -f /root/.bash_history 2>/dev/null || true
sudo rm -f /root/.zsh_history 2>/dev/null || true
sudo rm -f /root/.fish_history 2>/dev/null || true

# 所有用户历史
sudo find /home -name ".*_history" -delete 2>/dev/null || true

echo "  - 命令历史已清除"

# ==================== 清理系统日志 ====================
echo "[4/8] 清理系统日志..."
sudo journalctl --vacuum-time=1s 2>/dev/null || true
sudo rm -rf /var/log/*.log* 2>/dev/null || true
sudo rm -rf /var/log/auth.log* 2>/dev/null || true
sudo rm -rf /var/log/syslog* 2>/dev/null || true
sudo rm -rf /var/log/kern.log* 2>/dev/null || true
sudo rm -rf /var/log/dpkg.log* 2>/dev/null || true
sudo rm -rf /var/log/apt/ 2>/dev/null || true
echo "  - 系统日志已清除"

# ==================== 清理临时文件 ====================
echo "[5/8] 清理临时文件..."
sudo rm -rf /tmp/* 2>/dev/null || true
sudo rm -rf /var/tmp/* 2>/dev/null || true
sudo rm -rf /var/cache/apt/* 2>/dev/null || true
rm -rf ~/Downloads/* 2>/dev/null || true
rm -rf ~/Documents/* 2>/dev/null || true
echo "  - 临时文件已清除"

# ==================== 清理用户配置 ====================
echo "[6/8] 清理用户配置..."
rm -rf ~/.ssh 2>/dev/null || true
sudo rm -rf /root/.ssh 2>/dev/null || true
rm -f ~/.bashrc.backup ~/.profile.backup ~/.vimrc 2>/dev/null || true
sudo rm -f /root/.vimrc 2>/dev/null || true
echo "  - 用户配置已清除"

# ==================== 清理包管理器 ====================
echo "[7/8] 清理包管理器..."
sudo apt clean 2>/dev/null || true
sudo apt autoclean 2>/dev/null || true
sudo apt autoremove -y 2>/dev/null || true
sudo rm -rf /var/lib/apt/lists/* 2>/dev/null || true
sudo rm -rf /etc/apt/sources.list.d/docker* 2>/dev/null || true
sudo rm -rf /usr/share/keyrings/docker* 2>/dev/null || true
echo "  - 包管理器缓存已清除"

# ==================== 最终清理 ====================
echo "[8/8] 最终清理..."
# 清理其他可能的敏感文件
rm -f ~/.wget-hsts 2>/dev/null || true
rm -f ~/.gitconfig 2>/dev/null || true
sudo rm -f /root/.wget-hsts 2>/dev/null || true
sudo rm -f /root/.gitconfig 2>/dev/null || true

# 清理可能的密钥文件
find ~ -name "*.pem" -delete 2>/dev/null || true
find ~ -name "*.key" -delete 2>/dev/null || true
sudo find /root -name "*.pem" -delete 2>/dev/null || true
sudo find /root -name "*.key" -delete 2>/dev/null || true

echo "  - 最终清理完成"

echo "=========================================="
echo "           清理完成！"
echo "=========================================="
echo "已清理项目："
echo "✓ Docker (容器、镜像、卷、配置)"
echo "✓ Nginx (服务、配置、日志)"
echo "✓ 命令历史记录"
echo "✓ 系统日志"
echo "✓ 临时文件和缓存"
echo "✓ 用户配置文件"
echo "✓ SSH密钥"
echo "✓ 包管理器缓存"
echo "=========================================="

# 验证清理结果
echo "验证清理结果："
docker --version 2>/dev/null && echo "⚠ Docker仍存在" || echo "✓ Docker已清除"
nginx -v 2>/dev/null && echo "⚠ Nginx仍存在" || echo "✓ Nginx已清除"
ls -la ~/.bash_history 2>/dev/null && echo "⚠ 历史文件仍存在" || echo "✓ 历史记录已清除"

echo ""
echo "建议在交还服务器前重启系统："
echo "sudo reboot"
echo ""
echo "⚠ 注意：此脚本会立即清除历史记录，包括本次执行记录"

# 最后清理本次执行的历史记录
history -d $((HISTCMD-1)) 2>/dev/null || true
