# Use Debian 12 Slim as the base image
FROM debian:12-slim

# Set environment variables for non-interactive install
ENV DEBIAN_FRONTEND=noninteractive TZ=Etc/UTC

# Arguments for user and group ID
ARG UID=1000
ARG GID=1000

# Install necessary tools: Flatpak, builder, git, ccache, etc.
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    flatpak \
    flatpak-builder \
    git \
    curl \
    unzip \
    ca-certificates \
    elfutils \
    ccache \
    # Build dependencies for system libraries
    build-essential \
    ninja-build \
    pkg-config \
    python3 \
    # Clean up apt cache
    && rm -rf /var/lib/apt/lists/*

# Add Flathub remote and install the Freedesktop SDK and Platform
# Using 23.08 version as specified in the manifest
RUN flatpak remote-add --system --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo && \
    flatpak install --system --noninteractive flathub org.freedesktop.Sdk//23.08 && \
    flatpak install --system --noninteractive flathub org.freedesktop.Platform//23.08

# Create a non-root user for the build process
RUN groupadd -g $GID builder && \
    useradd -u $UID -g $GID -ms /bin/bash builder && \
    mkdir -p /home/builder/.cache/ccache && \
    chown -R builder:builder /home/builder

# Create directories needed for the build and set ownership
# /sources will be mounted from host, /build is for build artifacts
RUN mkdir -p /sources /build && \
    chown -R builder:builder /build

# Set user and home directory
USER builder
ENV HOME /home/builder
ENV CCACHE_DIR /home/builder/.cache/ccache
ENV PATH /usr/lib/ccache:$PATH

# Set working directory for the build process
WORKDIR /build

# Copy the Flatpak manifest into the build directory (owned by builder due to WORKDIR and USER settings)
COPY --chown=builder:builder com.aseprite.Aseprite.yaml .

# Default command for interactive use
CMD ["bash"]
