# déclaration unique des variables d'environnement
#bump for deply
FROM node:10-buster-slim as base
LABEL maintainer="antoine@barbare.me"

ARG ETHERPAD_VERSION_BUILD=1.7.5
ARG ETHERPAD_LANDING_PAGE_VERSION_BUILD=v0.1
ARG NODE_ENV_BUILD=production

ENV ETHERPAD_VERSION=${ETHERPAD_VERSION_BUILD}
ENV ETHERPAD_LANDING_PAGE_VERSION=${ETHERPAD_LANDING_PAGE_VERSION_BUILD}
ENV NODE_ENV=${NODE_ENV_BUILD}


# téléchargement d'Etherpad et de la page d'accueil dans une image temporaire
FROM base as downloader
RUN apt-get update && \
    apt-get install -y \
      git \
      curl \
      gzip \
      libssl-dev \
      pkg-config \
      python \
      unzip && \
    mkdir -p /opt/etherpad-lite && \
    chown -R node /opt/etherpad-lite

USER node
WORKDIR /opt/etherpad-lite
RUN curl -SL https://github.com/ether/etherpad-lite/archive/${ETHERPAD_VERSION}.zip > etherpad.zip && \
    unzip etherpad && \
    rm -Rf etherpad.zip etherpad-lite-${ETHERPAD_VERSION}/.git && \
    mv etherpad-lite-${ETHERPAD_VERSION}/* . && \
    mkdir -p src/templates && \
    bin/installDeps.sh && \
    npm install \
        ep_align \
        ep_author_hover \
        ep_chatdate \
	ep_colibris_full_width \
        ep_comments_page \
        ep_countable \
        ep_delete_empty_pads \
        ep_font_color \
        ep_font_family \
        ep_headings2 \
	ep_markdown \
        ep_pad_activity_nofication_in_title \
        ep_pads_stats \
        ep_page_view \
	ep_prompt_for_name \
        ep_set_title_on_pad \
        ep_subscript_and_superscript \
        ep_tables2 \
        ep_user_font_size && \
    git clone https://framagit.org/DeBugs/picasoft-etherpad-landing-page.git src/templates/picasoft-etherpad-landing-page && \
    rm package-lock.json && \
    git --git-dir=src/templates/picasoft-etherpad-landing-page/.git checkout ${ETHERPAD_LANDING_PAGE_VERSION} && \
    cp src/templates/picasoft-etherpad-landing-page/index.html src/templates/index.html && \
    cp src/templates/picasoft-etherpad-landing-page/static/* src/static/ -r && \
    # Change default hard-coded pad title
    sed -i 's|Untitled Pad|Nouveau pad|g' node_modules/ep_set_title_on_pad/templates/title.ejs && \
    sed -i 's|Untitled Pad|Nouveau pad|g' node_modules/ep_set_title_on_pad/static/tests/frontend/specs/atest.js


# construction de l'image finale
FROM base

COPY entrypoint.sh /opt/etherpad-lite/entrypoint.sh
RUN apt-get update && \
    apt-get install -y default-mysql-client && \
    chmod +x /opt/etherpad-lite/entrypoint.sh && \
    mkdir -p /opt/etherpad-lite && \
    chown -R node /opt/etherpad-lite && \
    usermod -d /opt/etherpad-lite node && \
    rm -rf /var/lib/apt/lists/*

# import des fichiers téléchargés dans l'image downloader
COPY --from=downloader --chown=node /opt/etherpad-lite /opt/etherpad-lite/

USER node
ENTRYPOINT ["/opt/etherpad-lite/entrypoint.sh"]
CMD /bin/bash -c "/opt/etherpad-lite/bin/run.sh"

EXPOSE 8080
VOLUME /opt/etherpad-lite/var