# set Java
ARG JAVA_VERSION=21.0.7_6
FROM eclipse-temurin:${JAVA_VERSION}-jre-alpine as build

ARG HAWKBIT_APP=hawkbit-repository-jpa-init
ARG HAWKBIT_VERSION=0-SNAPSHOT

ENV APP=${HAWKBIT_APP}
ENV VERSION=${HAWKBIT_VERSION}

RUN addgroup -S hawkbit_group && adduser -D hawkbit -G hawkbit_group
RUN mkdir -p /artifactrepo && chown -R hawkbit /artifactrepo
USER hawkbit

COPY org/eclipse/hawkbit/${APP}/${VERSION}/${APP}-${VERSION}.jar /${APP}-${VERSION}.jar

ARG X_MS=256m
ENV X_MS=${X_MS}
ARG X_MX=256m
ENV X_MX=${X_MX}
ARG XX_MAX_METASPACE_SIZE=1150m
ENV XX_MAX_METASPACE_SIZE=${XX_MAX_METASPACE_SIZE}
ARG XX_METASPACE_SIZE=115m
ENV XX_METASPACE_SIZE=${XX_METASPACE_SIZE}
ARG XSS=150K
ENV XSS=${XSS}
ARG GC=G1
ENV GC=${GC}
ARG AND_THEN="exit 0"
ENV AND_THEN=${AND_THEN}

ENTRYPOINT ["sh", "-c", "java -classpath /${APP}-${VERSION}.jar ${JAVA_OPTS} -Xms${X_MS} -Xmx${X_MX} -XX:MaxMetaspaceSize=${XX_MAX_METASPACE_SIZE} -XX:MetaspaceSize=${XX_METASPACE_SIZE} -Xss${XSS} -XX:+Use${GC}GC -XX:+UseStringDeduplication -XX:+UseCompressedOops -XX:+HeapDumpOnOutOfMemoryError org.eclipse.hawkbit.repository.jpa.init.HawkbitFlywayDbInit ${@} && ${AND_THEN}"]
