ARG LANGUAGETOOL_VERSION=6.8
ARG PROD_LANGUAGETOOL_IMAGE=languagetool

FROM --platform=$BUILDPLATFORM debian:trixie-slim AS build

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        openjdk-21-jdk-headless \
        git \
        maven \
        unzip

ARG LANGUAGETOOL_VERSION
RUN git clone https://github.com/languagetool-org/languagetool.git --depth 1 -b v${LANGUAGETOOL_VERSION}
WORKDIR /languagetool

# Add PostgreSQL support and build languagetool
RUN sed -i '/^\s*<\/dataSource>/i<property name="driver.prepareThreshold" value="0"\/>' languagetool-server/src/main/resources/org/languagetool/server/mybatis-config.xml \
    && sed -i '/^\s*<\/dependencies>/i<dependency>\n<groupId>org.postgresql<\/groupId>\n<artifactId>postgresql<\/artifactId>\n<version>42.7.8<\/version>\n<\/dependency>' languagetool-server/pom.xml \
    && sed -i '/<repositories>/,/<\/repositories>/d' pom.xml \
    && mvn --projects languagetool-standalone --also-make package -DskipTests \
        -Daether.connector.connectTimeout=5000 \
        -Daether.connector.requestTimeout=30000 \
    && unzip /languagetool/languagetool-standalone/target/LanguageTool-${LANGUAGETOOL_VERSION}.zip -d /dist \
    && mv /dist/LanguageTool-${LANGUAGETOOL_VERSION} /dist/LanguageTool

# Additional hunspell dictionaries
ARG HUNSPELL_LANGUAGE_DIRS="sq_AL bg_BG cs_CZ et_EE hr_HR hu_HU lt_LT lv_LV no sr tr_TR"
RUN git clone https://github.com/LibreOffice/dictionaries.git -b libreoffice-26.2.4.2 --depth 1 --no-checkout --filter=tree:0 /hunspell-dictionaries \
    && cd /hunspell-dictionaries \
    && git sparse-checkout set --no-cone ${HUNSPELL_LANGUAGE_DIRS} \
    && git checkout \
    && for lang in ${HUNSPELL_LANGUAGE_DIRS}; do \
        mkdir -p /dist/LanguageTool/org/languagetool/resource/${lang}/hunspell \
        && cp -r ${lang}/. /dist/LanguageTool/org/languagetool/resource/${lang}/hunspell/ \
        && touch /dist/LanguageTool/org/languagetool/resource/${lang}/hunspell/common_words.txt; \
    done

# Add scripts and custom wordlists
WORKDIR /dist/LanguageTool
COPY scripts/* .
COPY wordlists /custom-wordlists/
RUN ./add_wordlists.sh


FROM alpine:3.22 AS languagetool

RUN apk add --no-cache \
    curl \
    libc6-compat \
    libhunspell \
    openjdk17-jre-headless \
    zip
RUN addgroup --system languagetool \
    && adduser --system --uid=100 -G languagetool languagetool
COPY --chown=languagetool --from=build /dist/LanguageTool /LanguageTool
WORKDIR /LanguageTool

# Hunspell arm64 workaround
RUN if [ "$(uname -m)" = "aarch64" ]; then \
      mkdir -p /hunspell/linux-aarch64/ \
      && cp /usr/lib/libhunspell-1.7.so.0.0.1 /hunspell/linux-aarch64/libhunspell.so \
      && cd /hunspell \
      && zip /LanguageTool/libs/hunspell.jar linux-aarch64/libhunspell.so \
      && rm -rf /hunspell; \
    fi

USER 100
EXPOSE 8010
HEALTHCHECK --timeout=10s --start-period=5s CMD curl --fail --data "language=en-US&text=a simple test" http://localhost:8010/v2/check || exit 1
CMD ["/LanguageTool/start.sh"]


FROM ${PROD_LANGUAGETOOL_IMAGE} AS languagetool-src
USER 0
ARG LANGUAGETOOL_VERSION
RUN apk add --no-cache git
RUN git clone https://github.com/languagetool-org/languagetool.git --depth 1 -b v${LANGUAGETOOL_VERSION} /languagetool-src
USER 100

FROM languagetool