ARG CMAKE_VERSION_SYSTEM="3.30.9"
ARG CMAKE_VERSION_SDKMANAGER_ANDROID="3.30.5"

################################################################################
###  Build cmake                                                             ###
################################################################################
FROM ubuntu:jammy as cmake_builder
ARG CMAKE_VERSION_SYSTEM
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Europe/Paris
RUN apt update && apt install -y lsb-release g++ build-essential wget git libssl-dev pkg-config
RUN wget https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION_SYSTEM}/cmake-${CMAKE_VERSION_SYSTEM}.tar.gz
RUN tar -xvf cmake-${CMAKE_VERSION_SYSTEM}.tar.gz
RUN cd cmake-${CMAKE_VERSION_SYSTEM} && ./bootstrap && make -j && make install && cpack -G TGZ -C Release && mv cmake-${CMAKE_VERSION_SYSTEM}-Linux-x86_64.tar.gz ../cmake.tar.gz

################################################################################
###  Build cmake (focal)                                                     ###
################################################################################
FROM ubuntu:focal as cmake_builder_focal
ARG CMAKE_VERSION_SYSTEM
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Europe/Paris
RUN apt update && apt install -y lsb-release g++ build-essential wget git libssl-dev pkg-config
RUN wget https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION_SYSTEM}/cmake-${CMAKE_VERSION_SYSTEM}.tar.gz
RUN tar -xvf cmake-${CMAKE_VERSION_SYSTEM}.tar.gz
RUN cd cmake-${CMAKE_VERSION_SYSTEM} && ./bootstrap && make -j && make install && cpack -G TGZ -C Release && mv cmake-${CMAKE_VERSION_SYSTEM}-Linux-x86_64.tar.gz ../cmake.tar.gz

################################################################################
###  Prepare generic builder                                                 ###
################################################################################
FROM ubuntu:jammy as base_builder
RUN apt update && apt install -y lsb-release g++ build-essential wget git libssl-dev pkg-config
COPY --from=cmake_builder cmake.tar.gz ./cmake.tar.gz
RUN mkdir -p cmake_folder && cd cmake_folder && tar -xvf ../cmake.tar.gz --strip-components=1 && cp -rf * /usr/local/ && cd ..
RUN useradd builder -m
RUN mkdir -p /home/builder/cc3dsfs
RUN mkdir -p /home/builder/building
RUN chown -R builder:builder /home/builder
COPY linux_install_shared_packages.sh /
RUN chmod +x /linux_install_shared_packages.sh ; /linux_install_shared_packages.sh

################################################################################
###  Build x86_32                                                            ###
################################################################################
FROM base_builder as builder32
ENV ARCH_USABLE=i386
ENV ARCH_COMPILER_NAME=i686-linux-gnu
COPY linux_install_arch_packages.sh /
RUN chmod +x /linux_install_arch_packages.sh ; /linux_install_arch_packages.sh ${ARCH_USABLE}
COPY linux_compile_install_packages.sh /
RUN chmod +x /linux_compile_install_packages.sh ; /linux_compile_install_packages.sh ${ARCH_COMPILER_NAME}
WORKDIR /home/builder/building
COPY  docker_build_starter /
RUN chmod +x /docker_build_starter
USER builder
CMD /docker_build_starter docker-scripts/docker_build_x86_32

################################################################################
###  Build x86_64                                                            ###
################################################################################
FROM base_builder as builder64
ENV ARCH_USABLE=amd64
ENV ARCH_COMPILER_NAME=x86_64-linux-gnu
COPY linux_install_arch_packages.sh /
RUN chmod +x /linux_install_arch_packages.sh ; /linux_install_arch_packages.sh ${ARCH_USABLE}
COPY linux_compile_install_packages.sh /
RUN chmod +x /linux_compile_install_packages.sh ; /linux_compile_install_packages.sh ${ARCH_COMPILER_NAME}
WORKDIR /home/builder/building
COPY  docker_build_starter /
RUN chmod +x /docker_build_starter
USER builder
CMD /docker_build_starter docker-scripts/docker_build_x86_64

################################################################################
###  Build arm 32                                                            ###
################################################################################
FROM base_builder as builderarm32
ENV ARCH_USABLE=armhf
ENV ARCH_COMPILER_NAME=arm-linux-gnueabihf
COPY linux_install_arch_packages.sh /
RUN chmod +x /linux_install_arch_packages.sh ; /linux_install_arch_packages.sh ${ARCH_USABLE}
COPY linux_compile_install_packages.sh /
RUN chmod +x /linux_compile_install_packages.sh ; /linux_compile_install_packages.sh ${ARCH_COMPILER_NAME}
WORKDIR /home/builder/building
COPY  docker_build_starter /
RUN chmod +x /docker_build_starter
USER builder
CMD /docker_build_starter docker-scripts/docker_build_arm32

