FROM debian:bookworm-slim

# hfsprogs (mkfs.hfsplus) liegt in Debians "non-free"-Bereich, das offizielle
# bookworm-slim-Image aktiviert davon aber nur "main". Eigene sources.list.d
# Datei ergaenzt non-free, unabhaengig davon ob die Basis-Sources im alten
# Ein-Zeilen- oder im neuen deb822-Format vorliegen (apt liest beide parallel).
RUN echo "deb http://deb.debian.org/debian bookworm main contrib non-free non-free-firmware" \
    > /etc/apt/sources.list.d/non-free.list

RUN apt-get update && apt-get install -y --no-install-recommends \
    python3 python3-pip \
    util-linux \
    gdisk \
    hfsprogs \
    dmg2img \
    cpio \
    xz-utils liblzma-dev \
    libxml2-dev libssl-dev zlib1g-dev \
    autoconf automake libtool pkg-config \
    git build-essential \
    ca-certificates curl \
    && rm -rf /var/lib/apt/lists/*

# pbzx: entpackt Apples pbzx-komprimierte Installer-Payloads (macOS Big Sur+).
# Bewusst aus dem gepflegten Upstream-Repo gebaut statt selbst nachgebaut --
# das Binaerformat ist reverse-engineered und ein fertiges, getestetes Tool
# ist zuverlaessiger als eine selbstgeschriebene Parser-Vermutung.
RUN git clone --depth 1 https://github.com/mackyle/pbzx.git /tmp/pbzx \
    && cd /tmp/pbzx && make && install -m 755 pbzx /usr/local/bin/pbzx \
    && rm -rf /tmp/pbzx

# xar: existiert als Debian-Paket erst ab trixie (bookworm hat es gar nicht
# im Archiv, auch nicht in non-free). Statt die ganze Basis-Distro zu wechseln
# (Risiko fuer alle anderen Pakete) bauen wir es aus dem selben, von Apple-
# Tooling-Communities genutzten Upstream-Fork wie pbzx (gleicher Maintainer).
RUN git clone --depth 1 https://github.com/mackyle/xar.git /tmp/xar-src \
    && cd /tmp/xar-src/xar \
    && ./autogen.sh && ./configure && make && make install \
    && ldconfig \
    && rm -rf /tmp/xar-src

WORKDIR /app
COPY backend/requirements.txt /app/backend/requirements.txt
RUN pip3 install --no-cache-dir --break-system-packages -r /app/backend/requirements.txt

COPY backend /app/backend
COPY frontend /app/frontend

EXPOSE 8080
ENV PYTHONUNBUFFERED=1
CMD ["python3", "/app/backend/app.py"]
