FROM ubuntu:24.04

# Install tools
RUN apt-get update && apt-get upgrade -y && apt-get install -y \
	build-essential chrpath cmake git \
	file wget zip \
	libpng-dev libxext-dev libgl-dev libboost-all-dev libmpg123-dev \
	libxi-dev libxrender-dev \
	libasound2-dev libpulse-dev \
	libfreetype-dev

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

# Build sdl2
RUN mkdir /home/sdl2
WORKDIR /home/sdl2
RUN ls -lh /home
RUN wget https://www.libsdl.org/release/SDL2-2.0.22.tar.gz
RUN tar xvf SDL2-2.0.22.tar.gz
RUN mv SDL2-2.0.22 source
RUN rm SDL2-2.0.22.tar.gz
COPY sdl2/* ./
RUN ./make.sh
RUN rm -r source build options make.sh
WORKDIR /home

# Build sdl2-mixer
RUN mkdir /home/sdl2-mixer
WORKDIR /home/sdl2-mixer
RUN wget https://github.com/libsdl-org/SDL_mixer/archive/refs/tags/release-2.6.1.tar.gz
RUN tar xvf release-2.6.1.tar.gz
RUN mv SDL_mixer-release-2.6.1 source
RUN rm release-2.6.1.tar.gz
COPY sdl2-mixer/* ./
RUN ./make.sh
WORKDIR /home
RUN rm -r sdl2-mixer

# Install blender
WORKDIR /home
RUN wget https://download.blender.org/release/Blender3.2/blender-3.2.0-linux-x64.tar.xz
RUN tar xvf blender-3.2.0-linux-x64.tar.xz
RUN mv blender-3.2.0-linux-x64 blender
RUN rm blender-3.2.0-linux-x64.tar.xz
RUN ln -sv /home/blender/blender /usr/local/bin/blender

# Copy system-provided freetype
RUN cp `readlink -f /usr/lib/x86_64-linux-gnu/libfreetype.so` libfreetype.so
RUN mv libfreetype.so `objdump -p libfreetype.so | grep SONAME | awk '{print $2}'`

# Install wgpu-native
RUN wget https://github.com/gfx-rs/wgpu-native/releases/download/v0.19.4.1/wgpu-linux-x86_64-release.zip
RUN mkdir -p wgpu-native
RUN unzip wgpu-*.zip -d wgpu-native
RUN rm wgpu-*.zip
RUN mkdir -p wgpu-native/install
RUN mv wgpu-native/libwgpu_native.a wgpu-native/install

# Install toolchain file & main packaging script
COPY toolchain.cmake .
COPY package.sh .
