# Use node version 10. This is the long-term support version at the time of writing 
FROM node:10

# Install Linux dependencies for Xvfb (to run Pando headless without a display)
RUN apt-get update && apt-get install -y xvfb libxtst6 libxss1 libgconf-2-4 libnss3 libasound2 

# This is where the application code will reside (implicitly creates the directory)
WORKDIR /usr/src/pando-computing

# Copies both package.json and package-lock.json to ensure the exact same dependencies are used
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy application inside Docker
COPY . .

# Make 'pando' globally available
RUN npm link

# Retrieve examples
WORKDIR /usr/src/
RUN git clone https://github.com/elavoie/pando-handbook

# Setup throughput analysis script
WORKDIR /usr/src/pando-handbook/middleware19/analysis
RUN npm install

# Setup examples 
# -- Link to pando-computing to avoid wasteful copies

# Square (Test Example)
WORKDIR /usr/src/pando-handbook/examples/square
RUN npm link /usr/src/pando-computing 
RUN npm install

# Collatz 
WORKDIR /usr/src/pando-handbook/examples/collatz
RUN npm link /usr/src/pando-computing 
RUN npm install

# Crypto-Mining
WORKDIR /usr/src/pando-handbook/examples/crypto-mining
RUN npm link /usr/src/pando-computing 
RUN npm install

# Photo-Batch-Processing
WORKDIR /usr/src/pando-handbook/examples/photo-batch-processing
RUN npm link /usr/src/pando-computing 
RUN npm install

# Random Testing
WORKDIR /usr/src/pando-handbook/examples/random-testing
RUN npm link /usr/src/pando-computing 
RUN npm install

# Raytracer
WORKDIR /usr/src/pando-handbook/examples/raytracer
RUN npm link /usr/src/pando-computing 
RUN npm install

# RLNetwork
WORKDIR /usr/src/pando-handbook/examples/rlnetwork
RUN npm link /usr/src/pando-computing 
RUN npm install

# These are the ports that Pando uses,
# 5000 HTTP Server to connect volunteers
# 5001 HTTP Server to monitor performance
EXPOSE 5000 5001

WORKDIR  /usr/src/pando-handbook/middleware-2019/run
CMD ./square
