FROM codercom/enterprise-base:ubuntu

USER 0

ARG DEBCONF_NONINTERACTIVE_SEEN=true
ARG DEBIAN_FRONTEND="noninteractive"
ARG TURBOVNC_VERSION=2.2.5
ARG VIRTUALGL_VERSION=2.6.4
ARG LIBJPEG_VERSION=2.0.5

RUN echo 'tzdata tzdata/Areas select Etc' | debconf-set-selections; \
    echo 'tzdata tzdata/Zones/Etc select UTC' | debconf-set-selections

RUN apt-get update && apt-get install -y \
  wget \
  unzip \
  zsh \
  supervisor \
  xorg \
  ssh \
  xfce4 \
  xfce4-goodies \
  x11-apps \
  dbus-x11 \
  xterm \
  python-numpy \
  firefox \
  fonts-lyx \
  libxtst6 \
  libxv1 \
  libglu1-mesa \
  libc6-dev \
  libglu1 \
  libsm6 \
  libxv1 \
  x11-xkb-utils \
  xauth \
  xfonts-base \
  xkb-data

# Install quality of life packages.
RUN yes | unminimize

# Remove packages which may not behave well in a VNC environment.
RUN apt-get remove -y \
  xfce4-battery-plugin \
  xfce4-power-manager-plugins \
  xfce4-pulseaudio-plugin \
  light-locker

RUN locale-gen en_US.UTF-8

ARG HOME=/home/coder
ARG VNC_ROOT_DIR=/opt/vnc

RUN cd /tmp \
  && curl -fsSL -O https://netix.dl.sourceforge.net/project/turbovnc/${TURBOVNC_VERSION}/turbovnc_${TURBOVNC_VERSION}_amd64.deb \
    -O https://netix.dl.sourceforge.net/project/libjpeg-turbo/${LIBJPEG_VERSION}/libjpeg-turbo-official_${LIBJPEG_VERSION}_amd64.deb \
    -O https://netix.dl.sourceforge.net/project/virtualgl/${VIRTUALGL_VERSION}/virtualgl_${VIRTUALGL_VERSION}_amd64.deb \
    && dpkg -i *.deb \
    && rm -f /tmp/*.deb \
    && sed -i 's/$host:/unix:/g' /opt/TurboVNC/bin/vncserver

RUN ln -s /opt/TurboVNC/bin/* /opt/VirtualGL/bin/* /usr/local/bin/
# Configure VGL for use in a single user environment.
# This may trigger a warning about display managers needing to be restarted.
# This can be ignored as the VNC server manages this lifecycle.  
RUN vglserver_config -config +s +f +t

RUN echo 'no-remote-connections\n\
no-httpd\n\
no-x11-tcp-connections\n\
no-pam-sessions\n\
' > /etc/turbovncserver-security.conf

ENV VNC_SCRIPTS=$VNC_ROOT_DIR/scripts \
  VNC_SETUP_SCRIPTS=$VNC_ROOT_DIR/setup \
  VNC_LOG_DIR=$HOME/.vnc/log \
  VNC_XSTARTUP=$VNC_ROOT_DIR/xstartup \
  VNC_SUPERVISOR_CONFIG=$VNC_ROOT_DIR/supervisord.conf \
  VNC_PORT=5990 \
  VNC_DISPLAY_ID=:90 \
  VNC_COL_DEPTH=24 \
  VNC_RESOLUTION=3840x2160 \
  NO_VNC_HOME=$VNC_ROOT_DIR/noVNC \
  NO_VNC_PORT=6081 \
  XFCE_BASE_DIR=$VNC_ROOT_DIR/xfce4 \
  XFCE_DEST_DIR=$HOME/.config/xfce4

WORKDIR $HOME

# Enable better defaults for command tab completion.
RUN chsh -s $(readlink -f $(which zsh)) coder 

ADD --chown=coder:coder ./xfce4 $XFCE_BASE_DIR
ADD --chown=coder:coder ./vnc $VNC_ROOT_DIR
ADD --chown=coder:coder ./supervisor /etc/supervisor

RUN find $VNC_SETUP_SCRIPTS -name '*.sh' -exec chmod a+x {} +

RUN $VNC_SETUP_SCRIPTS/set_user_permission.sh $VNC_ROOT_DIR \
  && chmod +x $VNC_XSTARTUP

# Add Coder-specific scripts and metadata to the image
COPY ["./coder", "/coder"]
RUN chmod +x /coder/configure

COPY deprecated.txt /etc/motd
RUN echo '[ ! -z "$TERM" ] && cat /etc/motd' >> /etc/bash.bashrc

USER coder

RUN $VNC_SETUP_SCRIPTS/no_vnc.sh

EXPOSE $NO_VNC_PORT
