#!/bin/sh
# ============================================================
# Kangle 通用引导脚本 (Universal Bootstrap)
# 支持全系 Linux: CentOS/RHEL 6/7/8/9, Debian 10~13, Ubuntu 18~24
#
# 用法 (新服务器, 全系通用, 无需预装任何包):
#   curl -fsSL https://gxroast.com/data/kangle/start.sh -o start || wget -q https://gxroast.com/data/kangle/start.sh -O start; sh start
# ============================================================

BASE_URL="${KANGLE_BASE_URL:-https://gxroast.com/data/kangle}"
# gxroast 的 http 地址 (引导期无 ca-certificates 时用 http 拉包, 无证书鸡生蛋问题)
BASE_HTTP=$(echo "$BASE_URL" | sed 's|^https://|http://|')

# ---------- 确保有下载工具 (curl 或 wget) ----------
# 优先从 gxroast 本机源安装: apt/yum/dnf 自带下载能力, 不依赖 wget/curl;
# 把 gxroast 配成 http 源 (trusted=yes), 全程不碰外网。
# 仅当 gxroast 源安装失败时才回退系统源。
if ! command -v wget >/dev/null 2>&1 && ! command -v curl >/dev/null 2>&1; then
    echo "[引导] 未检测到 wget/curl, 尝试从 gxroast 本机源安装..."
    BOOT_OK=0
    if command -v apt-get >/dev/null 2>&1; then
        # Debian/Ubuntu: 按版本选 debs22 (jammy) / debs12 (bookworm) / debs (trixie+)
        . /etc/os-release 2>/dev/null
        case "$VERSION_ID" in
            26*) BOOT_REPO="$BASE_HTTP/debian/debs26";;
            24*) BOOT_REPO="$BASE_HTTP/debian/debs24";;
            22*) BOOT_REPO="$BASE_HTTP/debian/debs22";;
            12*) BOOT_REPO="$BASE_HTTP/debian/debs12";;
            11*) BOOT_REPO="$BASE_HTTP/debian/debs11";;
            *)   BOOT_REPO="$BASE_HTTP/debian/debs";;
        esac
        echo "deb [trusted=yes] $BOOT_REPO ./" > /etc/apt/sources.list.d/kangle-bootstrap.list
        apt-get update -qq >/dev/null 2>&1
        apt-get install -y -qq wget curl ca-certificates >/dev/null 2>&1 && BOOT_OK=1
        rm -f /etc/apt/sources.list.d/kangle-bootstrap.list
    elif command -v dnf >/dev/null 2>&1 || command -v yum >/dev/null 2>&1; then
        # RHEL 系: 按版本选 rpms (el9/el10) / rpms8 (el8/alinux3) / rpms-alinux4-full
        # [gxroast 2026-08-06] CentOS 6/7 已放弃支持 (证书/内存/适配成本), rpms6/rpms7 源已移除
        REL=$(cat /etc/redhat-release 2>/dev/null | sed 's/.*release\ //' | sed 's/\ .*//' | cut -d '.' -f1 | head -1)
        # [KANGLE_ALINUX_MAP] Alibaba Cloud Linux: alinux3 兼容 el8, alinux4 兼容 el9 (按 VERSION_ID 区分)
        # alinux4 实测无 /etc/redhat-release, REL 提取为空, 需从 os-release 兜底
        if [ "$ID" = "alinux" ] || grep -qi "Alibaba Cloud Linux" /etc/redhat-release 2>/dev/null || grep -qi "Alibaba Cloud Linux" /etc/os-release 2>/dev/null; then
        	. /etc/os-release 2>/dev/null
        	case "$VERSION_ID" in
        	    4*) REL="9";;
        	    *)  REL="8";;
        	esac
        elif [ -z "$REL" ] && [ -r /etc/os-release ]; then
        	. /etc/os-release 2>/dev/null
        	case "$ID" in
        	    debian|ubuntu) REL="12";;
        	    *) REL=$(echo "$VERSION_ID" | grep -oE "^[0-9]+" | head -1);;
        	esac
        fi
        # [ALINUX4 完整依赖镜像] alinux4 (glibc 2.38/perl 5.36) 不兼容 el9 编译链包,
        # 依赖必须走 gxroast rpms-alinux4-full 完整镜像源 (311 包闭包), 100% 走 gxroast.com
        if [ "$ID" = "alinux" ] && [ "${VERSION_ID%%.*}" = "4" ]; then
            BOOT_REPO="$BASE_HTTP/rpms-alinux4-full"
        elif [ "$ID" = "alinux" ] && [ "${VERSION_ID%%.*}" = "3" ]; then
            # [gxroast] alinux3 专属完整源 (glibc 2.32, 254 包闭包含 mysql 8.0.46 el8)
            BOOT_REPO="$BASE_HTTP/rpms-alinux3-full"
        else
        case "$REL" in
            10) BOOT_REPO="$BASE_HTTP/rpms10";;
            8) BOOT_REPO="$BASE_HTTP/rpms8";;
            *) BOOT_REPO="$BASE_HTTP/rpms";;
        esac
        fi
        cat > /etc/yum.repos.d/kangle-bootstrap.repo <<EOF
