FROM eclipse-temurin:17-jre-jammy

ARG SERVICE_VERSION="dev"
ENV RC_SERVICE_NAME="rc-service-external"
ENV RC_SERVICE_VERSION="${SERVICE_VERSION}"

ENV JVM_OPTION="-XX:InitialRAMPercentage=65.0 -XX:MaxRAMPercentage=65.0"
ENV SERVER_PORT="9000"
ENV SERVER_PROFILE_ACTIVE="prod"

RUN apt-get update \
    && apt-get install -y --no-install-recommends wget ca-certificates \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /application
COPY external-start/target/rc-service-external-*.jar /tmp/service-jars/
RUN set -e; \
    jars="$(find /tmp/service-jars -maxdepth 1 -type f -name 'rc-service-external-*.jar' | sort)"; \
    count="$(printf '%s\n' "$jars" | grep -c . || true)"; \
    if [ "$count" -ne 1 ]; then \
      echo "Expected exactly one rc-service-external jar, found $count:"; \
      ls -la /tmp/service-jars; \
      exit 1; \
    fi; \
    cp "$jars" /application/service.jar \
    && rm -rf /tmp/service-jars

CMD exec java -Djava.security.egd=file:/dev/./urandom \
              -Dnacos.remote.client.grpc.timeout=6000 \
              -Dnacos.remote.client.grpc.server.check.timeout=6000 \
              -Dnacos.remote.client.grpc.health.timeout=6000 \
              --add-opens java.base/java.lang.invoke=ALL-UNNAMED \
              --add-opens java.base/java.io=ALL-UNNAMED \
              --add-opens java.base/java.math=ALL-UNNAMED \
              --add-opens java.base/java.net=ALL-UNNAMED \
              --add-opens java.base/java.nio=ALL-UNNAMED \
              --add-opens java.base/java.security=ALL-UNNAMED \
              --add-opens java.base/java.text=ALL-UNNAMED \
              --add-opens java.base/java.time=ALL-UNNAMED \
              --add-opens java.base/java.util=ALL-UNNAMED \
              --add-opens java.base/jdk.internal.access=ALL-UNNAMED \
              --add-opens java.base/jdk.internal.misc=ALL-UNNAMED \
              $JVM_OPTION \
              -jar /application/service.jar \
              --server.port=$SERVER_PORT \
              --spring.profiles.active=$SERVER_PROFILE_ACTIVE
