FROM --platform=linux/amd64 ubuntu:latest

RUN apt-get update && apt-get install -y curl xz-utils

ARG CALIBRE_RELEASE="7.1.0"

RUN curl -o /tmp/calibre-tarball.txz -L "https://download.calibre-ebook.com/${CALIBRE_RELEASE}/calibre-${CALIBRE_RELEASE}-x86_64.txz" && \
    mkdir -p /opt/calibre && \
    tar xvf /tmp/calibre-tarball.txz -C /opt/calibre && \
    rm -rf /tmp/*


FROM --platform=linux/amd64 debian:bookworm-slim

RUN apt-get update && apt-get install -y \
    dnsutils \
    iproute2 \
    libfontconfig \
    libgl1 \
    libegl1 \
    libxkbcommon-x11-0 \
    libopengl0 \
    libnss3-dev \
    libxcomposite-dev \
    libxdamage1 \
    libxrandr2 \
    libxtst6 \
    libxkbfile-dev \
    xdg-utils \
    && rm -rf /var/lib/apt/lists/*
COPY --from=0 /opt/calibre /opt/calibre
RUN mkdir /usr/share/desktop-directories/
RUN /opt/calibre/calibre_postinstall && \
    mkdir /opt/calibre/calibre-library && \
    touch /opt/calibre/calibre-library/metadata.db

COPY server-users.sqlite /opt/calibre/calibre-library/server-users.sqlite

COPY start-calibre-server.sh .

EXPOSE 8080
CMD [ "/start-calibre-server.sh" ]
