# --------------------------------------------------------------------------------------------------
# NOTE: This Dockerfile was designed to be run with BuildKit.
#
# Enable BuildKit using:
#   $ export DOCKER_BUILDKIT=1
#   $ export COMPOSE_DOCKER_CLI_BUILD=1
# --------------------------------------------------------------------------------------------------

# The service to build
ARG SERVICE

# The base image to use
ARG BASE=alpine

# Base Definitions ---------------------------------------------------------------------------------

# Alpine base definition
FROM node:14-alpine AS base-alpine
RUN mkdir /var/autoplex && chown node:node -R /var/autoplex

# Alpine dev definition
FROM base-alpine AS base-alpine-dev
RUN apk add build-base python3

# Slim base definition
FROM node:14-slim AS base-slim
RUN mkdir /var/autoplex && chown node:node -R /var/autoplex
RUN apt-get update
RUN apt-get full-upgrade -y
RUN apt-get install -y libasound2 iputils-ping curl gnupg1 apt-transport-https dirmngr
RUN apt-get autoremove --purge -y
RUN rm -rf /var/lib/apt/lists/*

# Slim dev definition
FROM base-slim AS base-slim-dev


# Development Image --------------------------------------------------------------------------------

# Define the base image
FROM base-${BASE}-dev AS dev-base
WORKDIR /opt/app
ENV PATH "/app/node_modules/.bin:$PATH"
RUN echo "#!/bin/sh" >> /bin/lerna && \
	echo '( cd /opt/app && ./node_modules/.bin/lerna "$@" )' >> /bin/lerna && \
	chmod +x /bin/lerna
COPY lerna.json package.json yarn.lock ./
RUN yarn install
COPY tsconfig*.json ./

# Define the dev image
FROM dev-base AS dev
ARG SERVICE
RUN ln -s /opt/app/services/${SERVICE} /app
WORKDIR /app
CMD [ "yarn", "run", "start:dev" ]

# Build Definitions --------------------------------------------------------------------------------

# Create the builder image with all dependencies
FROM base-${BASE}-dev AS builder-base
WORKDIR /app
RUN yarn global add lerna typescript rimraf
COPY docker/scripts docker/scripts
COPY lerna.json package.json tsconfig*.json ./
RUN yarn install
COPY api api
COPY packages packages

# Copy service package.json and patch files if they exist
FROM builder-base AS builder
ARG SERVICE
COPY services/${SERVICE}/package.json ./services/${SERVICE}/
COPY services/${SERVICE}/patches* ./services/${SERVICE}/patches

# Cache an image containing only the prod-dependencies
FROM builder AS prod-deps
ARG BASE
RUN lerna bootstrap -- --production --no-optional
RUN yarn run export:deps ${BASE}

# Build the service
FROM builder AS build
ARG BASE
ARG SERVICE
COPY services/${SERVICE} ./services/${SERVICE}
RUN lerna clean --yes && lerna bootstrap && lerna run clean && lerna run build
RUN yarn run export:builds ${BASE}

# Return to base image with the compiled files and production dependencies
FROM base-${BASE} AS prod
ARG SERVICE
WORKDIR /app
COPY --from=prod-deps /app/build /opt
COPY --from=build /app/build /opt
RUN for link in $(find /opt/services -type l); do dir=$(readlink -f $link); rm $link; ln -s $dir $link; done
RUN mv /opt/services/${SERVICE}/* /app && rm -rf /opt/services
# TODO: Should remove unnecessary packages that were included

# Default entrypoint
CMD [ "yarn", "run", "start" ]
