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 via script in /app (keeps Dockerfile small)
COPY build_log4qt.sh /app/build_log4qt.sh
RUN /app/build_log4qt.sh


# 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"]
