FROM ubuntu:22.04

# Install tools
RUN apt-get update && apt-get upgrade -y 
RUN apt-get install -y \
	build-essential cmake git default-jre openjdk-19-jdk \
	libxext-dev libgl-dev \
	wget zip zstd \
	libpng-dev libboost-all-dev \
	libxi-dev libxrender-dev

# Set user
RUN useradd -u 1000 -U -d /home -M worker
RUN chown -R worker:worker /home /usr/local
USER worker

# Install android sdkmanager
RUN mkdir -v /home/sdk
WORKDIR /home/sdk
RUN wget https://dl.google.com/android/repository/commandlinetools-linux-9477386_latest.zip && unzip commandlinetools-linux-9477386_latest.zip && rm commandlinetools-linux-9477386_latest.zip
RUN mv -v cmdline-tools latest
RUN mkdir -v cmdline-tools
RUN mv -v latest cmdline-tools

# Install sdk
RUN yes | cmdline-tools/latest/bin/sdkmanager "build-tools;34.0.0" "platforms;android-34"

# Install ndk separately
RUN wget https://dl.google.com/android/repository/android-ndk-r26-beta1-linux.zip && unzip android-ndk-r26-beta1-linux.zip && rm android-ndk-r26-beta1-linux.zip
RUN mkdir -v ndk
RUN mv -v android-ndk-r26-beta1 ndk/26.0.10404224-beta1

# Env variables
ENV SDK_ROOT=/home/sdk
ENV BUILD_TOOLS_ROOT=${SDK_ROOT}/build-tools/34.0.0
ENV NDK_ROOT=${SDK_ROOT}/ndk/26.0.10404224-beta1
ENV PLATFORM_ROOT=${SDK_ROOT}/platforms/android-34
ENV PNG_ROOT=/home/png/install
ENV BOOST_ROOT=/home/boost/install

# Build libpng
RUN mkdir -v /home/png
WORKDIR /home/png
RUN git clone https://github.com/glennrp/libpng.git -b libpng16 --depth 1 source
RUN mkdir -v build install
RUN cmake -S source -B build -DCMAKE_INSTALL_PREFIX=install/arm64-v8a -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE="${NDK_ROOT}/build/cmake/android.toolchain.cmake" \
	-DANDROID_USE_LEGACY_TOOLCHAIN_FILE=ON -DANDROID_PLATFORM=34 -DANDROID_STL=c++_shared -DANDROID_CPP_FEATURES="rtti exceptions" -DANDROID_ABI=arm64-v8a
RUN cmake --build build -t install -j

# Build boost
RUN mkdir -v /home/boost
WORKDIR /home/boost
RUN wget https://boostorg.jfrog.io/artifactory/main/release/1.82.0/source/boost_1_82_0.tar.gz && tar xvf boost_1_82_0.tar.gz && rm boost_1_82_0.tar.gz
RUN mv -v boost_1_82_0 source
RUN cd source && ./bootstrap.sh --with-libraries=random --prefix=../install/arm64-v8a
RUN mkdir -p build install
RUN echo "using clang : android : ${NDK_ROOT}/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=aarch64-none-linux-android34 --sysroot=${NDK_ROOT}/toolchains/llvm/prebuilt/linux-x86_64/sysroot ;" > user-config.jam
RUN cd source && ./b2 toolset=clang-android target-os=android architecture=arm variant=release link=shared threading=single cxxflags=-fPIC --user-config=../user-config.jam --build-dir=../build install

# Finalize
WORKDIR /home
COPY package.sh package-helper.sh copy-files.sh ./