# gdo, the mailer, built from uhhm/gdo at GDO_RELEASE: a tag, or a commit.
#
# Built here rather than installed from the [uhhm] Arch registry, as it
# was while the site ran portal 0.19: the gdo that dials a portal 1.0
# site (0.4, which reads no NATS) is not in the registry yet, and is
# held back from it on purpose. A host that upgraded to it while it
# still has a 0.x site would stop sending that site's mail. Once 0.4 is
# published, this can install the package again (uhhm-registry.asc
# beside this file is the registry's key, kept for then).
#
# Arch for both stages, because that is what every uhhm binary is built
# on and run on: the runtime's glibc is then never older than the
# builder's.
FROM archlinux:base AS build
ARG GDO_RELEASE
# -Syu, never -Sy: a partial upgrade pulls a library newer than what the
# base image's own pacman was built against and refuses to install.
RUN pacman -Syu --noconfirm --needed git rust gcc \
 && pacman -Scc --noconfirm
WORKDIR /src
# The ref by name, so a tag and a commit both do; --locked, so the build
# is the one the repo's own lock file describes.
RUN test -n "$GDO_RELEASE" \
 && git init -q . \
 && git fetch -q --depth 1 https://project.uhhm.no/uhhm/gdo.git "$GDO_RELEASE" \
 && git checkout -q FETCH_HEAD \
 && cargo build --release --locked \
 && install -Dm755 target/release/gdo /out/gdo

FROM archlinux:base
RUN pacman -Syu --noconfirm --needed ca-certificates \
 && pacman -Scc --noconfirm \
 && rm -rf /var/cache/pacman/pkg/* /var/lib/pacman/sync/*
COPY --from=build /out/gdo /usr/bin/gdo

# A non-root user. gdo keeps its key on dhd/1 in /var/lib/gdo, a volume
# (GDO_KEY in compose.yml); what it has not yet delivered waits with the
# site, not here.
RUN useradd --system --no-create-home gdo \
 && mkdir -p /var/lib/gdo && chown gdo /var/lib/gdo && chmod 700 /var/lib/gdo
USER gdo
CMD ["/usr/bin/gdo"]
