# syntax=docker/dockerfile:1.7

ARG NODE_IMAGE=node:24-alpine3.23
ARG GO_IMAGE=golang:1.25-alpine3.23
ARG RUNTIME_IMAGE=alpine:3.23.4

FROM ${NODE_IMAGE} AS web-extensions-source

ARG WEB_EXTENSIONS_COMMIT=6af7f4b12736387b05bd9b235af3f75433460054
ARG WEB_EXTENSIONS_REPOSITORY=https://github.com/owncloud/web-extensions.git

RUN apk add --no-cache git
RUN mkdir -p /src/web-extensions \
    && git -C /src/web-extensions init \
    && git -C /src/web-extensions remote add origin "${WEB_EXTENSIONS_REPOSITORY}" \
    && git -C /src/web-extensions fetch --depth=1 origin "${WEB_EXTENSIONS_COMMIT}" \
    && git -C /src/web-extensions checkout --detach FETCH_HEAD \
    && test "$(git -C /src/web-extensions rev-parse HEAD)" = "${WEB_EXTENSIONS_COMMIT}"

FROM ${NODE_IMAGE} AS web-build

ARG WEB_COMMIT=b16a34fdf5569d2fd63b1ff3c575c1a4ee569ad3
ARG WEB_REPOSITORY=https://github.com/owncloud/web.git
ENV NODE_OPTIONS=--max-old-space-size=4096

RUN apk add --no-cache bash git make python3 g++ \
    && corepack enable
RUN mkdir -p /src/web \
    && git -C /src/web init \
    && git -C /src/web remote add origin "${WEB_REPOSITORY}" \
    && git -C /src/web fetch --depth=1 origin "${WEB_COMMIT}" \
    && git -C /src/web checkout --detach FETCH_HEAD \
    && test "$(git -C /src/web rev-parse HEAD)" = "${WEB_COMMIT}"

COPY patches/web/0001-hide-runtime-version.patch /tmp/web.patch
RUN git -C /src/web apply --check /tmp/web.patch \
    && git -C /src/web apply /tmp/web.patch
COPY patches/web/0002-infinidrive-single-space.patch /tmp/web-single-space.patch
RUN git -C /src/web apply --check /tmp/web-single-space.patch \
    && git -C /src/web apply /tmp/web-single-space.patch
COPY patches/web/0003-infinidrive-metadata-namespace.patch /tmp/web-metadata-namespace.patch
RUN git -C /src/web apply --check /tmp/web-metadata-namespace.patch \
    && git -C /src/web apply /tmp/web-metadata-namespace.patch
COPY patches/web/0004-customer-account-menu.patch /tmp/web-customer-account.patch
RUN git -C /src/web apply --check /tmp/web-customer-account.patch \
    && git -C /src/web apply /tmp/web-customer-account.patch
COPY patches/web/0005-disable-recipient-search.patch /tmp/web-disable-recipient-search.patch
RUN git -C /src/web apply --check /tmp/web-disable-recipient-search.patch \
    && git -C /src/web apply /tmp/web-disable-recipient-search.patch
COPY patches/web/0006-filter-customer-trash.patch /tmp/web-filter-customer-trash.patch
RUN git -C /src/web apply --check /tmp/web-filter-customer-trash.patch \
    && git -C /src/web apply /tmp/web-filter-customer-trash.patch
COPY patches/web/0007-render-pdf-in-iframe.patch /tmp/web-pdf-iframe.patch
RUN git -C /src/web apply --check /tmp/web-pdf-iframe.patch \
    && git -C /src/web apply /tmp/web-pdf-iframe.patch
COPY patches/web/0008-infinidrive-activities-space.patch /tmp/web-activities-space.patch
RUN git -C /src/web apply --check /tmp/web-activities-space.patch \
    && git -C /src/web apply /tmp/web-activities-space.patch
COPY patches/web/0009-hide-production-version-banners.patch /tmp/web-version-banners.patch
RUN git -C /src/web apply --check /tmp/web-version-banners.patch \
    && git -C /src/web apply /tmp/web-version-banners.patch
