FROM ubuntu:24.04

# Qt libs + virtual framebuffer for headless ODA File Converter
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        python3 python3-pip wget ca-certificates \
        xvfb libxcb-xinerama0 libxcb-icccm4 libxcb-image0 \
        libxcb-keysyms1 libxcb-randr0 libxcb-render-util0 \
        libxcb-shape0 libxcb-xkb1 libxkbcommon0 libxkbcommon-x11-0 \
        libglib2.0-0 libgl1 libfontconfig1 libfreetype6 && \
    pip3 install --no-cache-dir --break-system-packages flask && \
    apt-get purge -y python3-pip && \
    apt-get autoremove -y

# Download and install ODA File Converter
RUN wget -q "https://www.opendesign.com/guestfiles/get?filename=ODAFileConverter_QT6_lnxX64_8.3dll_27.1.deb" \
        -O /tmp/oda.deb && \
    dpkg -i /tmp/oda.deb || apt-get install -f -y && \
    rm /tmp/oda.deb && \
    rm -rf /var/lib/apt/lists/* && \
    # Find the binary and symlink to /usr/local/bin for PATH access
    find / -name "ODAFileConverter" -type f -executable 2>/dev/null | head -1 | \
        xargs -I{} ln -sf {} /usr/local/bin/ODAFileConverter

WORKDIR /app
COPY app.py .

EXPOSE 5001

HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
    CMD python3 -c "import urllib.request; urllib.request.urlopen('http://localhost:5001/health')" || exit 1

CMD ["python3", "app.py"]
