ARG MATLAB_RELEASE=R2025a

ARG MATLAB_PRODUCT_LIST="MATLAB Symbolic_Math_Toolbox Optimization_Toolbox Statistics_and_Machine_Learning_Toolbox"

ARG MATLAB_INSTALL_LOCATION="/opt/matlab/${MATLAB_RELEASE}"

FROM mathworks/matlab-deps:${MATLAB_RELEASE}

# Specify name of the user
ARG USER_NAME=ludo_hltl

# Declare build arguments to use at the current build stage.
ARG MATLAB_RELEASE
ARG MATLAB_PRODUCT_LIST
ARG MATLAB_INSTALL_LOCATION
ARG LICENSE_SERVER

# # Avoid interactive prompts during package installation
ENV DEBIAN_FRONTEND=noninteractive

# Install mpm dependencies.
RUN apt-get update
RUN apt-get install --no-install-recommends --yes \
    wget \
    git cmake g++ libgmp-dev libantlr4-runtime-dev curl\
    ca-certificates \
    && apt-get clean \
    && apt-get autoremove \
    && rm -rf /var/lib/apt/lists/*

# Add "USER_NAME" user and grant sudo permission. -> same username specified in matlab license!!
RUN adduser --shell /bin/bash --disabled-password --gecos "" $USER_NAME
RUN echo "$USER_NAME ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/$USER_NAME
RUN chmod 0440 /etc/sudoers.d/${USER_NAME}

# Set user and work directory.
USER ${USER_NAME}
WORKDIR /home/${USER_NAME}

# Run mpm to install MATLAB.
RUN wget -q https://www.mathworks.com/mpm/glnxa64/mpm \
    && chmod +x mpm \
    && sudo HOME=${HOME} ./mpm install \
    --release=${MATLAB_RELEASE} \
    --destination=${MATLAB_INSTALL_LOCATION} \
    --products ${MATLAB_PRODUCT_LIST} \
    || (echo "MPM Installation Failure. See below for more information:" && cat /tmp/mathworks_root.log && false) \
    && sudo rm -rf mpm /tmp/mathworks_root.log \
    && sudo ln -s ${MATLAB_INSTALL_LOCATION}/bin/matlab /usr/local/bin/matlab
#######################################################
# END MATLAB INSTALLATION
#######################################################


USER ${USER_NAME}
RUN mkdir -p /home/${USER_NAME}/h_ltl
# Set the working directory
WORKDIR /home/$USER_NAME/h_ltl
USER root

# Install Python and required tools
RUN apt-get update
RUN apt install -y python3 python3-pip python3-venv
RUN apt install -y wget
RUN apt-get clean
RUN rm -rf /var/lib/apt/lists/*

# Add virtual environment to PATH
ENV PATH="/opt/venv/bin:$PATH"

# Set the working directory
WORKDIR /home/$USER_NAME/h_ltl

# Create a virtual environment and install dependencies
RUN python3 -m venv /python_venv

WORKDIR /home/$USER_NAME/h_ltl

RUN /python_venv/bin/pip install --upgrade pip
RUN /python_venv/bin/pip install --no-cache-dir -r requirements.txt
RUN /python_venv/bin/pip install setuptools
RUN /python_venv/bin/pip install pytest
RUN /python_venv/bin/pysmt-install --z3 --confirm-agreement
#RUN /python_venv/bin/pysmt-install --cvc5 --confirm-agreement
RUN /python_venv/bin/pip install pytest-timeout numpy lxml pyvmt sympy scipy==1.15.2
RUN /python_venv/bin/pip install SumOfSquares mosek==10.1.15 ply

# Manually install cvc5
WORKDIR /opt
RUN sudo wget https://github.com/cvc5/cvc5/archive/refs/tags/cvc5-1.1.2.zip
RUN unzip cvc5-1.1.2.zip -d ./cvc5
RUN rm cvc5-1.1.2.zip
WORKDIR /opt/cvc5/cvc5-cvc5-1.1.2
ENV PATH="/python_venv/bin:$PATH"
RUN pip install -r contrib/requirements_python_dev.txt
RUN pip install pyparsing
RUN ./configure.sh --python-bindings --auto-download
WORKDIR /opt/cvc5/cvc5-cvc5-1.1.2/build
RUN make -j 40
RUN make install
RUN pysmt-install --check

WORKDIR /estools
RUN wget https://es-static-lfs.fbk.eu/access/lbattista/nuXmv
RUN chmod +x /estools/nuXmv
ENV NUXMV_PATH=/estools/nuXmv

ENV MATLAB="matlab"
ENV MATLAB_ARGS="-c /home/$USER_NAME/license.lic"


#######
# MOSEK INSTALLATION FOR MATLAB
#######

USER ${USER_NAME}
RUN mkdir -p /home/${USER_NAME}/installed_software/
WORKDIR /home/${USER_NAME}/installed_software/

RUN wget https://download.mosek.com/stable/10.2.17/mosektoolslinux64x86.tar.bz2
RUN tar -xvjf mosektoolslinux64x86.tar.bz2
RUN rm mosektoolslinux64x86.tar.bz2
ENV MOSEK_MATLAB_PATH=/home/${USER_NAME}/installed_software/mosek/10.2/toolbox/r2017aom

#######
# END MOSEK INSTALLATION FOR MATLAB
#######

#######
# YALMIP INSTALLATION FOR MATLAB
#######
WORKDIR /home/${USER_NAME}/
RUN wget https://github.com/yalmip/YALMIP/archive/master.zip
RUN unzip master.zip
RUN rm master.zip
ENV YALMIP_PATH=/home/${USER_NAME}/YALMIP-master
#######
# ENDYALMIP INSTALLATION FOR MATLAB
#######

#######
# CORA INSTALLATION FOR MATLAB
#######
WORKDIR /home/${USER_NAME}/

RUN wget https://github.com/TUMcps/CORA/archive/refs/heads/master.zip
RUN unzip master.zip
RUN rm master.zip
ENV CORA_PATH=/home/${USER_NAME}/CORA-master

#######
# END CORA INSTALLATION FOR MATLAB
#######

USER ${USER_NAME}

# Set the working directory
WORKDIR /home/${USER_NAME}/h_ltl

# Copy the rest of the application code (optional)
ENV CORA_OURS_PATH=/home/${USER_NAME}/h_ltl/CORA/general_scripts

ENV HLTL_WD="/home/${USER_NAME}/h_ltl" 

ENV PYTHONPATH="/opt/cvc5/cvc5-cvc5-1.1.2/build/src/api/python/:$PYTHONPATH"
