FROM python:3.11-slim

# ── system deps ───────────────────────────────────────────────────────────────
# libusb-1.0 + udev needed for RTL-SDR USB access
# dump1090-fa from FlightAware PPA (Debian/Ubuntu compatible)
RUN apt-get update && apt-get install -y --no-install-recommends \
    libpq-dev gcc python3-dev \
    libusb-1.0-0 udev \
    wget gnupg ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# ── rtl-sdr-blog driver (RTL-SDR Blog V4 / R828D tuner) ──────────────────────
# The standard rtl-sdr package does NOT support V4. We install the rtl-sdr-blog
# fork which adds R828D support and bias-T control.
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential cmake pkg-config \
    libusb-1.0-0-dev \
    git \
    && rm -rf /var/lib/apt/lists/*

RUN git clone https://github.com/rtlsdrblog/rtl-sdr-blog.git /tmp/rtl-sdr-blog \
    && cmake -S /tmp/rtl-sdr-blog -B /tmp/rtl-sdr-blog/build \
        -DINSTALL_UDEV_RULES=ON \
        -DDETACH_KERNEL_DRIVER=ON \
    && cmake --build /tmp/rtl-sdr-blog/build --parallel $(nproc) \
    && cmake --install /tmp/rtl-sdr-blog/build \
    && ldconfig \
    && rm -rf /tmp/rtl-sdr-blog

# ── dump1090-fa (FlightAware fork — best SBS-1 output support) ───────────────
# Build from source: works on any Debian/Ubuntu without PPA
RUN apt-get update && apt-get install -y --no-install-recommends \
    libncurses-dev \
    && rm -rf /var/lib/apt/lists/*

RUN git clone --depth 1 https://github.com/flightaware/dump1090.git /tmp/dump1090 \
    && make -C /tmp/dump1090 -j$(nproc) \
    && cp /tmp/dump1090/dump1090 /usr/local/bin/dump1090-fa \
    && rm -rf /tmp/dump1090

# ── python app ────────────────────────────────────────────────────────────────
WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY main.py .

# dump1090 JSON output dir
RUN mkdir -p /tmp/dump1090-json

CMD ["python", "-u", "main.py"]
