# Multi-stage build: clones rustdesk-server from a Git remote and builds the
# hbbs / hbbr / rustdesk-utils binaries from source.
#
# Build args:
#   RUSTDESK_GIT_URL    Git URL to clone (default: gitea.cstudio.ch fork)
#   RUSTDESK_GIT_BRANCH Branch / tag / ref to check out (default: pro-features)
#   RUST_VERSION        Rust toolchain image tag (default: 1-bookworm)

ARG RUST_VERSION=1-bookworm

FROM rust:${RUST_VERSION} AS builder

ARG RUSTDESK_GIT_URL=https://gitea.cstudio.ch/mike/rustdesk-server.git
ARG RUSTDESK_GIT_BRANCH=pro-features
# sqlx::query! macros verify SQL at compile time against the checked-in
# db_v2.sqlite3. Override only if you point cargo at a different DB.
ARG DATABASE_URL=

RUN apt-get update \
 && apt-get install -y --no-install-recommends git ca-certificates pkg-config cmake \
 && rm -rf /var/lib/apt/lists/*

WORKDIR /src

RUN git clone --recurse-submodules --shallow-submodules \
        --branch "${RUSTDESK_GIT_BRANCH}" --single-branch \
        "${RUSTDESK_GIT_URL}" .

RUN if [ -n "${DATABASE_URL}" ]; then export DATABASE_URL="${DATABASE_URL}"; fi \
 && cargo build --release --bins

# Runtime stage
FROM debian:bookworm-slim

RUN apt-get update \
 && apt-get install -y --no-install-recommends ca-certificates tini \
 && rm -rf /var/lib/apt/lists/*

COPY --from=builder /src/target/release/hbbs           /usr/local/bin/hbbs
COPY --from=builder /src/target/release/hbbr           /usr/local/bin/hbbr
COPY --from=builder /src/target/release/rustdesk-utils /usr/local/bin/rustdesk-utils
COPY --from=builder /src/admin_ui                       /opt/rustdesk/admin_ui

WORKDIR /root
EXPOSE 21114 21115 21116 21116/udp 21117 21118 21119

ENTRYPOINT ["/usr/bin/tini", "--"]
CMD ["hbbs"]
