#####
# Alpine image
#####

FROM node:22 AS alpine

LABEL org.opencontainers.image.authors="Mike Mellor <mike.mellor@avalerehealth.com>"

ARG TARGETARCH

WORKDIR /app

EXPOSE 3000
EXPOSE 3001

# Get latest apt-get
RUN apt-get update

# Update npm to latest
RUN npm install -g npm@latest

# Install dart sass vm (arch switch)
RUN curl -L -o /sass.tar.gz https://github.com/sass/dart-sass/releases/download/1.57.1/dart-sass-1.57.1-linux-$(if [ "$TARGETARCH" = "arm64" ]; then echo 'arm64'; else echo 'x64'; fi).tar.gz
RUN tar -xf /sass.tar.gz -C /
ENV PATH="${PATH}:/dart-sass/"
RUN rm -rf /sass.tar.gz

# Set safe directory to remove intel errors when running git rev-parse
RUN su node -c "git config --global --add safe.directory /app"

# Also set global git flag for allowing access across filesystems for mounted node_modules dir
ENV GIT_DISCOVERY_ACROSS_FILESYSTEM=1

# Disable npm message about updates being available
RUN su node -c "npm config set update-notifier false"

# Add node user to www-data group
RUN usermod -a -G www-data node

# Install lftp
RUN apt-get install -y lftp

# Install chromium
RUN apt-get install -y chromium fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf libxss1 --no-install-recommends
# Stop Puppeteer downloading browsers during npm install
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium

# Install package.json checker
RUN npm install check-dependencies -g

# Install node_modules checker
RUN npm install are-you-es5 -g

# Install git branch fetcher
RUN npm install git-branch -g

# Install snyk package
RUN npm install snyk snyk-to-html -g

# Link root global node_modules to ~/.node_modules
RUN ln -s /usr/local/lib/node_modules/ /home/node/.node_modules

# Install rclone
RUN curl https://rclone.org/install.sh | bash

# Cleanup apt-get install folders
RUN apt-get clean && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

COPY entrypoint.sh /bin/entrypoint.sh
RUN chmod +x /bin/entrypoint.sh
ENTRYPOINT ["/bin/entrypoint.sh"]

#####
# Full image
#####

FROM alpine AS full

# Get latest apt-get
RUN apt-get update

# Install ghostscript
RUN apt-get install -y ghostscript

# Install zip
RUN apt-get install zip

# Install AWS Elastic Beanstalk cli
ENV PATH="${PATH}:/.ebcli-virtual-env/executables"
RUN apt-get -y install python-is-python3 python3-pip && \
    pip install virtualenv --break-system-packages && \
    git clone https://github.com/aws/aws-elastic-beanstalk-cli-setup.git ~/aws-eb && \
    python ~/aws-eb/scripts/ebcli_installer.py --location / && \
    rm -rf ~/aws-eb

# Install AWS-CLI@2 (arch switch)
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-$(if [ "$TARGETARCH" = "arm64" ]; then echo 'aarch64'; else echo 'x86_64'; fi).zip" -o "awscliv2.zip"
RUN unzip awscliv2.zip
RUN ./aws/install
RUN rm -rf ./aws && rm -rf awscliv2.zip

# Update electron globally
RUN npm install -g electron-packager@latest

# Install wine64
RUN apt-get install -y wine64 && \
    ln -s /usr/bin/wine /usr/bin/wine64

# Install watertight
RUN --mount=type=ssh npm install -g git+ssh://git@bitbucket.org/fishawackdigital/watertight-node-auto.git#v6.0.0

# Cleanup apt-get install folders
RUN apt-get clean && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*