# syntax=docker/dockerfile:1
FROM ubuntu:jammy as base

ENV DEBIAN_FRONTEND=noninteractive
ENV PASSWORD=vnc4me
RUN apt-get update

# Build tools
RUN apt-get install -y dbus-x11 x11-xkb-utils xkb-data
RUN apt-get install -y python3
RUN apt-get install -y wget
RUN apt-get install -y git make gcc
RUN apt-get install -y icewm
RUN apt-get install -y x11-apps

FROM base as build
ADD https://github.com/TurboVNC/turbovnc/releases/download/3.1.1/turbovnc_3.1.1_amd64.deb /tmp/
ADD https://github.com/novnc/websockify.git /tmp/websockify
ADD https://github.com/novnc/noVNC.git /tmp/noVNC

WORKDIR "/tmp"
RUN dpkg -i turbovnc_3.1.1_amd64.deb

WORKDIR "/tmp/websockify"
RUN make -j4

# develop only
RUN apt-get install -y net-tools socat python3-numpy

ENV PATH=$PATH:/opt/TurboVNC/bin
RUN	mkdir ~/.vnc/
RUN	echo $PASSWORD | vncpasswd -f > ~/.vnc/passwd
RUN	chmod 0600 ~/.vnc/passwd

EXPOSE 5900

COPY vnc/xstartup.turbovnc /root/.vnc/
COPY vnc/turbovncserver-security.conf /etc/
RUN	chmod 0600 /etc/turbovncserver-security.conf
COPY vnc/turbovncserver.conf /etc/
COPY app/*.sh /app/

RUN openssl req -x509 -nodes -newkey rsa:2048 -keyout /tmp/novnc.key -out /tmp/novnc.pem -days 3650 -subj "/C=US/ST=NY/L=NY/O=NY/OU=NY/CN=NY emailAddress=email@example.com"

CMD ["/app/app.sh"]

# https://dunedlin.wordpress.com/2020/10/24/a-minimal-x11-install/
# https://askubuntu.com/questions/1313445/how-do-i-install-vnc-on-a-headless-ubuntu-20-10
# https://github.com/mviereck/x11docker/wiki/How-to-access-X-over-TCP-IP-network
# https://jaydenm.com/blog/docker-x11-desktop/
# https://github.com/karinepires/docker-secure-vncserver/blob/master/Dockerfile
# https://github.com/theonemule/docker-opengl-turbovnc/blob/master/dockerfile

# MESA
# https://docs.mesa3d.org/install.html
