#!/usr/bin/env bash
# KVT fork: tools for the BlueGit CI jobs in blue-ci-cd.yml, on an Ubuntu image.
#   ci-setup.sh linux     Linux build (as .github/actions/setup-linux): headers, clang 21, mold (clang-21 zonder lldb/lld/clangd)
#   ci-setup.sh windows   the above plus the cross-build bits: lld/llvm 21, wine, cargo-xwin
# Rust comes from rustup (stable) into $CARGO_HOME, which the jobs cache.
set -euo pipefail
mode="${1:-linux}"
export DEBIAN_FRONTEND=noninteractive
# Keep Cargo and rustup in the workspace so ci-cache.sh can pack them.
# BlueGit cache only copies files, not directories; the job stores a tarball.
if [[ -n "${CI:-}" ]]; then
  export CARGO_HOME="${CARGO_HOME:-$PWD/cargo-home}"
  export RUSTUP_HOME="${RUSTUP_HOME:-$PWD/rustup-home}"
  export PATH="$CARGO_HOME/bin:$PATH"
  if [[ "$mode" == windows ]]; then
    export XWIN_CACHE_DIR="${XWIN_CACHE_DIR:-$PWD/xwin-cache}"
  fi
fi
apt-get update -qq
apt-get install -y -qq --no-install-recommends \
  ca-certificates curl git gnupg lsb-release wget \
  build-essential binutils file python3 unzip xz-utils patch jq \
  clang mold pkg-config libclang-dev \
  libglib2.0-dev libfontconfig-dev libasound2-dev libpulse-dev libva-dev \
  libxkbcommon-dev libxkbcommon-x11-dev \
  libx11-dev libxcb1-dev libxext-dev libxfixes-dev libxdamage-dev \
  libxrandr-dev libxcomposite-dev libwayland-dev libpipewire-0.3-dev \
  desktop-file-utils >/dev/null
# webrtc-sys needs clang 21 (check_clang_version in vendor/webrtc-sys/build.rs).
# Alleen het apt.llvm.org-repo toevoegen en clang-21 installeren: llvm.sh zelf zet er
# ook lldb, lld en clangd bij (honderden MB extra uitpakken op de gedeelde runner).
codename="$(lsb_release -cs)"
curl -fsSL https://apt.llvm.org/llvm-snapshot.gpg.key -o /etc/apt/trusted.gpg.d/apt.llvm.org.asc
echo "deb https://apt.llvm.org/$codename/ llvm-toolchain-$codename-21 main" > /etc/apt/sources.list.d/llvm-21.list
apt-get update -qq
apt-get install -y -qq --no-install-recommends clang-21 >/dev/null
if [[ "$mode" == windows ]]; then
  # cargo-xwin (ureq/native-tls) needs OpenSSL headers to build on the Linux host.
  apt-get install -y -qq --no-install-recommends lld-21 llvm-21 clang-tools-21 wine64 libssl-dev >/dev/null
  # cargo-xwin calls the plain names.
  for tool in clang-cl lld-link llvm-lib llvm-rc llvm-ar llvm-dlltool; do
    for candidate in "/usr/lib/llvm-21/bin/$tool" "/usr/bin/$tool-21"; do
      [[ -x "$candidate" ]] && ln -sf "$candidate" "/usr/local/bin/$tool" && break
    done
  done
fi
if ! command -v cargo >/dev/null; then
  curl -fsSL https://sh.rustup.rs | sh -s -- -y --profile minimal --default-toolchain stable >/dev/null
fi
# De CI-cache bewaart cargo-home/bin maar niet cargo-home/env. Is cargo er dan al (uit de cache),
# dan draait rustup-init niet en ontbreekt dat bestand, waar de jobs hierna hun PATH uit halen.
cargo_env="${CARGO_HOME:-$HOME/.cargo}/env"
if [[ ! -f "$cargo_env" ]]; then
  printf '%s\n' "case \":\${PATH}:\" in *:\"$(dirname "$cargo_env")/bin\":*) ;; *) export PATH=\"$(dirname "$cargo_env")/bin:\$PATH\" ;; esac" > "$cargo_env"
fi
# shellcheck disable=SC1091
. "$cargo_env"
rustup toolchain install stable --profile minimal >/dev/null
if [[ "$mode" == windows ]]; then
  rustup target add x86_64-pc-windows-msvc >/dev/null
  command -v cargo-xwin >/dev/null || cargo install cargo-xwin --locked >/dev/null
fi
rustc --version
clang-21 --version | head -n 1
