FROM golang:1.26 AS build

ARG VERSION

# Install dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libsecret-1-dev \
    libfido2-dev \
    libcbor-dev \
    pass

# Build
ADD https://github.com/ProtonMail/proton-bridge.git#${VERSION} /build/proton-bridge/
WORKDIR /build/proton-bridge/
RUN sed -i 's/127.0.0.1/0.0.0.0/g' internal/constants/constants.go
RUN set -eux; \
    make build-nogui vault-editor

FROM ubuntu:latest
LABEL maintainer="Xiaonan Shen <s@sxn.dev>"

EXPOSE 1025/tcp
EXPOSE 1143/tcp

# Install dependencies and protonmail bridge
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        pass libsecret-1-0 ca-certificates libfido2-1 \
        dbus dbus-x11 gnupg2 \
    && rm -rf /var/lib/apt/lists/*

# Copy bash scripts
COPY gpgparams entrypoint.sh /protonmail/

# Copy protonmail
WORKDIR /usr/bin/
COPY --from=build /build/proton-bridge/bridge /usr/bin/
COPY --from=build /build/proton-bridge/proton-bridge /usr/bin/
COPY --from=build /build/proton-bridge/vault-editor /usr/bin/

ENTRYPOINT ["bash", "/protonmail/entrypoint.sh"]
