# Builds a minimal debian image that installs the hbbs/hbbr/utils .deb from a
# Gitea Actions artifact and runs it. Both services share the same image; only
# the entrypoint command differs.
#
# Settings below are baked in as defaults. To override a value without editing
# this file, export it in your shell or put it in .env, e.g.:
#   RUSTDESK_DOMAIN=rd.example.com docker compose up -d
#
# To pin a specific artifact zip (skip API auto-discovery), set ARTIFACT_URL:
#   ARTIFACT_URL=https://gitea.cstudio.ch/mike/rustdesk-server/actions/runs/173/artifacts/rustdesk-server-linux-amd64-1e961cdd929f7af97148b76d9de79998a89402a3 \
#     docker compose build
#
# To pick up a newer artifact, bump the cache buster:
#   CACHE_BUST=$(date +%s) docker compose build

networks:
  rustdesk-net:
    external: false

# Build args — passed to docker/Dockerfile.deb. ARTIFACT_URL, if set, short-
# circuits the API discovery and downloads the zip directly.
x-rustdesk-build: &rustdesk-build
  context: ./docker
  dockerfile: Dockerfile.deb
  args:
    GITEA_URL: "${GITEA_URL:-https://gitea.cstudio.ch}"
    GITEA_OWNER: "${GITEA_OWNER:-mike}"
    GITEA_REPO: "${GITEA_REPO:-rustdesk-server}"
    GITEA_BRANCH: "${GITEA_BRANCH:-pro-features}"
    ARTIFACT_URL: "${ARTIFACT_URL:-}"
    CACHE_BUST: "${CACHE_BUST:-0}"

# Runtime env that hbbs/hbbr actually read. Most settings (relay, bootstrap
# admin, key, http port) are passed via CLI flags below — the binary's
# env-var convention transforms `--foo-bar` into `FOO-BAR` (literal dashes,
# uppercase), which is awkward in YAML, so flags are clearer. The names
# below are the few that the binary reads as plain env vars.
x-rustdesk-env: &rustdesk-env
  RUST_LOG: "${RUST_LOG:-info}"
  # Force relay for all sessions even on LAN. Uncomment to enable.
  # ALWAYS_USE_RELAY: "Y"
  # Override DB path. Default: ./db_v2.sqlite3 in WORKDIR
  # (= /var/lib/rustdesk-server/db_v2.sqlite3 in this image).
  # DB_URL: "/var/lib/rustdesk-server/db_v2.sqlite3"

services:
  hbbs:
    container_name: hbbs
    build: *rustdesk-build
    image: rustdesk-server-cst:latest
    platform: linux/amd64
    command:
      - hbbs
      - --bootstrap-admin-username=${RUSTDESK_BOOTSTRAP_ADMIN_USERNAME:-admin}
      - --bootstrap-admin-password=${RUSTDESK_BOOTSTRAP_ADMIN_PASSWORD:-changeme}
      # - --key=-                # "-" auto-generates a key; "_" forces encrypted-only with no explicit key
      # - --http-port=21114      # admin HTTP API/UI port; 0 disables
      # When the admin UI shows a device's unattended password.
      # logged-out (default) = only when nobody is logged in; always = also while a user is logged in.
      - --unattended-pwd-visibility=${RUSTDESK_UNATTENDED_PWD_VISIBILITY:-logged-out}
    environment: *rustdesk-env
    ports:
      - 21114:21114
      - 21115:21115
      - 21116:21116
      - 21116:21116/udp
      - 21118:21118
    volumes:
      - ./data:/var/lib/rustdesk-server
    networks:
      - rustdesk-net
    depends_on:
      - hbbr
    restart: unless-stopped

  hbbr:
    container_name: hbbr
    # Same build + image tag as hbbs — compose builds once and both reuse it.
    build: *rustdesk-build
    image: rustdesk-server-cst:latest
    platform: linux/amd64
    command:
      - hbbr
      # - --key=-                # match the key set on hbbs (if any)
    environment: *rustdesk-env
    ports:
      - 21117:21117
      - 21119:21119
    volumes:
      - ./data:/var/lib/rustdesk-server
    networks:
      - rustdesk-net
    restart: unless-stopped
