FROM lofar/lofar-pipeline:LOFAR-Release-4_0_16
LABEL Maintainer Giuliano Taffoni <giuliano.taffoni@inaf.it>
USER root
# Set non-interactive
ENV DEBIAN_FRONTEND noninteractive

# Always update when extending base images
RUN apt update
RUN apt -y upgrade

#------------------------
# Install deps
#------------------------

# Git, Curl, sudo and  Nano
RUN apt-get install git curl sudo nano -y
RUN apt -y install lubuntu-core libjpeg-dev wget sudo git curl nano vim emacs
RUN apt-get install -y  supervisor strace net-tools

RUN  sed -i 's\1\0\g' /etc/apt/apt.conf.d/20auto-upgrades
RUN  ln -s /usr/share/lxde/wallpapers/lxde_blue.jpg /etc/alternatives/desktop-background
#


#------------------------
# Lofar user
#------------------------

# Add group. We chose GID 65527 to try avoiding conflicts.
RUN groupadd -g 65527 lofar

# Add user. We chose UID 65527 to try avoiding conflicts.
RUN useradd lofar -d /home/lofar -u 65527 -g 65527 -m -s /bin/bash

# Add metuaser user to sudoers
RUN adduser lofar sudo

# No pass sudo (for everyone, actually)
COPY sudoers /etc/sudoers

# Setup home and input/output data directories
COPY data/input_data /input_data
COPY data/output_data /output_data
RUN chown lofar:lofar /home/lofar && chown -R lofar:lofar /input_data && chown -R lofar:lofar /output_data

# Prepare for logs
RUN mkdir /home/lofar/.logs && chown lofar:lofar /home/lofar/.logs


RUN mkdir /home/lofar/.vnc
COPY files/config  /home/lofar/.vnc
COPY files/xstartup /home/lofar/.vnc
RUN chmod 755 /home/lofar/.vnc/xstartup
RUN chown -R lofar:lofar /home/lofar/.vnc

# Rename user home folder as a "vanilla" home folder
RUN mv /home/lofar /lofar_home_vanilla

# Give write access to anyone to the home folder so the entrypoint will be able
# to copy over the /home/matauser_vanilla into /home/metauser (for Singularity)
RUN chmod 777 /home

# Copy and install kasmvnc
COPY files/kasmvnc-Linux-x86_64-0.9.tar.gz /tmp
RUN sudo tar xz --strip 1 -C / -f /tmp/kasmvnc-Linux-x86_64-0.9.tar.gz && rm /tmp/kasmvnc-Linux-x86_64-0.9.tar.gz
RUN mkdir /usr/local/share/kasmvnc/certs
RUN chown lofar.lofar /usr/local/share/kasmvnc/certs
COPY files/index.html /usr/local/share/kasmvnc/www/


# Global Supervisord conf
COPY files/supervisord.conf /etc/supervisor/
COPY files/supervisord_kasm.conf /etc/supervisor/conf.d/
COPY files/run_kasm.sh /etc/supervisor/conf.d/
RUN chmod 755 /etc/supervisor/conf.d/run_kasm.sh



#----------------------
# Entrypoint
#----------------------

# Copy entrypoint
COPY entrypoint.sh /

# Give right permissions
RUN chmod 755 /entrypoint.sh

# Set entrypoint
ENTRYPOINT ["/entrypoint.sh"]

RUN apt -y clean
# Set user lofar
USER lofar

# Set container name
ENV CONTAINER_NAME='base4.0.16'
