ARG UBUNTU_VERSION=22.04
FROM ubuntu:${UBUNTU_VERSION}

ARG LLVM_VERSION=18
ARG CMAKE_VERSION=3.20.0

# Prevent interactive prompts during apt install
ENV DEBIAN_FRONTEND=noninteractive

RUN for i in 1 2 3; do apt-get update && break || sleep 2; done && \
    apt-get install -y --fix-missing \
    wget gnupg software-properties-common lsb-release \
    zlib1g zlib1g-dev python3 ninja-build curl g++ libcurl4-openssl-dev git && \
    CODENAME=$(lsb_release -cs) && \
    ARCH=$(uname -m) && \
    wget https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/cmake-${CMAKE_VERSION}-linux-${ARCH}.sh && \
    mkdir -p /opt/cmake && \
    sh cmake-${CMAKE_VERSION}-linux-${ARCH}.sh --prefix=/opt/cmake --skip-license && \
    ln -s /opt/cmake/bin/cmake /usr/local/bin/cmake && \
    rm cmake-${CMAKE_VERSION}-linux-${ARCH}.sh

RUN CODENAME=$(lsb_release -cs) && \
    for i in 1 2; do wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key && break || sleep 2; done | apt-key add - && \
    if [ "${LLVM_VERSION}" -ge 16 ]; then \
        add-apt-repository "deb http://apt.llvm.org/${CODENAME}/ llvm-toolchain-${CODENAME}-${LLVM_VERSION} main"; \
    else \
        add-apt-repository "deb http://apt.llvm.org/${CODENAME}/ llvm-toolchain-${CODENAME} main"; \
    fi && \
    for i in 1 2 3; do apt-get update && break || sleep 2; done && \
    apt-get install -y --fix-missing \
      clang-${LLVM_VERSION} \
      clang++-${LLVM_VERSION} \
      llvm-${LLVM_VERSION} \
      llvm-${LLVM_VERSION}-dev \
      lld-${LLVM_VERSION} \
      liblld-${LLVM_VERSION}-dev \
      libpolly-${LLVM_VERSION}-dev && \
    rm -rf /var/lib/apt/lists/*

RUN groupadd -g 1337 c3c && \
    useradd -m -u 1337 -g c3c c3c

USER c3c
ENV PATH="/opt/cmake/bin:${PATH}"
WORKDIR /home/c3c