FROM docker.io/rust:1-alpine3.20 AS builder

RUN apk --no-cache add \
  git \
  gcc \
  protoc \
  protobuf-dev \
  musl-dev

ENV CARGO_CARGO_NEW_VCS="none"
ENV CARGO_BUILD_RUSTFLAGS="-C target-feature=+crt-static"

RUN echo "atuin:x:2222:2222:Linux User,,,:/atuin:/atuin" > /passwd
RUN mkdir /atuin

ENV VERSION=v18.3.0

RUN git clone https://github.com/atuinsh/atuin.git /work
WORKDIR /work
RUN git checkout $VERSION

RUN cargo build --release --target=$(arch)-unknown-linux-musl
RUN cp "./target/$(arch)-unknown-linux-musl/release/atuin" /app


FROM scratch

COPY --from=builder --chown=2222:2222 /atuin /atuin
COPY --from=builder /passwd /etc/passwd

ENV ATUIN_HOST="0.0.0.0"
ENV ATUIN_PORT=8888
ENV ATUIN_OPEN_REGISTRATION=false
ENV RUST_LOG=info,atuin_server=debug

USER atuin
COPY --from=builder /app /app
CMD ["/app", "server", "start"]

EXPOSE 8888