# Librespot (Spotify Connect) for ARM64
# Multi-stage Docker build. Run on ARM64 host (RPi 3B+ etc).
# Audio output: PulseAudio (connects to audio-bridge via TCP localhost:4713).

FROM rust:1.85.1-bookworm AS builder

RUN apt-get update \
 && apt-get install -y --no-install-recommends \
      libpulse-dev \
      libasound2-dev \
 && rm -rf /var/lib/apt/lists/*

ARG LIBRESPOT_VERSION=v0.8.0
RUN git clone --depth 1 --branch ${LIBRESPOT_VERSION} \
        https://github.com/librespot-org/librespot.git /src
WORKDIR /src

# Limit parallelism to 2 (RPi 3B+ has 4 cores but only 1GB RAM).
# Lower = less memory pressure, slower build. Higher = OOM risk.
# Features: pulseaudio (audio out) + native-tls (TLS) + with-libmdns (Spotify Connect
# discovery via mDNS — required for the device to appear in phone Spotify apps).
RUN cargo build --release --jobs 2 \
    --no-default-features \
    --features="pulseaudio-backend,native-tls,with-libmdns"

FROM debian:bookworm-slim

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

COPY --from=builder /src/target/release/librespot /usr/local/bin/librespot

VOLUME ["/var/cache/librespot"]
ENV PULSE_SERVER=tcp:127.0.0.1:4713

CMD ["librespot", \
     "--name", "Klubhaus", \
     "--backend", "pulseaudio", \
     "--bitrate", "320", \
     "--initial-volume", "100", \
     "--cache", "/var/cache/librespot"]