[kangle-bootstrap]
name=Kangle Bootstrap
baseurl=$BOOT_REPO
enabled=1
gpgcheck=0
EOF
        if command -v dnf >/dev/null 2>&1; then
            dnf -y install wget curl ca-certificates >/dev/null 2>&1 && BOOT_OK=1
        else
            yum -y install wget curl ca-certificates >/dev/null 2>&1 && BOOT_OK=1
        fi
        rm -f /etc/yum.repos.d/kangle-bootstrap.repo
    elif command -v zypper >/dev/null 2>&1; then
        zypper -n install wget curl ca-certificates >/dev/null 2>&1 && BOOT_OK=1
    elif command -v apk >/dev/null 2>&1; then
        apk add wget curl ca-certificates >/dev/null 2>&1 && BOOT_OK=1
    fi
    if [ "$BOOT_OK" != "1" ]; then
        echo "[引导] gxroast 源安装失败, 回退系统源安装..."
        if command -v apt-get >/dev/null 2>&1; then
            apt-get update -qq >/dev/null 2>&1
            apt-get install -y -qq wget curl ca-certificates >/dev/null 2>&1
        elif command -v dnf >/dev/null 2>&1; then
            dnf -y install wget curl ca-certificates >/dev/null 2>&1
        elif command -v yum >/dev/null 2>&1; then
            yum -y install wget curl ca-certificates >/dev/null 2>&1
        elif command -v zypper >/dev/null 2>&1; then
            zypper -n install wget curl ca-certificates >/dev/null 2>&1
        elif command -v apk >/dev/null 2>&1; then
            apk add wget curl ca-certificates >/dev/null 2>&1
        fi
    fi
fi

# ---------- 下载主安装脚本并执行 ----------
DL=""
if command -v wget >/dev/null 2>&1; then
    DL="wget -q --timeout=30 -O start-offline.sh"
elif command -v curl >/dev/null 2>&1; then
    DL="curl -fsSL --connect-timeout 30 -o start-offline.sh"
else
    echo "[引导] 错误: 无 wget/curl, 请手动安装后重试"
    exit 1
fi

echo "[引导] 下载主安装脚本: $BASE_URL/start-offline.sh"
# 只通过 gxroast.com 域名下载, 不固定 IP (用户要求)
# 清理历史可能残留的 hosts 固定行, 确保解析走 DNS
sed -i '/nas\.gxroast\.com/d' /etc/hosts 2>/dev/null
ok=0
for i in 1 2 3; do
    if $DL "$BASE_URL/start-offline.sh" 2>/dev/null && [ -s start-offline.sh ]; then ok=1; break; fi
    sleep 2
    echo "[引导] 第 $i 次下载失败, 重试..."
done
if [ $ok -ne 1 ]; then
    echo "[引导] 下载失败, 请检查网络或 $BASE_URL 可达性"
    exit 1
fi
chmod +x start-offline.sh
sh start-offline.sh