COPY patches/web/0010-customer-space-content-only.patch /tmp/web-customer-space-content.patch
RUN git -C /src/web apply --check /tmp/web-customer-space-content.patch \
    && git -C /src/web apply /tmp/web-customer-space-content.patch
COPY patches/web/0011-customer-sharing-surface.patch /tmp/web-customer-sharing-surface.patch
RUN git -C /src/web apply --check /tmp/web-customer-sharing-surface.patch \
    && git -C /src/web apply /tmp/web-customer-sharing-surface.patch
COPY patches/web/0012-office-app-launchers.patch /tmp/web-office-app-launchers.patch
RUN git -C /src/web apply --check /tmp/web-office-app-launchers.patch \
    && git -C /src/web apply /tmp/web-office-app-launchers.patch
COPY patches/web/0013-infinidrive-theme-sync.patch /tmp/web-theme-sync.patch
RUN git -C /src/web apply --check /tmp/web-theme-sync.patch \
    && git -C /src/web apply /tmp/web-theme-sync.patch
COPY patches/web/0014-file-versioning.patch /tmp/web-file-versioning.patch
RUN git -C /src/web apply --check /tmp/web-file-versioning.patch \
    && git -C /src/web apply /tmp/web-file-versioning.patch
COPY web-photos /src/web/packages/web-app-photos
COPY web-open-in-app /src/web/packages/web-app-open-in-app
COPY --from=web-extensions-source /src/web-extensions/packages/web-app-json-viewer /src/web/packages/web-app-json-viewer
COPY --from=web-extensions-source /src/web-extensions/packages/web-app-unzip /src/web/packages/web-app-unzip
COPY --from=web-extensions-source /src/web-extensions/packages/web-app-progress-bars /src/web/packages/web-app-progress-bars
COPY patches/web/0015-progress-bars-dark-theme.patch /tmp/web-progress-bars-dark-theme.patch
RUN git -C /src/web apply --check /tmp/web-progress-bars-dark-theme.patch \
    && git -C /src/web apply /tmp/web-progress-bars-dark-theme.patch
COPY patches/web/0016-provisioning-access-denied-message.patch /tmp/web-provisioning-message.patch
RUN git -C /src/web apply --check /tmp/web-provisioning-message.patch \
    && git -C /src/web apply /tmp/web-provisioning-message.patch
RUN for package in \
      /src/web/packages/web-app-json-viewer/package.json \
      /src/web/packages/web-app-unzip/package.json \
      /src/web/packages/web-app-progress-bars/package.json; do \
      sed -i -E 's#("@ownclouders/[^"]+":) "[^"]+"#\1 "workspace:*"#g' "$package"; \
      sed -i -E 's#("vue":) "[^"]+"#\1 "3.5.29"#g' "$package"; \
      sed -i -E 's#("vue3-gettext":) "[^"]+"#\1 "2.4.0"#g' "$package"; \
    done

WORKDIR /src/web
RUN --mount=type=cache,id=pnpm-store,target=/root/.local/share/pnpm/store \
    pnpm install --no-frozen-lockfile \
    && pnpm build \
    && cp config/config.json.dist dist/config.json \
    && pnpm licenses:csv \
    && pnpm licenses:save

FROM ${NODE_IMAGE} AS ocis-generate

ARG OCIS_COMMIT=730d9a047a1ade7134e0fda9e0c9c75e99826cd1
ARG OCIS_REPOSITORY=https://github.com/owncloud/ocis.git

RUN apk add --no-cache bash curl git make tar \
    && corepack enable
RUN mkdir -p /src/ocis \
    && git -C /src/ocis init \
    && git -C /src/ocis remote add origin "${OCIS_REPOSITORY}" \
    && git -C /src/ocis fetch --depth=1 origin "${OCIS_COMMIT}" \
    && git -C /src/ocis checkout --detach FETCH_HEAD \
    && test "$(git -C /src/ocis rev-parse HEAD)" = "${OCIS_COMMIT}"

