# Optimization workflow: GA/PSO with OPM Flow
# Run from workflow/: docker compose up -d
FROM ubuntu:24.04

# License notes:
#   Application code: Apache-2.0
#   OPM Flow binaries (libopm-simulators*): GPL-3.0-or-later — bundled as a
#   standalone external executable invoked via subprocess; not linked into the
#   application. GPL does not propagate across the process boundary.
#   See /usr/share/licenses/opm/NOTICE.txt (in image) for source offer.
LABEL org.opencontainers.image.licenses="Apache-2.0 AND GPL-3.0-or-later"

ENV DEBIAN_FRONTEND=noninteractive
ENV PYTHONUNBUFFERED=1
ENV PIP_PREFER_BINARY=1

WORKDIR /workspace

# System deps for OPM Flow
RUN apt-get update && apt-get install -y --no-install-recommends \
    software-properties-common \
    gnupg \
    git \
    wget \
    curl \
    ca-certificates \
    python3 \
    python3-venv \
    python3-dev \
    python3-pip \
    && add-apt-repository ppa:opm/ppa \
    && apt-get update \
    && apt-get install -y --no-install-recommends \
        libopm-simulators-bin \
        libopm-simulators \
    && rm -rf /var/lib/apt/lists/* \
    # GPL-3.0 compliance: embed notice and source pointer for OPM binaries
    && mkdir -p /usr/share/licenses/opm \
    && printf 'OPM Flow Simulator -- GPL-3.0-or-later Notice\n\nPackages: libopm-simulators, libopm-simulators-bin\nSource:   https://github.com/OPM/opm-simulators\nLicense:  GNU General Public License v3.0 or later (SPDX: GPL-3.0-or-later)\n\nCopyright (C) 2011-2024 SINTEF Digital, Mathematics & Cybernetics\nCopyright (C) 2011-2024 Equinor ASA\n\nFull license text: https://www.gnu.org/licenses/gpl-3.0.html\n\nSource (GPL-3.0 s.6d written offer, valid 3 years from image build):\n  https://github.com/OPM/opm-simulators\n  https://github.com/OPM/opm-common\n  https://github.com/OPM/opm-grid\n  https://github.com/OPM/opm-models\n' > /usr/share/licenses/opm/NOTICE.txt

# Copy workflow (build context is workflow dir)
COPY . /workspace/workflow

WORKDIR /workspace/workflow/optimization
RUN python3 -m venv /opt/venv && \
    /opt/venv/bin/pip install --upgrade pip && \
    /opt/venv/bin/pip install -r requirements.txt

ENV PATH="/opt/venv/bin:$PATH"

# When OPM_FLOW_EXTRA_ARGS is set (e.g. in docker-compose), wrap `flow`
RUN echo '' >> /root/.bashrc && \
    echo 'flow() { command flow "$@" $OPM_FLOW_EXTRA_ARGS; }' >> /root/.bashrc

WORKDIR /workspace/workflow

CMD ["python", "optimization/src/run_optimization.py", "--help"]
