ARG NEXTCLOUD_BASE_IMAGE=nextcloud:34-apache
ARG EUROOFFICE_NEXTCLOUD_TAG=v11.0.1
ARG GROUPFOLDERS_TAG=v22.0.1

FROM node:24-bookworm-slim AS eurooffice-builder

ARG EUROOFFICE_NEXTCLOUD_TAG

COPY patch-eurooffice-gooffice.sh /tmp/patch-eurooffice-gooffice.sh

RUN set -eux; \
	apt-get update; \
	apt-get install -y --no-install-recommends ca-certificates git python3; \
	git clone --depth 1 --branch "${EUROOFFICE_NEXTCLOUD_TAG}" https://github.com/Euro-Office/eurooffice-nextcloud.git /tmp/eurooffice; \
	git -C /tmp/eurooffice submodule update --init --recursive; \
	sh /tmp/patch-eurooffice-gooffice.sh /tmp/eurooffice; \
	cd /tmp/eurooffice; \
	npm install --include=dev --no-audit --no-fund; \
	npm run build; \
	rm -rf node_modules .git .github; \
	apt-get purge -y --auto-remove git python3; \
	rm -rf /var/lib/apt/lists/* /root/.npm

FROM composer:2 AS composer

FROM ${NEXTCLOUD_BASE_IMAGE}

ARG GROUPFOLDERS_TAG

COPY --from=composer /usr/bin/composer /usr/bin/composer
COPY sync-custom-apps.sh /docker-entrypoint-hooks.d/before-starting/10-sync-custom-apps.sh
COPY gooffice-apache-performance.conf /etc/apache2/conf-enabled/gooffice-performance.conf
COPY gooffice-php-performance.ini /usr/local/etc/php/conf.d/zz-gooffice-performance.ini
COPY custom_apps/gooffice_login /usr/src/nextcloud/custom_apps/gooffice_login
COPY custom_apps/gooffice_mail /usr/src/nextcloud/custom_apps/gooffice_mail
COPY custom_apps/gooffice_office /usr/src/nextcloud/custom_apps/gooffice_office
COPY skeleton /usr/src/nextcloud/gooffice-skeleton
COPY --from=eurooffice-builder /tmp/eurooffice /usr/src/nextcloud/custom_apps/eurooffice

RUN set -eux; \
	chmod +x /docker-entrypoint-hooks.d/before-starting/10-sync-custom-apps.sh; \
	rm -rf /usr/src/nextcloud/gooffice-skeleton/* /usr/src/nextcloud/gooffice-skeleton/.[!.]* /usr/src/nextcloud/gooffice-skeleton/..?*; \
	mkdir -p /usr/src/nextcloud/gooffice-skeleton/Documents /usr/src/nextcloud/gooffice-skeleton/Templates /usr/src/nextcloud/gooffice-skeleton/Shared; \
	apt-get update; \
	apt-get install -y --no-install-recommends ca-certificates curl git unzip; \
	composer --working-dir=/usr/src/nextcloud/custom_apps/eurooffice install --no-dev --no-interaction --prefer-dist --classmap-authoritative; \
	curl -fsSL -o /tmp/groupfolders.tar.gz "https://github.com/nextcloud-releases/groupfolders/releases/download/${GROUPFOLDERS_TAG}/groupfolders-${GROUPFOLDERS_TAG}.tar.gz"; \
	tar -xzf /tmp/groupfolders.tar.gz -C /usr/src/nextcloud/custom_apps; \
	rm -rf /usr/src/nextcloud/custom_apps/eurooffice/.git /usr/src/nextcloud/custom_apps/eurooffice/.github; \
	rm -rf /tmp/groupfolders.tar.gz /root/.composer/cache; \
	apt-get purge -y --auto-remove curl git unzip; \
	rm -rf /var/lib/apt/lists/* /usr/bin/composer
