#!/bin/sh
# bootstrap a bare pod with the deps to cross build windows vulkan and native linux cuda
# run once after a pod reset, from the llama-install.sh root
set -e

# apt: ssl for the http client, ninja for the workflows, wine to smoke windows binaries,
# zstd for inspecting payloads, xz for the lunarg tarball
apt-get update
apt-get install -y --no-install-recommends ninja-build wine zstd libssl-dev xz-utils

# zig master, the cross compiler the cross.cmake toolchain expects
ZURL=$(curl -fsSL https://ziglang.org/download/index.json | python3 -c "import sys,json;print(json.load(sys.stdin)['master']['x86_64-linux']['tarball'])")
rm -rf /opt/zig
mkdir -p /opt/zig
curl -fsSL "$ZURL" | tar -xJ --strip-components=1 -C /opt/zig
ln -sf /opt/zig/zig /usr/local/bin/zig

# lunarg vulkan sdk, headers and glslc only, hinted by vulkan/FindVulkan.cmake at /opt/vulkan-sdk
# pinned to the same version upstream CI uses, bumped via scripts/update-ci.py when needed
VULKAN_VERSION=1.4.341.1
VULKAN="vulkansdk-linux-$(uname -m)-${VULKAN_VERSION}"
rm -rf /tmp/vulkan-extract /opt/vulkan-sdk
mkdir -p /tmp/vulkan-extract
curl -fsSL "https://sdk.lunarg.com/sdk/download/${VULKAN_VERSION}/linux/${VULKAN}.tar.xz" | tar -xJf - -C /tmp/vulkan-extract
mv "/tmp/vulkan-extract/${VULKAN_VERSION}/$(uname -m)" /opt/vulkan-sdk
rm -rf /tmp/vulkan-extract

# python: preset generator, bucket client with fast transfers, hf CLI ships with huggingface_hub
pip install -q --break-system-packages "huggingface_hub[hf_xet]" ruamel.yaml

# smoke the toolchain, prepend cuda so the check works in any shell
PATH="/usr/local/cuda/bin:$PATH"
zig version
wine --version
/opt/vulkan-sdk/bin/glslc --version | head -1
nvcc --version | tail -1
hf --version

# wipe any cmake cache left over from a pre-SDK configure, the SDK is now in place
# so the next configure must re-run find_path for VULKAN_HEADERS from scratch
rm -rf build

echo "pod ready, run: hf auth login"
