## Start from an Ubuntu image that contains all MATLAB dependencies
# The version of Ubuntu should be the same as in matlab.mathworks.com
# (can be checked there with: !lsb_release -a).

# Declare MATLAB_VERSION in the global scope
ARG MATLAB_VERSION=R2026a

FROM mathworks/matlab-deps:${MATLAB_VERSION}-ubuntu24.04

# Redeclare MATLAB_VERSION in the build stage, inheriting its value from the global scope
ARG MATLAB_VERSION

## Install additional Debian packages

# Configure APT and upgrade base image
# – do not install recommends, to keep image minimal
# – do no keep downloaded packages, to keep image minimal (now the default for
#   apt but not for apt-get, see Debian bug #160743; and we prefer to use
#   apt-get since it is recommended for scripting)
# – run non-interactively and assume yes to all questions
# – update APT database
# – upgrade the packages of the base image
RUN { echo 'APT::Install-Recommends "false";'; \
    echo 'APT::Keep-Downloaded-Packages "false";'; \
    echo 'APT::Get::Assume-Yes "true";'; } > /etc/apt/apt.conf \
    && apt-get update \
    && apt-get dist-upgrade

# Install packages needed by Dynare jobs
RUN apt-get install \
# Basic git support
    git \
    git-lfs \
# The Dynare build dependencies, see README.md
# NB: we don’t need Octave dependencies (octave, libslicot-dev,
# libsuitesparse-dev), x13as (we use the upstream static binary), nor tools for
# building the documentation
# NB: we use libboost-graph-dev instead of libboost-dev, because of this meson bug:
# https://github.com/mesonbuild/meson/issues/15470
    gcc \
    g++ \
    gfortran \
    libboost-graph-dev \
    libslicot-pic \
    flex \
    libfl-dev \
    bison \
    meson \
    pkgconf \
# Other tools needed by the build
    jq


## Create dedicated user
RUN useradd -m gitlab-runner


## Install MATLAB

# NB: it will be possible to use --chmod=+x in Debian forky (requires Dockerfile 1.14)
ADD --chmod=755 https://www.mathworks.com/mpm/glnxa64/mpm /tmp/

RUN /tmp/mpm install --release=${MATLAB_VERSION} --destination=/usr/local/MATLAB/${MATLAB_VERSION} \
    --products MATLAB
COPY --chown=gitlab-runner:gitlab-runner license-${MATLAB_VERSION}.lic \
    /home/gitlab-runner/.matlab/${MATLAB_VERSION}_licenses/

## NB: matlab-support is buggy in Ubuntu 24.04 so we do not use it, its use can be restored when
## upgrading to Ubuntu 26.04
## Remove embedded system libraries using the matlab-support package, and clean up
#RUN { echo 'matlab-support matlab-support/matlab-install-glob string /usr/local/MATLAB/*'; \
#    echo 'matlab-support matlab-support/rename-libs boolean true'; } | debconf-set-selections \
#    && apt-get install matlab-support \
#    && rm -f /tmp/mpm /tmp/mathworks_root.log
RUN rm -f /usr/local/MATLAB/${MATLAB_VERSION}/sys/os/glnxa64/libgcc_s.so.1 \
    /usr/local/MATLAB/${MATLAB_VERSION}/sys/os/glnxa64/libstdc++.so.6 \
    /usr/local/MATLAB/${MATLAB_VERSION}/sys/os/glnxa64/libgfortran.so.5 \
    /usr/local/MATLAB/${MATLAB_VERSION}/sys/os/glnxa64/libquadmath.so.0 \
    && rm -f /tmp/mpm /tmp/mathworks_root.log


## Misc

# Run subsequent commands as gitlab-runner, and ensure that at runtime the
# image starts with that user as well
USER gitlab-runner
WORKDIR /home/gitlab-runner
