FROM alpine:3.14 AS builder

RUN echo "http://dl-cdn.alpinelinux.org/alpine/v3.14/community" >> /etc/apk/repositories && \
    apk update && \
    apk upgrade && \
    apk add alpine-sdk linux-headers git zlib-dev openssl-dev gperf php cmake openjdk8 curl

WORKDIR /build
RUN git clone https://github.com/tdlib/td.git && \
    cd td && \
    mkdir -p build && \
    cd build && \
    cmake -DCMAKE_BUILD_TYPE=Release \
          -DJAVA_HOME=/usr/lib/jvm/java-1.8-openjdk/ \
          -DCMAKE_INSTALL_PREFIX:PATH=../example/java/td \
          -DTD_ENABLE_JNI=ON .. && \
    cmake --build . --target install && \
    cd .. && \
    cd example/java && \
    mkdir -p build && \
    cd build && \
    cmake -DCMAKE_BUILD_TYPE=Release \
          -DJAVA_HOME=/usr/lib/jvm/java-1.8-openjdk/ \
          -DCMAKE_INSTALL_PREFIX:PATH=../../../tdlib \
          -DTd_DIR:PATH=$(readlink -f ../td/lib/cmake/Td) .. && \
    cmake --build . --target install

RUN mkdir -p /output && \
    cp /build/td/tdlib/bin/libtdjni.so /output/ && \
    cp -r /build/td/example/java /output/java

FROM alpine:3.14

WORKDIR /tdlib-output
COPY --from=builder /output /tdlib-output/libs

CMD ["cp", "-r", "/tdlib-output/libs", "/app/tdlib"]
