#!/bin/bash
set -e
echo "🚀 自动下载稳定版本 + 零污染编译（离线友好）"

# ====== 1. 创建独立工作目录 ======
WORK_DIR="$PWD/allinone-clean"
CACHE_DIR="$WORK_DIR/deps-cache"
BUILD_DIR="$WORK_DIR/build"
mkdir -p "$CACHE_DIR" "$BUILD_DIR"

# ====== 2. 稳定版本定义 ======
NODE_VER="18.20.3"
EMQX_VER="5.8.2"
REDIS_VER="7.2.5"
MARIA_VER="10.11.8"
LUAJIT_VER="2.1.0-beta3"
TINYGO_VER="0.31.0"
TD_VER="3.3.2.0"

# ====== 3. 自动下载（不污染系统） ======
echo "📦 开始下载依赖（本地缓存）"

# Node.js 静态头文件
wget -q --show-progress -O "$CACHE_DIR/node-headers.tar.gz" \
  "https://nodejs.org/dist/v$NODE_VER/node-v$NODE_VER-headers.tar.gz"

# EMQX 静态库（官方提供）
wget -q --show-progress -O "$CACHE_DIR/emqx-static.tar.gz" \
  "https://github.com/emqx/emqx/releases/download/v$EMQX_VER/emqx-v$EMQX_VER-static.tar.gz"

# Redis 静态库
wget -q --show-progress -O "$CACHE_DIR/redis-static.tar.gz" \
  "https://github.com/redis/redis/archive/v$REDIS_VER.tar.gz"

# MariaDB 嵌入式静态库
wget -q --show-progress -O "$CACHE_DIR/mariadb-static.tar.gz" \
  "https://archive.mariadb.org/mariadb-$MARIA_VER/bintar-linux-systemd-x86_64.tar.gz"

# LuaJIT 静态库
wget -q --show-progress -O "$CACHE_DIR/luajit-static.tar.gz" \
  "https://luajit.org/download/LuaJIT-$LUAJIT_VER.tar.gz"

# TinyGo 静态库
wget -q --show-progress -O "$CACHE_DIR/tinygo-static.tar.gz" \
  "https://github.com/tinygo-org/tinygo/releases/download/v$TINYGO_VER/tinygo-linux-amd64.tar.gz"

# TDengine 静态库
wget -q --show-progress -O "$CACHE_DIR/td-static.tar.gz" \
  "https://github.com/taosdata/TDengine/releases/download/v$TD_VER/TDengine-server-$TD_VER-Linux-x64.tar.gz"

echo "✅ 下载完成！"

# ====== 4. 解压到本地缓存（不装系统） ======
echo "📂 正在解压依赖（不污染系统）"
tar -xzf "$CACHE_DIR/node-headers.tar.gz" -C "$CACHE_DIR" --strip-components=1
tar -xzf "$CACHE_DIR/emqx-static.tar.gz" -C "$CACHE_DIR" --strip-components=1
tar -xzf "$CACHE_DIR/redis-static.tar.gz" -C "$CACHE_DIR" --strip-components=1
tar -xzf "$CACHE_DIR/mariadb-static.tar.gz" -C "$CACHE_DIR" --strip-components=1
tar -xzf "$CACHE_DIR/luajit-static.tar.gz" -C "$CACHE_DIR" --strip-components=1
tar -xzf "$CACHE_DIR/tinygo-static.tar.gz" -C "$CACHE_DIR" --strip-components=1
tar -xzf "$CACHE_DIR/td-static.tar.gz" -C "$CACHE_DIR" --strip-components=1

# ====== 5. 生成基本框架（不依赖外网） ======
echo "🏗️ 生成基本框架（稳定版本）"
mkdir -p "$WORK_DIR"/{src/core,src/modules,build,cmake}
for mod in node emqx redis mysql go lua java; do
  mkdir -p "$WORK_DIR/src/modules/$mod"
done

# === 版本头 ===
cat > "$WORK_DIR/src/shared/version.h" <<EOF
#ifndef VERSION_H
#define VERSION_H
#define ALLINONE_VERSION "2025-06-LTS-OFFLINE"
#define NODE_VER    "18.20.3"
#define EMQX_VER    "5.8.2"
#define REDIS_VER   "7.2.5"
#define MARIA_VER   "10.11.8"
#define LUAJIT_VER  "2.1.0-beta3"
#define TINYGO_VER  "0.31.0"
#define TD_VER      "3.3.2.0"
#endif
EOF

# === 管家桩 ===
cat > "$WORK_DIR/src/core/manager.c" <<'EOF'
#include "version.h"
#include <stdio.h>
void manager_init(void) {
    printf("[CORE] All-in-One OFFLINE LTS %s\\n", ALLINONE_VERSION);
}
EOF

# === 模块桩 ===
for mod in node emqx redis mysql go lua java; do
cat > "$WORK_DIR/src/modules/$mod/stub.c" <<EOF
#include <stdio.h>
void mod_${mod}_init(void) {
    printf("[MOD] $mod stub (OFFLINE LTS)\\n");
}
EOF
done

# === CMakeLists.txt（无 Chromium/大模型） ===
cat > "$WORK_DIR/CMakeLists.txt" <<'EOF'
cmake_minimum_required(VERSION 3.20)
project(allinone LANGUAGES C CXX)

option(M_NODE      "Node.js 18.20.3" ON)
option(M_EMQX      "EMQX 5.8.2" ON)
option(M_REDIS     "Redis 7.2.5" ON)
option(M_MYSQL     "MariaDB 10.11.8" ON)
option(M_GO        "Go + TD 3.3.2.0" ON)
option(M_LUA       "LuaJIT 2.1.0-beta3" ON)
option(M_JAVA      "Java LTS" ON)
option(M_CHROMIUM  "Chromium" OFF)
option(M_LLAMA     "Large Model" OFF)

add_subdirectory(src/core)
foreach(mod IN ITEMS node emqx redis mysql go lua java)
  if(M_${mod}) add_subdirectory(src/modules/${mod}) endif()
endforeach()
add_executable(allinone src/main.c)
target_link_libraries(allinone PRIVATE core)
EOF

# === 主入口 ===
cat > "$WORK_DIR/src/main.c" <<'EOF'
#include "version.h"
extern void manager_init(void);
int main(void) {
    manager_init();
    return 0;
}
EOF

# === 编译脚本 ===
cat > "$WORK_DIR/build.sh" <<'EOF'
#!/bin/bash
set -e
echo "🔨 编译（无 Chromium/大模型）"
cmake -B build -S . -G Ninja \
  -DCMAKE_BUILD_TYPE=Release \
  -DMODULE_CHROMIUM=OFF \
  -DMODULE_LLAMA=OFF \
  -DCACHE_DIR=deps-cache
ninja -C build
echo "✅ 完成！产物：build/src/allinone"
EOF
chmod +x "$WORK_DIR/build.sh"

# === 7. 完成提示 ===
echo "✅ 离线环境已生成："
tree -L 2 "$WORK_DIR"
echo "🎯 下一步：cd $WORK_DIR && ./build.sh"
