# Use Debian 13 (Trixie) as base
FROM docker.io/library/debian:13-slim

# Avoid interactive prompts during package installation
ENV DEBIAN_FRONTEND=noninteractive

# Update package lists and install basic tools
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    locales \
    git \
    ca-certificates \
    make \
    cmake \
    cppcheck \
    usbutils \
    sudo \
    curl \
    bash \
    bash-completion \
    xz-utils \
    gcc \
    g++ \
    nano \
    ssh \
    btop \
    xxd \
    gdb-multiarch \
    && rm -rf /var/lib/apt/lists/*

# Generate en_US.UTF-8 locale
RUN sed -i 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \
    locale-gen

# Set environment variables for locale
ENV LANG=en_US.UTF-8 \
    LANGUAGE=en_US:en \
    LC_ALL=en_US.UTF-8

# Install betaflight specific development tools
# libblocksruntime-dev --> blocks library
# python3-intelhex --> required to convert .hex files to .dfu files
# dfu-util --> required for dfu flashing
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    python3-intelhex \
    dfu-util \
    libblocksruntime-dev \
    && rm -rf /var/lib/apt/lists/*

# Install clang-18 - version purposefully held back for stability
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    clang-18 \
    clang-format-18 \
    llvm-18-dev \
    libc++-18-dev \
    libc++abi-18-dev \
    libclang-rt-18-dev \
    && rm -rf /var/lib/apt/lists/*

# Set clang/clang++ alternatives to clang-18
RUN update-alternatives --install /usr/bin/clang clang /usr/bin/clang-18 100 && \
    update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-18 100 && \
    update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-18 100

# Install stm32flash from source - NOTE: make stm32flash_install points to a broken link
RUN git clone git://git.code.sf.net/p/stm32flash/code stm32flash-code && \
    cd stm32flash-code && \
    make && \
    make install && \
    cd .. && \
    rm -rf stm32flash-code

# Build OpenOCD from STMicroelectronics' fork.
# The Debian-packaged OpenOCD (0.12.0) has no STM32H5 support: neither
# target/stm32h5x.cfg nor the stm32h5x flash driver are present (H5 landed
# after the 0.12.0 release; mainline master and the xpack builds don't have it
# yet either). Without them `make openocd-gdb` cannot flash or debug H5
# (H562/H563) boards. ST's fork ships both and keeps the other STM32 families
# working. Built from source because no current distro/prebuilt package carries
# H5 support.
ARG OPENOCD_ST_REF=openocd-cubeide-r7
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    autoconf \
    automake \
    libtool \
    pkg-config \
    libusb-1.0-0-dev \
    libhidapi-dev && \
    git clone --depth 1 -b ${OPENOCD_ST_REF} https://github.com/STMicroelectronics/OpenOCD /tmp/openocd-st && \
    cd /tmp/openocd-st && \
    ./bootstrap && \
    ./configure --enable-stlink --disable-werror --disable-doc --prefix=/usr/local && \
    make -j"$(nproc)" && \
    make install && \
    cd / && \
    rm -rf /tmp/openocd-st && \
    rm -rf /var/lib/apt/lists/*

# Create a non-root user for DevPod/VSCodium
ARG USERNAME=devpod
ARG USER_UID=1000
ARG USER_GID=$USER_UID

# Create the user and primary group
RUN groupadd --gid $USER_GID $USERNAME && \
    useradd --uid $USER_UID --gid $USER_GID -m $USERNAME --shell /bin/bash && \
    echo "$USERNAME ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

# Switch to non-root user
USER $USERNAME

# Set working directory
WORKDIR /workspace

# Default command
CMD [ "bash" ]
