# Docker file for creating a container with freesurfer 7.2.0 release (07/2021). Note that installs which do
# not use the linux packaging tools (to install via an .rpm or .deb file) are not officially supported.
#
# Users are expected to fill out the registration form at https://surfer.nmr.mgh.harvard.edu/registration.html
# in order to download a license.txt file received thru the email address provided in the form.  Subsequently
# you can copy the license.txt file to live under $FREESURFER_HOME as .license (see commented out COPY command below).
#
# Alternately, you can place license.txt anywhere inside the container, e.g., under $HOME, and set
# the FS_LICENSE environment variable to point to it (see commented out COPY and ENV commands below).
# Then your license file will always be found even if you install/remove other freesurfer distributions.

FROM ubuntu:22.04

# shell settings
WORKDIR /root

ENV DEBIAN_FRONTEND=noninteractive
ENV DEBCONF_NONINTERACTIVE_SEEN=true

# Basic dependencies
RUN apt-get update \
    && apt-get install -y build-essential \
    curl \
    wget \
    unzip \
    fontconfig \
    libfreetype6 \
    libjpeg-turbo8 \
    libpng16-16 \
    libx11-6 \
    libxcb1 \
    libxext6 \
    libxrender1 \
    xfonts-75dpi \
    xfonts-base \
    xvfb \
    libglu1 \
    libxt-dev \
    libxext-dev \
    libncurses5 \
    language-pack-en \
    gettext \
    xterm \
    x11-apps \
    csh \
    xorg \
    openbox \
    xorg-dev \
    xserver-xorg-video-intel \
    libegl1 \
    libglib2.0-0 \
    libjpeg62 \
    libpcre2-16-0 \
    libxcb-icccm4 \
    libxcb-image0 \
    libxcb-keysyms1 \
    libxcb-render-util0 \
    libxcb-render0 \
    libxcb-util1 \
    libxcb-xinerama0 \
    libxcb-xinput0 \
    libxcb-xkb1 \
    libxkbcommon-x11-0 \
    libxkbcommon0 \
    libxss1 \
    file \
    binutils \
    libgomp1 \
    perl \ 
    psmisc \
    uuid-dev \
    vim-common \
    libjpeg62-dev \ 
    git \
    bc \
    cmake \
    tcsh \
    xxd \
    gfortran \
    libblas-dev \
    liblapack-dev \
    zlib1g-dev \
    libxmu-dev \
    libxmu-headers \
    libxi-dev \
    libx11-dev \
    libglu1-mesa-dev \   
    insighttoolkit5-examples \
    libinsighttoolkit5.2 \
    git-annex \
    vtkdata \
    tk-itk4 \
    libsndfile1-dev \
    libjack-jackd2-dev \
    libfftw3-dev \
    emacs \
    libudev-dev \
    qtcreator \
    qtbase5-dev \
    qt5-qmake \
    qttools5-dev \
    qttools5-dev-tools \
    qtwebengine5-dev \
    libqt5svg5-dev \
    libqt5websockets5-dev \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
   
RUN apt-get update \
    && apt-get install -y libinsighttoolkit5-dev
    
RUN mkdir -p ~/miniconda3 \
    && wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh \
    && bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3 \
    && rm ~/miniconda3/miniconda.sh 
        
#SHELL ["source", "~/miniconda3/bin/activate"]
#RUN echo "source ~/miniconda3/bin/activate" > ~/.bashrc

#SHELL ["conda", "init", "--all"]
#RUN conda init --all 

#SHELL ["conda", "create", "-n", "fs_build", "python=3.8.13"]
#RUN conda env create -n fs_build python=3.8.13

#SHELL ["conda", "run", "-n", "fs_build", "/bin/bash", "-c"]

#EXPOSE 5003
# The code to run when container is started:
#ENTRYPOINT ["conda", "run", "--no-capture-output", "-n", "fs_build", "python3", "src/server.py"]
    
RUN apt-get update -qq && apt-get install -y -q --no-install-recommends \
    build-essential python3.11 python3.11-dev python3-pip python3-setuptools libssl-dev libffi-dev libxml2-dev libxslt1-dev zlib1g-dev mlocate gifsicle \
    && apt-get clean \
    && updatedb \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
    
RUN ln -fs /usr/bin/python3.11 /usr/bin/python \
    && ln -fs /usr/bin/python3.11 /usr/bin/python3 \
    && ln -fs /usr/bin/pip3 /usr/bin/pip

RUN python -m pip install --upgrade pip    
RUN pip install vtk itk

RUN wget https://surfer.nmr.mgh.harvard.edu/pub/data/fspackages/prebuilt/centos7-packages.tar.gz \
    && tar -xzvf centos7-packages.tar.gz 
    
RUN git clone https://github.com/freesurfer/freesurfer \
    && mv  packages/* freesurfer/packages/ \
    && cd freesurfer/ \
    && git remote add datasrc https://surfer.nmr.mgh.harvard.edu/pub/dist/freesurfer/repo/annex.git \
    && git fetch datasrc \
    && git-annex get .
    
COPY license.txt /root/license.txt

RUN cd /root/freesurfer/ \   
    && cmake . -DMARTINOS_BUILD=OFF -DDISTRIBUTE_FSPYTHON=OFF -DBUILD_GUIS=OFF -DFS_PACKAGES_DIR=/root/freesurfer/packages -DQATOOLS_MODULE=OFF -DINTEGRATE_SAMSEG=OFF -DQT_SYSLIBS=OFF -DVTK_SYSLIBS=OFF

RUN cd /root/freesurfer/ \
    && make
    
