ARG BASE_IMAGE=texlive/texlive:latest
FROM ${BASE_IMAGE}

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

ENV DEBIAN_FRONTEND=noninteractive \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PYTHONUNBUFFERED=1

RUN command -v apt-get >/dev/null 2>&1 || { echo 'texlive/texlive-console:pro requires a Debian/Ubuntu-compatible texlive/texlive base image with apt-get.' >&2; exit 1; }; \
    apt-get update; \
    apt-get install -y --no-install-recommends \
      bash ca-certificates curl wget git \
      python3 python3-pip python3-venv \
      gcc g++ make cmake \
      zip unzip tar jq; \
    if ! apt-cache show gh >/dev/null 2>&1; then \
      install -d -m 0755 /etc/apt/keyrings; \
      curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg -o /etc/apt/keyrings/githubcli-archive-keyring.gpg; \
      chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg; \
      printf 'deb [arch=%s signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main\n' "$(dpkg --print-architecture)" > /etc/apt/sources.list.d/github-cli.list; \
      apt-get update; \
    fi; \
    apt-get install -y --no-install-recommends gh; \
    rm -rf /var/lib/apt/lists/*

COPY requirements-scientific.txt /tmp/requirements-scientific.txt
RUN python3 -m venv /opt/texise-venv; \
    /opt/texise-venv/bin/python -m pip install --no-cache-dir --upgrade pip setuptools wheel; \
    /opt/texise-venv/bin/python -m pip install --no-cache-dir --only-binary=:all: -r /tmp/requirements-scientific.txt; \
    /opt/texise-venv/bin/python -c 'import numpy,scipy,pandas,matplotlib,sympy'; \
    rm -f /tmp/requirements-scientific.txt

ENV VIRTUAL_ENV=/opt/texise-venv \
    PATH=/opt/texise-venv/bin:${PATH}

# Login shells may rebuild PATH from /etc/profile. Re-assert the TeXise virtual
# environment at the profile layer so python3/pip remain the scientific runtime
# in interactive Linux Terminal sessions.
RUN printf '%s\n' \
      'export VIRTUAL_ENV=/opt/texise-venv' \
      'case ":$PATH:" in *":/opt/texise-venv/bin:"*) ;; *) export PATH="/opt/texise-venv/bin:$PATH" ;; esac' \
      > /etc/profile.d/texise-venv.sh; \
    chmod 0644 /etc/profile.d/texise-venv.sh

RUN command -v bash git gh python3 gcc g++ make cmake zip unzip tar curl wget jq pdflatex xelatex lualatex biber latexmk >/dev/null; \
    python3 -m pip --version >/dev/null; \
    python3 -m venv --help >/dev/null; \
    python3 -c 'import sys,numpy,scipy,pandas,matplotlib,sympy; assert sys.version_info >= (3, 9)'

LABEL com.texise.image.role="linux-terminal-pro" \
      com.texise.image.schema="r14.28.14-bf12" \
      com.texise.image.base="texlive/texlive:latest"

WORKDIR /workspace
ENTRYPOINT ["/bin/bash"]
CMD ["-l"]
