#/* vim: set filetype=dockerfile : */
#############################
#  Set up build environment #
#############################
FROM frolvlad/alpine-miniconda3 AS build
LABEL org.pydata.bokeh.maintainer="Karel-van-de-Plassche <karelvandeplassche@gmail.com>"

RUN apk add --no-cache \
    git \
    bash

ENV BOKEH_SOURCE_DIR /bokeh
ARG BOKEH_VERSION

RUN git clone --depth=50 https://github.com/bokeh/bokeh.git $BOKEH_SOURCE_DIR

#Increase build number to force rebuild
ENV build 0
WORKDIR $BOKEH_SOURCE_DIR
RUN sh -c 'if [[ ! -z "$BOKEH_VERSION" ]]; then echo Fetching $BOKEH_VERSION; git fetch origin $BOKEH_VERSION; git checkout -f FETCH_HEAD; fi'

ENV CONDA_REQS="conda-build"
ENV BASH /bin/bash

# Prepare conda env for building
RUN conda create --yes --name bokeh_build
RUN $BASH -c 'source activate bokeh_build'
# Install build dependencies
RUN conda install --yes --quiet $CONDA_REQS
# set default conda channels
RUN conda config --set auto_update_conda off
RUN conda config --append channels bokeh
RUN conda config --append channels conda-forge
RUN conda config --get channels
# install build time dependencies
RUN conda install --yes --quiet `python scripts/deps.py build`
# install NPM dependencies
RUN npm install -g npm@7
WORKDIR $BOKEH_SOURCE_DIR/bokehjs
# build BokehJS
RUN npm ci --no-progress
RUN sh -c 'if [[ -d make ]]; then node make build; else node_modules/.bin/gulp build; fi'
WORKDIR $BOKEH_SOURCE_DIR
# build a noarch conda package for Bokeh using the just-built BokehJS
RUN conda build conda.recipe --quiet --no-test --no-anaconda-upload --dirty

################################
#  Install build conda package #
################################
FROM frolvlad/alpine-miniconda3 AS install
COPY --from=build /opt/conda/conda-bld/noarch/ /opt/conda/conda-bld/noarch/

RUN apk add --no-cache \
    bash

RUN conda install --quiet --yes --use-local \
    bokeh \
    nodejs

# Clean the conda environment
RUN conda clean -ay

RUN python3 -c "import tornado; print('tornado version=' + tornado.version)"
RUN bokeh info

# Add some bokeh examples to be run
ENV BOKEH_EXAMPLE_DIR /bokeh_examples
COPY examples/models $BOKEH_EXAMPLE_DIR/models
COPY examples/app $BOKEH_EXAMPLE_DIR/app
