# if provided it will make debug build
ARG cmake_build_type=Release
# whether to make GUI or headless build (defaults to headless)
ARG headless_build=1
# system to start with the build with
# currently will break for other images
ARG base_image=ubuntu:latest

#-------------------------------BUILDER--------------------------------
# Start with the base image
FROM --platform=linux/arm64 ${base_image} AS builder

# Renew the ARGs
ARG headless_build
ARG cmake_build_type

# Environment settings
ENV LANG en_US.utf8
ENV LANGUAGE=en_US
ENV DEBIAN_FRONTEND=noninteractive
ENV CMAKE_BUILD_TYPE=${cmake_build_type}
ENV HEADLESS_BUILD=${headless_build}

# Enable ARM64 architecture and update package lists
RUN dpkg --add-architecture arm64 && apt-get update -y

# Install necessary tools to create users and groups
RUN apt-get install -y adduser \
    && apt-cache search clang-tools

# Configure dpkg to skip systemd post-installation scripts
RUN mkdir -p /etc/systemd/system && \
    echo "exit 0" > /usr/sbin/policy-rc.d && chmod +x /usr/sbin/policy-rc.d && \
    apt-get install -y --no-install-recommends \
# testing
    clang

RUN apt-get install -y --no-install-recommends \
    python3.12-dev \
    python3.12-venv \
    python3-pip \
    libsdl2-dev \
    libglut-dev \
    libopenal-dev \
    make \
    curl \
    rsync \
    clang-format \
    cmake \
    libvorbis-dev \
    build-essential \
    && rm /usr/sbin/policy-rc.d

# Copy source code
COPY ./ /home/ubuntu/ballistica

# Set the working directory
WORKDIR /home/ubuntu/ballistica

# Compile the application
RUN mkdir /ballistica_cmake && \
    if [ "$HEADLESS_BUILD" != "0" ]; then \
        make cmake-server-build && \
        mv build/cmake/*/ballisticakit_headless build/cmake/*/staged/dist; \
    else \
        make cmake-build && \
        mv build/cmake/*/ballisticakit build/cmake/*/staged/; \
    fi && \
    mv build/cmake/*/staged/* /ballistica_cmake

#-------------------------------RUNNER--------------------------------
# Create a new stage for the runtime environment using ARM64 base image
FROM --platform=linux/arm64 ${base_image}

# Environment settings
ENV LANG en_US.utf8
ENV LANGUAGE=en_US
ENV DEBIAN_FRONTEND=noninteractive

# Renew the ARGs
ARG headless_build
ENV HEADLESS_BUILD=${headless_build}
ARG cmake_build_type
LABEL BOMBSQUAD_BUILD_TYPE=${cmake_build_type}

# Install necessary tools to create users and groups
RUN apt-get update -y && apt-get install -y adduser

# Pre-create necessary users and groups to avoid systemd configuration errors
RUN addgroup --system systemd-journal && \
    adduser --system --no-create-home --ingroup systemd-journal systemd-network

# Configure dpkg to skip systemd post-installation scripts
RUN mkdir -p /etc/systemd/system && \
    echo "exit 0" > /usr/sbin/policy-rc.d && chmod +x /usr/sbin/policy-rc.d && \
    apt-get update -y && \
    apt-get install -y \
        python3.12-dev && \
    if [ "$HEADLESS_BUILD" = "0" ]; then \
        apt-get install -y \
            libsdl2-dev \
            libvorbis-dev \
            libglut-dev \
            libopenal-dev; \
    fi && \
    rm -rf /var/lib/apt/lists/* && \
    python3.12 -c "import uuid;print(uuid.uuid4())" > /etc/machine-id && \
    rm /usr/sbin/policy-rc.d

# Copy the compiled application from the builder stage
COPY --from=builder /ballistica_cmake \
    /home/ubuntu/ballistica

    # Set the working directory
WORKDIR /home/ubuntu/ballistica

RUN ln -s ballisticakit* run

# Expose the necessary port
EXPOSE 43210/udp

# Set the default command to run the application
CMD [ "./run" ]