COPY patches/ocis/0001-brand-idp-footer.patch /tmp/ocis.patch
RUN git -C /src/ocis apply --check /tmp/ocis.patch \
    && git -C /src/ocis apply /tmp/ocis.patch
COPY patches/ocis/0002-enable-photos-app.patch /tmp/ocis-photos.patch
RUN git -C /src/ocis apply --check /tmp/ocis-photos.patch \
    && git -C /src/ocis apply /tmp/ocis-photos.patch
COPY patches/ocis/0003-force-collabora-light-theme.patch /tmp/ocis-collabora-theme.patch
RUN git -C /src/ocis apply --check /tmp/ocis-collabora-theme.patch \
    && git -C /src/ocis apply /tmp/ocis-collabora-theme.patch
COPY patches/ocis/0004-file-versioning.patch /tmp/ocis-file-versioning.patch
RUN git -C /src/ocis apply --check /tmp/ocis-file-versioning.patch \
    && git -C /src/ocis apply /tmp/ocis-file-versioning.patch
COPY patches/ocis/0005-ocdav-gateway-retry.patch /tmp/ocis-gateway-retry.patch
RUN git -C /src/ocis apply --check /tmp/ocis-gateway-retry.patch \
    && git -C /src/ocis apply /tmp/ocis-gateway-retry.patch

WORKDIR /src/ocis/ocis
RUN --mount=type=cache,id=ocis-pnpm-store,target=/root/.local/share/pnpm/store \
    make ci-node-generate

RUN rm -rf /src/ocis/services/web/assets/core \
    && mkdir -p /src/ocis/services/web/assets/core
COPY --from=web-build /src/web/dist/ /src/ocis/services/web/assets/core/
RUN find /src/ocis -type d -name node_modules -prune -exec rm -rf '{}' \;

FROM ${GO_IMAGE} AS ocis-build

ENV PATH=/usr/local/go/bin:/go/bin:${PATH}
RUN apk add --no-cache bash build-base git make vips-dev
COPY --from=ocis-generate /src/ocis /src/ocis

WORKDIR /src/ocis/ocis
RUN --mount=type=cache,id=go-build,target=/root/.cache/go-build \
    --mount=type=cache,id=go-mod,target=/go/pkg/mod \
    GOMAXPROCS=2 GOFLAGS=-p=2 make ci-go-generate build ENABLE_VIPS=true GO=/usr/local/go/bin/go

FROM ${RUNTIME_IMAGE} AS runtime

ARG BUILD_DATE
ARG CI_PROJECT_URL=https://gitlab.com/roma.kononov/storage
ARG CI_COMMIT_SHA=local
ARG OCIS_COMMIT=730d9a047a1ade7134e0fda9e0c9c75e99826cd1
ARG WEB_COMMIT=b16a34fdf5569d2fd63b1ff3c575c1a4ee569ad3

ENV OCIS_BASE_DATA_PATH=/var/lib/ocis

RUN apk add --no-cache attr ca-certificates curl mailcap tree vips \
    && addgroup -g 1000 ocis \
    && adduser -D -u 1000 -G ocis ocis \
    && printf 'hosts: files dns\n' > /etc/nsswitch.conf \
    && mkdir -p /etc/ocis /var/lib/ocis \
        /usr/share/licenses/ocis /usr/share/licenses/web /usr/share/source-offer \
    && chown -R 1000:1000 /etc/ocis /var/lib/ocis