################################################################################
###  Build arm 64                                                            ###
################################################################################
FROM base_builder as builderarm64
ENV ARCH_USABLE=arm64
ENV ARCH_COMPILER_NAME=aarch64-linux-gnu
COPY linux_install_arch_packages.sh /
RUN chmod +x /linux_install_arch_packages.sh ; /linux_install_arch_packages.sh ${ARCH_USABLE}
COPY linux_compile_install_packages.sh /
RUN chmod +x /linux_compile_install_packages.sh ; /linux_compile_install_packages.sh ${ARCH_COMPILER_NAME}
WORKDIR /home/builder/building
COPY  docker_build_starter /
RUN chmod +x /docker_build_starter
USER builder
CMD /docker_build_starter docker-scripts/docker_build_arm64

################################################################################
###  Build riscv 64                                                          ###
################################################################################
FROM base_builder as builderriscv64
ENV ARCH_USABLE=riscv64
ENV ARCH_COMPILER_NAME=riscv64-linux-gnu
COPY linux_install_arch_packages.sh /
RUN chmod +x /linux_install_arch_packages.sh ; /linux_install_arch_packages.sh ${ARCH_USABLE}
COPY linux_compile_install_packages.sh /
RUN chmod +x /linux_compile_install_packages.sh ; /linux_compile_install_packages.sh ${ARCH_COMPILER_NAME}
WORKDIR /home/builder/building
COPY  docker_build_starter /
RUN chmod +x /docker_build_starter
USER builder
CMD /docker_build_starter docker-scripts/docker_build_riscv64

################################################################################
###  Build Android without caching                                           ###
################################################################################
# Different from all others
FROM saschpe/android-ndk:31-jdk11.0.13_8-ndk23.0.7599858-cmake3.18.1 as builderandroid_nocache
ARG CMAKE_VERSION_SDKMANAGER_ANDROID
RUN apt update && apt install -y lsb-release g++ build-essential wget git libssl-dev pkg-config zip unzip
RUN curl -s "https://get.sdkman.io" | bash
RUN /bin/bash -c "source /root/.sdkman/bin/sdkman-init.sh ; sdk install gradle 7.6.2"
RUN sdkmanager "build-tools;30.0.3" "cmake;${CMAKE_VERSION_SDKMANAGER_ANDROID}"
ENV ANDROID_SDK_VERSION="31"
ENV ANDROID_HOME="/opt/android-sdk-linux"
ENV ANDROID_NDK_VERSION="23.0.7599858"
ENV ANDROID_NDK_HOME="$ANDROID_HOME/ndk/$ANDROID_NDK_VERSION"
ENV GRADLE_DIR="/home/builder/gradle"
ENV GRADLE_HOME="$GRADLE_DIR/current"
ENV GRADLE_USER_HOME=$GRADLE_HOME
ENV PATH="$ANDROID_HOME:$GRADLE_HOME/bin:$ANDROID_NDK_HOME:$PATH"
RUN useradd builder -m
RUN mkdir -p /home/builder/cc3dsfs
RUN mkdir -p /home/builder/building
RUN chown -R builder:builder /home/builder
WORKDIR /home/builder/building
COPY  docker_build_starter /
RUN chmod +x /docker_build_starter
RUN chown -R builder:builder $ANDROID_HOME
RUN mkdir -p $GRADLE_DIR
RUN mv /root/.sdkman/candidates/gradle/* $GRADLE_DIR/
RUN chown -R builder:builder $GRADLE_DIR
COPY --from=cmake_builder_focal cmake.tar.gz /cmake.tar.gz
RUN mkdir -p /cmake_folder && cd /cmake_folder && tar -xvf /cmake.tar.gz --strip-components=1 && cp -rf * /usr/local/
USER builder
CMD /docker_build_starter docker-scripts/docker_build_android

################################################################################
###  Build Android with caching                                              ###
################################################################################
# Different from all others
FROM builderandroid_nocache as builderandroid
RUN git clone https://github.com/Lorenzooone/cc3dsfs.git
RUN mv cc3dsfs ../cc3dsfs_to_cache
RUN cd ../cc3dsfs_to_cache ; chmod -R +x . ; ./docker-scripts/docker_build_android
RUN rm -rf ../cc3dsfs_to_cache
CMD /docker_build_starter docker-scripts/docker_build_android
