# 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 \
    openocd \
    && 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

# 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" ]
