FROM ubuntu:20.04
MAINTAINER Stefano Alberto Russo <stefano.russo@gmail.com>

#----------------------
# Basics
#----------------------

# Set non-interactive
ENV DEBIAN_FRONTEND noninteractive

# Update
RUN apt-get update

# Utilities
RUN apt-get install -y nano telnet unzip wget openssh-server sudo curl

# Devel
RUN apt-get install -y build-essential python-dev git-core

# Node & nginx
RUN apt-get install -y nodejs npm nginx


#------------------------
# Esap user
#------------------------

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

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

# Add esap user to sudoers
RUN adduser esap sudo

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


#------------------------
# Code
#------------------------

RUN cd /opt && git clone https://git.astron.nl/astron-sdc/esap-gui.git
RUN cd /opt/esap-gui && git pull && git checkout 2882da5a

# Chenge hardcoded API URL
RUN sed -i 's#http://localhost:5555/esap-api/#/esap-api/#g' /opt/esap-gui/src/contexts/GlobalContext.js
RUN sed -i 's#https://sdc-dev.astron.nl/esap-api/#/esap-api/#g' /opt/esap-gui/src/contexts/GlobalContext.js
RUN cd /opt/esap-gui && npm install
RUN cd /opt/esap-gui && npm run build

# Link static files
RUN ln -s /opt/esap-gui/build /var/www/html/esap-gui
RUN ln -s /opt/esap-gui/build/static/ /var/www/html/static

# Change nginx conf to listen on 8001
COPY sites_enabled_default /etc/nginx/sites-enabled/default

# Add redirect index
COPY index.html /var/www/html/index.html



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

COPY run_gui.sh /run_gui.sh
COPY entrypoint.sh /entrypoint.sh
RUN chmod 755 /entrypoint.sh /run_gui.sh

USER esap
ENTRYPOINT ["/entrypoint.sh"]
