FROM docker.io/archlinux

ARG AUR_FOLDER
ARG AUR_REPO
ARG AUR_KEY
ARG AUR_USER
ARG COMMIT_USERNAME
ARG COMMIT_EMAIL
ARG COMMIT_MESSAGE

RUN pacman -Sy --noconfirm base-devel curl openssh git pacman-contrib

RUN useradd -m ${AUR_USER}

USER ${AUR_USER}

WORKDIR /home/${AUR_USER}

RUN mkdir .ssh
RUN curl https://gitlab.archlinux.org/archlinux/infrastructure/-/raw/master/docs/ssh-known_hosts.txt?inline=false > .ssh/known_hosts
RUN echo ${AUR_KEY} | base64 -d > .ssh/id_rsa
RUN chmod 0400 .ssh/id_rsa

RUN git config --global user.name ${COMMIT_USERNAME}
RUN git config --global user.email ${COMMIT_EMAIL}
RUN git clone ${AUR_REPO} aur

WORKDIR /home/${AUR_USER}/aur

RUN rm -rf *
COPY --chown=${AUR_USER}:${AUR_USER} ${AUR_FOLDER}/* ./
RUN ls -la

RUN updpkgsums
RUN makepkg --printsrcinfo > .SRCINFO

RUN git add .
RUN git diff --staged --quiet || git commit -m "${COMMIT_MESSAGE}"
RUN git push
