FROM archlinux:base-devel

# Install dependencies
RUN pacman -Syu --noconfirm && \
    pacman -S --noconfirm \
    git \
    openssh \
    gnupg \
    python \
    python-pip \
    python-requests \
    python-yaml \
    python-gitpython \
    python-gitdb \
    python-pytest \
    python-pyfakefs \
    python-pytest-cov \
    namcap \
    rust \
    cargo \
    devtools \
    fakeroot \
    sudo \
    # Tools for non-interactive builds
    expect \
    # Common build dependencies that are frequently needed
    go \
    jdk-openjdk \
    nodejs \
    npm \
    cmake \
    qt5-base \
    qt6-base \
    php \
    perl \
    ruby \
    python-setuptools \
    python-wheel \
    python-pip \
    gcc \
    gcc-libs \
    autoconf \
    automake \
    bison \
    flex \
    patch \
    pkgconf \
    make \
    clang \
    llvm \
    # Additional build deps for problematic packages like gstreamer0.10
    intltool \
    gtk-doc \
    glib2 \
    glib2-devel \
    texinfo \
    doxygen && \
    # Clean up
    pacman -Scc --noconfirm && \
    rm -rf /var/cache/pacman/pkg/* && \
    rm -rf /var/lib/pacman/sync/*

# Create non-root user with specific UID/GID to match GitHub Actions
RUN useradd -u 1001 -m -s /bin/bash runner && \
    echo "runner ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers && \
    mkdir -p /github/workspace && \
    chown -R runner:runner /github/workspace

# Install paru (AUR helper)
RUN sudo -u runner bash -c "cd /tmp && \
    git clone https://aur.archlinux.org/paru-bin.git && \
    cd paru-bin && \
    makepkg -si --noconfirm" && \
    rm -rf /tmp/paru-bin

# Set up chroot build environment
RUN mkdir -p /var/cache/pacman/pkg && \
    chmod 777 /var/cache/pacman/pkg && \
    mkdir -p /var/lib/archbuild && \
    chmod 777 /var/lib/archbuild && \
    # Give runner ability to use sudo without password for specific commands
    echo "runner ALL=(ALL) NOPASSWD: /usr/bin/pacman, /usr/bin/makepkg, /usr/bin/chown, /usr/bin/chmod" >> /etc/sudoers.d/runner && \
    mkdir -p /build && \
    chown -R runner:runner /build && \
    # Configure pacman for non-interactive use with provider auto-selection
    echo "ProviderAutoSelection = always" >> /etc/pacman.conf

# Configure paru
RUN mkdir -p /home/runner/.config/paru && \
    echo '[bin]' > /home/runner/.config/paru/paru.conf && \
    echo 'FileManager = cat' >> /home/runner/.config/paru/paru.conf && \
    echo 'Redownload = false' >> /home/runner/.config/paru/paru.conf && \
    echo 'BatchInstall = true' >> /home/runner/.config/paru/paru.conf && \
    echo 'SudoLoop = true' >> /home/runner/.config/paru/paru.conf && \
    echo 'PacmanConf = /etc/pacman.conf' >> /home/runner/.config/paru/paru.conf && \
    echo 'CleanAfter = true' >> /home/runner/.config/paru/paru.conf && \
    echo 'NoConfirm = true' >> /home/runner/.config/paru/paru.conf && \
    echo 'KeepSrc = true' >> /home/runner/.config/paru/paru.conf && \
    echo '[build]' >> /home/runner/.config/paru/paru.conf && \
    echo 'NoCheck = true' >> /home/runner/.config/paru/paru.conf && \
    chown -R runner:runner /home/runner/.config

USER runner
WORKDIR /github/workspace

# Pre-configure git for container reuse
RUN git config --global --add safe.directory '*' 