FROM node:slim AS build

RUN mkdir -p /usr/src/cloudcmd/
WORKDIR /usr/src/cloudcmd

COPY package.json /usr/src/cloudcmd/

RUN apt-get update && \
    apt-get install -y build-essential python3 libncurses5-dev pkg-config && \
    apt-get install -y --no-install-recommends curl ca-certificates unzip && \
    curl -fsSL https://bun.sh/install | bash && \
    ln -s ~/.bun/bin/bun /usr/local/bin/bun && \
    chmod +x /usr/local/bin/bun && \
    bun r gritty --omit dev && \
    bun i gritty --omit dev && \
    ~/.bun/bin/bun pm cache rm && \
    rm -rf /var/lib/apt/lists/*

COPY . /usr/src/cloudcmd

FROM node:slim AS runtime

COPY --from=build /usr/src/cloudcmd /usr/src/cloudcmd
COPY --from=build /root/.bun /root/.bun

LABEL maintainer="Coderaiser"
LABEL org.opencontainers.image.source="https://github.com/coderaiser/cloudcmd"

WORKDIR /

ENV cloudcmd_terminal=true
ENV cloudcmd_terminal_path=gritty
ENV cloudcmd_open=false
ENV PATH="/root/.bun/bin:$PATH"

EXPOSE 8000

ENTRYPOINT ["/usr/src/cloudcmd/bin/cloudcmd.js"]
