FROM avatao/debian:buster

RUN curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -                                     &&\
    curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -                             &&\
    echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list &&\
    apt-get update                                                                                     &&\
    apt-get install -y --no-install-recommends \
        nodejs                                 \
        yarn                                   \
        supervisor                             \
        libzmq5                                \
        nginx                                  \
        jq                                     \
        gettext-base                                                                                   &&\
    rm -rf /var/lib/apt/lists/*                                                                        &&\
    ln -sf /bin/bash /bin/sh

COPY requirements.txt /tmp
RUN pip3 install -r /tmp/requirements.txt

ENV TFW_PUBLIC_PORT=8888          \
    TFW_WEB_PORT=4242             \
    TFW_LOGIN_APP_PORT=6666       \
    TFW_TERMINADO_PORT=7878       \
    TFW_SUPERVISOR_HTTP_PORT=9001 \
    TFW_PUB_PORT=7654             \
    TFW_PULL_PORT=8765

EXPOSE ${TFW_PUBLIC_PORT}

ENV PYTHONPATH="/usr/local/lib"                                            \
    TFW_SUPERVISOR_HTTP_URI="http://localhost:${TFW_SUPERVISOR_HTTP_PORT}" \
    TFW_SUPERVISORD_CONF="/etc/supervisor/supervisord.conf"                \
    TFW_SUPERVISORD_COMPONENTS="/etc/supervisor/conf"                      \
    TFW_NGINX_CONF="/etc/nginx/nginx.conf"                                 \
    TFW_NGINX_DEFAULT="/etc/nginx/sites-enabled/default"                   \
    TFW_NGINX_COMPONENTS="/etc/nginx/components"                           \
    TFW_LIB_DIR="/usr/local/lib"                                           \
    TFW_FRONTEND_DIR="/srv/frontend"                                       \
    TFW_DIR="/.tfw"                                                        \
    TFW_SERVER_DIR="/.tfw/tfw_server"                                      \
    TFW_SNAPSHOTS_DIR="/.tfw/snapshots"                                    \
    TFW_AUTH_KEY="/tmp/tfw-auth.key"                                       \
    TFW_HISTFILE="/home/${AVATAO_USER}/.bash_history"                      \
    TFW_LOGFILE="/var/log/tfw.log"                                         \
    PROMPT_COMMAND="history -a"

COPY bashrc /tmp
RUN echo "export HISTFILE=${TFW_HISTFILE}" >> /tmp/bashrc &&\
    cat /tmp/bashrc >> /home/${AVATAO_USER}/.bashrc

COPY supervisor/supervisord.conf ${TFW_SUPERVISORD_CONF}
COPY supervisor/components/ ${TFW_SUPERVISORD_COMPONENTS}
COPY nginx/nginx.conf ${TFW_NGINX_CONF}
COPY nginx/default.conf ${TFW_NGINX_DEFAULT}
COPY nginx/components/ ${TFW_NGINX_COMPONENTS}
COPY lib LICENSE ${TFW_LIB_DIR}/
COPY supervisor/tfw_server.py ${TFW_SERVER_DIR}/

RUN for dir in "${TFW_LIB_DIR}"/{tfw,tao,envvars} "/etc/nginx" "/etc/supervisor"; do \
        chown -R root:root "$dir" && chmod -R 700 "$dir";                            \
    done

ONBUILD ARG BUILD_CONTEXT="solvable"
ONBUILD ARG NOFRONTEND=""

ONBUILD COPY ${BUILD_CONTEXT}/nginx/ ${TFW_NGINX_COMPONENTS}
ONBUILD COPY ${BUILD_CONTEXT}/supervisor/ ${TFW_SUPERVISORD_COMPONENTS}

ONBUILD RUN for f in "${TFW_NGINX_DEFAULT}" ${TFW_NGINX_COMPONENTS}/*.conf; do                               \
                envsubst "$(printenv | cut -d= -f1 | grep TFW_ | sed -e 's/^/$/g')" < $f > $f~ && mv $f~ $f ;\
            done
ONBUILD VOLUME ["/etc/nginx", "/var/lib/nginx", "/var/log/nginx", "${TFW_LIB_DIR}/envvars", "${TFW_LIB_DIR}/tfw"]

ONBUILD COPY ${BUILD_CONTEXT}/frontend /data/
ONBUILD RUN test -z "${NOFRONTEND}" && cd /data && yarn install --frozen-lockfile || :
ONBUILD RUN test -z "${NOFRONTEND}" && cd /data && yarn build --no-progress || :
ONBUILD RUN test -z "${NOFRONTEND}" && mv /data/dist ${TFW_FRONTEND_DIR} && rm -rf /data || :

CMD exec supervisord --nodaemon --configuration ${TFW_SUPERVISORD_CONF}