# 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=pass_as_buils_arg

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

COPY KEY .

RUN set -x &&\
 apk add --no-cache --virtual build-dependencies gnupg unzip libressl wget &&\
 gpg --import KEY &&\
 wget -O ${APP}-${VERSION}.jar --no-verbose https://repo1.maven.org/maven2/org/eclipse/hawkbit/${APP}/${VERSION}/${APP}-${VERSION}.jar &&\
 wget -O ${APP}-${VERSION}.jar.asc --no-verbose https://repo1.maven.org/maven2/org/eclipse/hawkbit/${APP}/${VERSION}/${APP}-${VERSION}.jar.asc &&\
 gpg --batch --verify ${APP}-${VERSION}.jar.asc ${APP}-${VERSION}.jar &&\
 rm ${APP}-${VERSION}.jar.asc KEY &&\
 apk del build-dependencies

FROM eclipse-temurin:${JAVA_VERSION}-jre-alpine

RUN addgroup -S hawkbit_group && adduser -D hawkbit -G hawkbit_group
USER hawkbit

ARG HAWKBIT_APP=hawkbit-repository-jpa-init
ARG HAWKBIT_VERSION=pass_as_buils_arg

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

COPY --from=build /${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}"]
