FROM --platform=${BUILDPLATFORM:-linux/amd64} node:20-bookworm-slim

ARG TARGETPLATFORM
ARG BUILDPLATFORM
ARG TARGETOS
ARG TARGETBRANCH

LABEL maintainer="dave@force9.org"

ENV NVM_DIR /root/.nvm
ENV DEBIAN_FRONTEND noninteractive


# Clone repo and build
RUN apt-get update \
    && apt-get install -y \
    git \
    curl \
    build-essential \
    python3 \
    libssl-dev \
    lrzsz \
    arj \
    lhasa \
    unrar-free \
    p7zip-full \
    dos2unix \
    && npm set progress=false && npm config set depth 0 \
    && npm install -g npm@latest \
    && npm install -g pm2


# Clone Enigma and install
RUN git clone --depth 1 https://github.com/NuSkooler/enigma-bbs.git /enigma-bbs \
    && cd /enigma-bbs && npm install \
    && dos2unix /enigma-bbs/docker/bin/docker-entrypoint.sh \
    && chmod +x /enigma-bbs/docker/bin/docker-entrypoint.sh \
    && cp -f /enigma-bbs/docker/bin/sexyz /usr/local/bin

# Setup staging directories with dummy SSH key
RUN mkdir -p /enigma-bbs-pre/art \
    && mkdir -p /enigma-bbs-pre/mods \
    && mkdir -p /enigma-bbs-pre/config/security \
    && cp -rp /enigma-bbs/art/* /enigma-bbs-pre/art/ \
    && cp -rp /enigma-bbs/mods/* /enigma-bbs-pre/mods/ \
    && cp -rp /enigma-bbs/config/* /enigma-bbs-pre/config/ \
    && touch /enigma-bbs-pre/config/security/ssh_private_key.pem \
    && apt-get remove build-essential python3 libssl-dev git curl dos2unix -y \
    && apt-get autoremove -y \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
    && apt-get clean

# Volumes
VOLUME /enigma-bbs/art
VOLUME /enigma-bbs/config
VOLUME /enigma-bbs/db
VOLUME /enigma-bbs/filebase
VOLUME /enigma-bbs/logs
VOLUME /enigma-bbs/mods
VOLUME /mail

EXPOSE 8888 44510 44511

WORKDIR /enigma-bbs

ENTRYPOINT ["/enigma-bbs/docker/bin/docker-entrypoint.sh"]
