FROM ubuntu:24.04

ENV DEBIAN_FRONTEND=noninteractive \
    PYTHONUNBUFFERED=1 \
    PIP_NO_CACHE_DIR=1 \
    LANG=C.UTF-8 \
    LC_ALL=C.UTF-8 \
    QT_VERSION=6.8.3 \
    QT_ARCH=linux_gcc_64 \
    QT_BASE_DIR=/opt/Qt \
    QT_ROOT=/opt/Qt/6.8.3/gcc_64 \
    Qt6_ROOT=/opt/Qt/6.8.3/gcc_64 \
    CMAKE_PREFIX_PATH=/usr/local:/opt/Qt/6.8.3/gcc_64 \
    PATH=/opt/Qt/6.8.3/gcc_64/bin:$PATH

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

WORKDIR /app

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        ca-certificates \
        sudo \
        git \
        cmake \
        ninja-build \
        clang \
        build-essential \
        gnupg \
        curl \
        jq \
        openssh-client \
        pkg-config \
        libgl1-mesa-dev \
        libxkbcommon-x11-0 \
        libfontconfig1 \
        libdbus-1-3 \
        libpulse0 \
        libxcb-glx0 \
        libxcb-cursor0 \
        libpq-dev \
        libbz2-dev \
        libpoppler-dev \
        libpoppler-qt6-dev \
        python3 \
        python3-venv \
        python3-pip \
    && ln -sf /usr/bin/python3 /usr/local/bin/python3 \
    && ln -sf /usr/bin/python3 /usr/local/bin/python \
    && rm -rf /var/lib/apt/lists/*

COPY requirements.txt /app/requirements.txt

RUN python3 -m venv /opt/venv \
    && /opt/venv/bin/pip install --upgrade pip setuptools wheel \
    && /opt/venv/bin/pip install -r /app/requirements.txt aqtinstall \
    && mkdir -p "${QT_BASE_DIR}" \
    && /opt/venv/bin/python -m aqt install-qt linux desktop "${QT_VERSION}" "${QT_ARCH}" -m qtmultimedia qtserialport qtimageformats qtwebsockets qtwebengine qtpdf qtwebview --outputdir "${QT_BASE_DIR}" \
    && test -f "${QT_ROOT}/lib/cmake/Qt6/Qt6Config.cmake"

# Build and install Log4Qt from source (provides headers + shared libs)
RUN set -eux; \
    git clone --depth 1 https://github.com/MEONMedical/Log4Qt.git /tmp/log4qt; \
    mkdir -p /tmp/log4qt/build; \
    cd /tmp/log4qt/build; \
    cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_STANDARD=20 -DCMAKE_CXX_EXTENSIONS=OFF -DCMAKE_PREFIX_PATH="${QT_ROOT}" -DCMAKE_INSTALL_PREFIX=/usr/local ..; \
    cmake --build . -- -j$(nproc); \
    cmake --install .; \
    # create a minimal CMake config file so find_package(Log4Qt) finds the installed library
    mkdir -p /usr/local/lib/cmake/Log4Qt; \
    libpath="$((ls /usr/local/lib/libLog4Qt.so* 2>/dev/null || ls /usr/local/lib/liblog4qt.so* 2>/dev/null) | head -n1 || true)"; \
    if [ -n "$libpath" ]; then \
      printf '%s\n' \
        '# Minimal Log4QtConfig.cmake created by Dockerfile' \
        'if (NOT TARGET Log4Qt::log4qt)' \
        '  add_library(Log4Qt::log4qt UNKNOWN IMPORTED)' \
        '  set_target_properties(Log4Qt::log4qt PROPERTIES' \
        "    IMPORTED_LOCATION \"${libpath}\"" \
        '    INTERFACE_INCLUDE_DIRECTORIES "/usr/local/include"' \
        '  )' \
        'endif()' \
        'set(Log4Qt_FOUND TRUE CACHE INTERNAL "Log4Qt found by package config")' \
        'set(Log4Qt_INCLUDE_DIRS "/usr/local/include" CACHE INTERNAL "Log4Qt include dir")' \
        "set(Log4Qt_LIBRARIES \"${libpath}\" CACHE INTERNAL \"Log4Qt library\")" \
      > /usr/local/lib/cmake/Log4Qt/Log4QtConfig.cmake; \
    fi; \
    rm -rf /tmp/log4qt

# Install additional external dependencies required by Lts modules
# Attempt to install system packages first; then build Grantlee and KDReports from source if not available
RUN set -eux; \
    apt-get update; \
    apt-get install -y --no-install-recommends \
      libgeographic-dev \
      libssl-dev \
      libxml2-dev \
      libicu-dev \
      libqt6svg6-dev \
      libqt6webkit6-dev || true; \
    # Try to install Grantlee / KDReports from apt if available; ignore failures
    apt-get install -y --no-install-recommends libgrantlee-qt5-dev libgrantlee-templates5-dev libkdreports-dev || true; \
    # Build Grantlee from source (KDE frameworks) if cmake config not available
    if ! ldconfig -p | grep -q grantlee || ! pkg-config --exists grantlee-templates; then \
      echo "Building Grantlee from source"; \
      git clone --depth 1 https://invent.kde.org/frameworks/grantlee.git /tmp/grantlee || true; \
      if [ -d /tmp/grantlee ]; then \
        mkdir -p /tmp/grantlee/build; cd /tmp/grantlee/build; \
        cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_PREFIX_PATH="${QT_ROOT}:/usr/local" ..; \
        cmake --build . -- -j$(nproc) || true; \
        cmake --install . || true; \
      fi; \
    fi; \
    # Build KDReports from source if not available
    if ! ldconfig -p | grep -q kdreports && [ ! -f /usr/local/lib/libKDReports.so ]; then \
      echo "Building KDReports from source"; \
      git clone --depth 1 https://invent.kde.org/sdk/kdreports/kdreports.git /tmp/kdreports || true; \
      if [ -d /tmp/kdreports ]; then \
        mkdir -p /tmp/kdreports/build; cd /tmp/kdreports/build; \
        cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_PREFIX_PATH="${QT_ROOT}:/usr/local" ..; \
        cmake --build . -- -j$(nproc) || true; \
        cmake --install . || true; \
      fi; \
    fi; \
    # geographiclib already installed via apt; RTI (Proprietary) cannot be installed automatically
    echo "Note: RTI Connext (RTI) is proprietary and must be installed manually into /opt/RTI if required."

COPY . /app

CMD ["python3.11", "run_migration.py", "--help"]
