FROM docker.io/archlinux/archlinux:latest

# Update pacman dtaabase
# Initialize pacman key
# Ensure archlinux-keyring package is up to date
# Update all packages

RUN pacman -Sy && \
    pacman-key --init && \
    pacman -S --noconfirm archlinux-keyring && \
    pacman -Su --noconfirm

# Install packages required for cloning and building the aur-utils repo

RUN pacman -S --noconfirm base-devel git curl jq man-db man-pages nano

# Install additional aurutils dependencies
# https://github.com/aurutils/aurutils/blob/release/makepkg/PKGBUILD#L14

RUN pacman -S --noconfirm pacutils bash perl perl-json-xs

# Copy in makepkg config file, aur sudo ocnfig, custom repo config, and include custom repo config in main pacman config

COPY --chown=root:root --chmod=0664 makepkg.conf /etc/makepkg.conf.d/makepkg.conf
COPY --chown=root:root --chmod=0600 aur.sudo /etc/sudoers.d/aur
COPY --chown=root:root --chmod=0664 home.repo.conf /etc/pacman.d/home.repo.conf
# Append text block to end of pacman.conf
RUN cat <<EOF >> /etc/pacman.conf

[home]
Include = /etc/pacman.d/home.repo.conf

EOF

# Create the aur system user

RUN useradd -r -U -m -d /aur aur && \
    mkdir /repo && \
    chmod 0775 /repo && \
    chown aur:aur /repo

# Switch to aur user for build operations

USER aur

# Git clone the aur-utils repo
# Checkout the latest release, fetching latest release version from github API via curl

# 2026-07-02 switch source from GitHub to AUR
# RUN git clone https://github.com/aurutils/aurutils.git /aur/aurutils && \
RUN git clone https://aur.archlinux.org/aurutils.git /aur/aurutils
    # cd /aur/aurutils && \
    # cd /aur/aurutils 
    # 2026-07-02 commenting out as switching git source to AUR instead of github
    # git switch --detach $(curl -sSfL https://api.github.com/repos/aurutils/aurutils/releases/latest | jq -r .tag_name)

# Ensure makepkg dirs exist

RUN mkdir -p /aur/{builddir,srcdest,srcpkgdest,log}

# Init the home repo db
# Update pacman sources

RUN repo-add /repo/home.db.tar.zst && \
    sudo pacman -Sy

# Build the aurutils package

RUN makepkg --nosign -i -s --noconfirm -D /aur/aurutils 
# RUN makepkg --nosign -i --noconfirm -D /aur/aurutils/makepkg

RUN rm -f /repo/aurutils*.pkg.tar.zst && \
    rm -r /aur/aurutils

# Set working directory and entrypoint

WORKDIR /aur
ENV PATH=/usr/local/bin:/usr/bin
ENTRYPOINT ["/bin/bash"]