COPY --from=ocis-build /src/ocis/ocis/bin/ocis /usr/bin/ocis
COPY --from=ocis-build /src/ocis/LICENSE /usr/share/licenses/ocis/LICENSE
COPY --from=web-build /src/web/LICENSE /usr/share/licenses/web/LICENSE
COPY --from=web-extensions-source /src/web-extensions/LICENSE /usr/share/licenses/web-extensions/LICENSE
COPY --from=web-build /src/web/third-party-licenses/ /usr/share/licenses/web/third-party/
COPY patches/web/0001-hide-runtime-version.patch /usr/share/source-offer/web-ui.patch
COPY patches/web/0002-infinidrive-single-space.patch /usr/share/source-offer/web-single-space.patch
COPY patches/web/0003-infinidrive-metadata-namespace.patch /usr/share/source-offer/web-metadata-namespace.patch
COPY patches/web/0004-customer-account-menu.patch /usr/share/source-offer/web-customer-account.patch
COPY patches/web/0005-disable-recipient-search.patch /usr/share/source-offer/web-disable-recipient-search.patch
COPY patches/web/0006-filter-customer-trash.patch /usr/share/source-offer/web-filter-customer-trash.patch
COPY patches/web/0007-render-pdf-in-iframe.patch /usr/share/source-offer/web-pdf-iframe.patch
COPY patches/web/0008-infinidrive-activities-space.patch /usr/share/source-offer/web-activities-space.patch
COPY patches/web/0009-hide-production-version-banners.patch /usr/share/source-offer/web-version-banners.patch
COPY patches/web/0010-customer-space-content-only.patch /usr/share/source-offer/web-customer-space-content.patch
COPY patches/web/0011-customer-sharing-surface.patch /usr/share/source-offer/web-customer-sharing-surface.patch
COPY patches/web/0012-office-app-launchers.patch /usr/share/source-offer/web-office-app-launchers.patch
COPY patches/web/0013-infinidrive-theme-sync.patch /usr/share/source-offer/web-theme-sync.patch
COPY patches/web/0014-file-versioning.patch /usr/share/source-offer/web-file-versioning.patch
COPY patches/web/0015-progress-bars-dark-theme.patch /usr/share/source-offer/web-progress-bars-dark-theme.patch
COPY patches/web/0016-provisioning-access-denied-message.patch /usr/share/source-offer/web-provisioning-message.patch
COPY web-photos /usr/share/source-offer/web-app-photos
COPY web-open-in-app /usr/share/source-offer/web-app-open-in-app
COPY --from=web-extensions-source /src/web-extensions/packages/web-app-json-viewer /usr/share/source-offer/web-app-json-viewer
COPY --from=web-extensions-source /src/web-extensions/packages/web-app-unzip /usr/share/source-offer/web-app-unzip
COPY --from=web-extensions-source /src/web-extensions/packages/web-app-progress-bars /usr/share/source-offer/web-app-progress-bars
COPY patches/ocis/0001-brand-idp-footer.patch /usr/share/source-offer/ocis-idp.patch
COPY patches/ocis/0002-enable-photos-app.patch /usr/share/source-offer/ocis-photos-default.patch
COPY patches/ocis/0003-force-collabora-light-theme.patch /usr/share/source-offer/ocis-collabora-theme.patch
COPY patches/ocis/0004-file-versioning.patch /usr/share/source-offer/ocis-file-versioning.patch
COPY patches/ocis/0005-ocdav-gateway-retry.patch /usr/share/source-offer/ocis-gateway-retry.patch
COPY SOURCE.md /usr/share/source-offer/SOURCE.md

LABEL org.opencontainers.image.title="InfiniDrive oCIS" \
      org.opencontainers.image.description="Source-built oCIS 8.1.0 with source-built Web 12.5.0" \
      org.opencontainers.image.source="${CI_PROJECT_URL}" \
      org.opencontainers.image.revision="${CI_COMMIT_SHA}" \
      org.opencontainers.image.created="${BUILD_DATE}" \
      org.opencontainers.image.version="8.1.0-web.12.5.0" \
      org.opencontainers.image.licenses="Apache-2.0 AND AGPL-3.0-only" \
      com.cloud-nodes.ocis.revision="${OCIS_COMMIT}" \
      com.cloud-nodes.web.revision="${WEB_COMMIT}"

USER 1000:1000
ENTRYPOINT ["/usr/bin/ocis"]
CMD ["server"]
