# Swixsit's build of Postiz (AGPL-3.0, https://github.com/gitroomhq/postiz-app).
# The Postiz source is not kept in this repository: it is fetched here at one
# exact commit, Swixsit's patches and overlay files are applied on top, and the
# result is built the way upstream builds its own image (Dockerfile.dev).
#
# docs/POSTIZ.md lists every change made to Postiz and how to upgrade.

ARG NODE_IMAGE=node:22.23.4-bookworm-slim

# ---- 1. The pinned Postiz source -------------------------------------------
FROM ${NODE_IMAGE} AS source
RUN apt-get update \
 && apt-get install -y --no-install-recommends git ca-certificates \
 && rm -rf /var/lib/apt/lists/*
# postiz.version names the release and its commit. A tag can be moved; the
# commit cannot. The build stops if they disagree.
COPY postiz.version /pin/postiz.version
RUN . /pin/postiz.version \
 && test -n "${POSTIZ_TAG}" && test -n "${POSTIZ_COMMIT}" \
 && git clone --depth 1 --branch "${POSTIZ_TAG}" https://github.com/gitroomhq/postiz-app.git /app \
 && test "$(git -C /app rev-parse HEAD)" = "${POSTIZ_COMMIT}" \
 && rm -rf /app/.git

# ---- 2. Dependencies, exactly as upstream locks them ------------------------
FROM ${NODE_IMAGE} AS deps
# Both the installer/build and the running CLI/agent inherit these opt-outs.
ENV NEXT_TELEMETRY_DISABLED=1 \
    CHECKPOINT_DISABLE=1 \
    COPILOTKIT_TELEMETRY_DISABLED=1
RUN apt-get update && apt-get install -y --no-install-recommends \
    g++ \
    make \
    python3-pip \
    bash \
    nginx \
    patch \
 && rm -rf /var/lib/apt/lists/*
RUN addgroup --system www \
 && adduser --system --ingroup www --home /www --shell /usr/sbin/nologin www \
 && mkdir -p /www \
 && chown -R www:www /www /var/lib/nginx
RUN npm --no-update-notifier --no-fund --global install pnpm@10.6.1 pm2
WORKDIR /app
COPY --from=source /app /app
RUN pnpm install --frozen-lockfile

# ---- 3. Swixsit's changes, then the upstream build --------------------------
FROM deps AS dist
COPY patches /swixsit/patches
COPY overlay /swixsit/overlay
COPY scripts/apply-swixsit.sh scripts/start.sh /swixsit/
COPY Dockerfile postiz.version /swixsit/
RUN sh /swixsit/apply-swixsit.sh /app \
 && cp /app/var/docker/nginx.conf /etc/nginx/nginx.conf
# Postiz is AGPL-3.0: whoever uses this copy over the network may have its
# source, Swixsit's changes included. The archive holds the patched source and,
# under swixsit-build/, the patches, overlay and build files that made it. The
# sign-in page links to it (docs/POSTIZ.md, "Licence").
RUN mkdir -p /app/swixsit-build \
 && cp -R /swixsit/. /app/swixsit-build/ \
 && tar -czf /tmp/source.tar.gz --exclude=node_modules --exclude=.nx -C /app . \
 && mv /tmp/source.tar.gz /app/apps/frontend/public/icons/swixsit/source.tar.gz
RUN . /swixsit/postiz.version \
 && NEXT_PUBLIC_VERSION="${POSTIZ_TAG}-swixsit" NODE_OPTIONS="--max-old-space-size=4096" pnpm run build
# nginx answers on 5000; scripts/start.sh says what else happens at start.
EXPOSE 5000
CMD ["sh", "/swixsit/start.sh"]
