FROM eclipse-temurin:21-jdk-noble AS build

RUN apt update && apt install -y git curl gcc && rm -rf /var/lib/apt/lists/*
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile minimal
ENV PATH="/root/.cargo/bin:$PATH"

WORKDIR /r4j

RUN git clone --single-branch --depth 1 https://github.com/TeamPiped/reqwest4j.git ./

RUN sed -E -i \
    -e 's/command\.set\("cross"\)/command.set("cargo")/' \
    -e '/targets \+= target/d' \
    -e '/command\.set\("cargo"\)/a targets += target("x86_64-unknown-linux-gnu", "libreqwest.so")' \
    reqwest-jni/build.gradle.kts

RUN ./gradlew shadowJar
RUN find build/libs -name 'reqwest4j-*.jar' -exec mv {} /r4j/lib.jar \;

WORKDIR /app

RUN git clone --single-branch --depth 1 https://github.com/TeamPiped/Piped-Backend.git ./

RUN sed -i \
    -e "s#implementation 'rocks.kavin:reqwest4j:.*'#implementation files('/r4j/lib.jar')#" \
    build.gradle

RUN ./gradlew shadowJar
RUN sh -c 'echo "$(git log -1 --date=short --pretty=format:%cd)-$(git rev-parse --short HEAD)" >VERSION'

# ------

FROM git.dc09.ru/darkcat09/openj9:latest AS run

WORKDIR /app

COPY --from=build /app/build/libs/piped-1.0-all.jar /app/piped.jar
COPY --from=build /app/VERSION /app/VERSION

COPY ./piped.sh ./
CMD ["/app/piped.sh"]

# ------