#!/bin/bash
set -e

echo "======================================="
echo " 🔧 Debian 12 → Proxmox VE 8 一键安装脚本（无自动重启）"
echo "======================================="

# 1. 检查系统版本
if ! grep -qi "12" /etc/os-release; then
    echo "❌ 本脚本仅支持 Debian 12 (Bookworm)"
    exit 1
fi

echo "✔ 系统版本正确：Debian 12"

# 2. 更新系统
echo "🔄 更新系统..."
apt update && apt full-upgrade -y

# 3. 添加 PVE 仓库
echo "📦 添加 Proxmox VE 官方仓库..."
echo "deb http://mirrors.tuna.tsinghua.edu.cn/proxmox/debian/pve bookworm pve-no-subscription" \
    > /etc/apt/sources.list.d/pve.list

# 4. 添加 PVE 密钥（正确文件名）
echo "🔑 添加 Proxmox 仓库密钥..."
wget -O /etc/apt/trusted.gpg.d/proxmox-release-bookworm.gpg \
https://enterprise.proxmox.com/debian/proxmox-release-bookworm.gpg

# 5. 更新包列表
echo "🔄 更新软件列表..."
apt update

# 6. 安装 Proxmox VE
echo "🚀 安装 Proxmox VE..."
DEBIAN_FRONTEND=noninteractive apt install -y proxmox-ve postfix open-iscsi

# 7. 删除 Debian 默认内核（避免冲突）
echo "🧹 清理 Debian 默认内核..."
apt remove -y linux-image-amd64 || true
apt remove -y "linux-image-6.1*" || true
update-grub

# 8. 禁用订阅提示
echo "🔧 禁用订阅提示(去广告)..."
sed -i "s/data.status !== 'Active'/false/" \
    /usr/share/javascript/proxmox-widget-toolkit/proxmoxlib.js || true
systemctl restart pveproxy || true

echo "======================================="
echo " 🎉 Proxmox VE 8 安装完成！(未重启)"
echo " 📌 请手动重启以加载 PVE 内核 (6.x-pve)"
echo " ⚠️ 当前仍运行 Debian 内核，PVE 功能仅部分可用"
echo "======================================="

echo "👉 手动重启命令:  reboot"
EOF
