ARG product_version=6.4.2
ARG build_number=6
ARG oo_root='/var/www/onlyoffice/documentserver'


## Setup
FROM onlyoffice/documentserver:${product_version}.${build_number} as setup-stage
ARG product_version
ARG build_number
ARG oo_root

ENV PRODUCT_VERSION=${product_version}
ENV BUILD_NUMBER=${build_number}

ARG build_deps="git make g++ nodejs npm jq"
RUN apt-get update && apt-get install -y ${build_deps}
RUN npm install -g pkg grunt grunt-cli

WORKDIR /build


## Clone
FROM setup-stage as clone-stage
ARG tag=v${PRODUCT_VERSION}.${BUILD_NUMBER}

RUN git clone --quiet --branch $tag --depth 1 https://github.com/ONLYOFFICE/build_tools.git /build/build_tools
RUN git clone --quiet --branch $tag --depth 1 https://github.com/ONLYOFFICE/server.git      /build/server

COPY server.patch /build/

RUN cd /build/server   && git apply /build/server.patch


## Build
FROM clone-stage as build-stage

# build server with license checks patched
WORKDIR /build/server
RUN make
RUN pkg /build/build_tools/out/linux_64/onlyoffice/documentserver/server/FileConverter --targets=node10-linux -o /build/converter
RUN pkg /build/build_tools/out/linux_64/onlyoffice/documentserver/server/DocService --targets=node10-linux --options max_old_space_size=4096 -o /build/docservice

# add fonts
COPY ./fonts/ /usr/share/fonts/truetype/

RUN mv /etc/onlyoffice/documentserver/default.json /etc/onlyoffice/documentserver/default-origin.json
COPY ./default-override.json /etc/onlyoffice/documentserver/
RUN jq -s '.[0] * .[1]' /etc/onlyoffice/documentserver/default-origin.json /etc/onlyoffice/documentserver/default-override.json -r > /etc/onlyoffice/documentserver/default.json

## Final image
FROM onlyoffice/documentserver:${product_version}.${build_number}
ARG oo_root

# server
COPY --from=build-stage /build/converter  ${oo_root}/server/FileConverter/converter
COPY --from=build-stage /build/docservice ${oo_root}/server/DocService/docservice

# fonts
COPY ./fonts/ /usr/share/fonts/truetype/

# config
COPY --from=build-stage /etc/onlyoffice/documentserver/default.json /etc/onlyoffice/documentserver/default.json

