FROM ubuntu:24.04

# Install tools
RUN apt-get update && apt-get upgrade -y && apt-get install -y \
	build-essential cmake git \
	mingw-w64-x86-64-dev gcc-mingw-w64-x86-64-posix g++-mingw-w64-x86-64-posix \
	libxext-dev libgl-dev \
	wget zip zstd \
	libpng-dev libboost-all-dev \
	libxi-dev libxrender-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

# Install sdl2
RUN mkdir /home/sdl2
WORKDIR /home/sdl2
RUN wget https://github.com/libsdl-org/SDL/releases/download/release-2.30.6/SDL2-devel-2.30.6-mingw.tar.gz
RUN tar xvf SDL2-devel-2.30.6-mingw.tar.gz
RUN mv SDL2-2.30.6/x86_64-w64-mingw32 ./install
RUN rm -r SDL2-devel-2.30.6-mingw.tar.gz SDL2-2.30.6

# Install boost
RUN mkdir /home/boost
WORKDIR /home/boost
RUN wget https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-boost-1.85.0-4-any.pkg.tar.zst
RUN tar xvf *.zst
RUN mv mingw64 install
RUN rm *.zst

# Install wgpu-native for host
WORKDIR /home
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/host
RUN mv wgpu-native/libwgpu_native.a wgpu-native/install/host

# Install wgpu-native for target
RUN wget https://github.com/gfx-rs/wgpu-native/releases/download/v0.19.4.1/wgpu-windows-x86_64-release.zip
RUN mkdir -p wgpu-native
RUN unzip -o wgpu-*.zip -d wgpu-native
RUN rm wgpu-*.zip
RUN chmod +x wgpu-native/wgpu_native.dll
RUN mkdir -p wgpu-native/install/target
RUN mv wgpu-native/wgpu_native.dll.lib wgpu-native/wgpu_native.dll wgpu-native/install/target

# Install freetype
RUN mkdir -p /home/freetype
WORKDIR /home/freetype
RUN wget https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-freetype-2.13.2-1-any.pkg.tar.zst
RUN tar xvf mingw-w64*
RUN mv mingw64 install
RUN mkdir -p /tmp/freetype-dep

# Instal freetype dependency: zlib
WORKDIR /tmp/freetype-dep
RUN rm -rf ./*
RUN wget https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-zlib-1.3.1-1-any.pkg.tar.zst
RUN tar xvf *.zst
RUN mv mingw64/bin/zlib1.dll /home/freetype/install/bin

# Instal freetype dependency: bzip2
WORKDIR /tmp/freetype-dep
RUN rm -rf ./*
RUN wget https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-bzip2-1.0.8-3-any.pkg.tar.zst
RUN tar xvf *.zst
RUN mv mingw64/bin/libbz2-1.dll /home/freetype/install/bin

# Instal freetype dependency: brotli
WORKDIR /tmp/freetype-dep
RUN rm -rf ./*
RUN wget https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-brotli-1.1.0-2-any.pkg.tar.zst
RUN tar xvf *.zst
RUN mv mingw64/bin/libbrotlidec.dll mingw64/bin/libbrotlicommon.dll /home/freetype/install/bin

# Instal freetype dependency: png
WORKDIR /tmp/freetype-dep
RUN rm -rf ./*
RUN wget https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-libpng-1.6.43-1-any.pkg.tar.zst
RUN tar xvf *.zst
RUN mv mingw64/bin/libpng16-16.dll /home/freetype/install/bin

# Instal freetype dependency: harfbuzz
WORKDIR /tmp/freetype-dep
RUN rm -rf ./*
RUN wget https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-harfbuzz-9.0.0-1-any.pkg.tar.zst
RUN tar xvf *.zst
RUN mv mingw64/bin/libharfbuzz-0.dll /home/freetype/install/bin

# Install freetype dependency: gcc-libs
WORKDIR /tmp/freetype-dep
RUN rm -rf ./*
RUN wget https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-gcc-libs-14.2.0-1-any.pkg.tar.zst
RUN tar xvf *.zst
RUN mv mingw64/bin/libstdc++-6.dll mingw64/bin/libgcc_s_seh-1.dll /home/freetype/install/bin

# Install freetype dependency: glib
WORKDIR /tmp/freetype-dep
RUN rm -rf ./*
RUN wget https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-glib2-2.80.4-1-any.pkg.tar.zst
RUN tar xvf *.zst
RUN mv mingw64/bin/libglib-2.0-0.dll /home/freetype/install/bin

# Install freetype dependency: graphite
WORKDIR /tmp/freetype-dep
RUN rm -rf ./*
RUN wget https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-graphite2-1.3.14-3-any.pkg.tar.zst
RUN tar xvf *.zst
RUN mv mingw64/bin/libgraphite2.dll /home/freetype/install/bin

# Install freetype dependency: gettext-runtime
WORKDIR /tmp/freetype-dep
RUN rm -rf ./*
RUN wget https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-gettext-runtime-0.22.5-2-any.pkg.tar.zst
RUN tar xvf *.zst
RUN mv mingw64/bin/libintl-8.dll /home/freetype/install/bin

# Install freetype dependency: iconv
WORKDIR /tmp/freetype-dep
RUN rm -rf ./*
RUN wget https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-libiconv-1.17-4-any.pkg.tar.zst
RUN tar xvf *.zst
RUN mv mingw64/bin/libiconv-2.dll /home/freetype/install/bin

# Install freetype dependency: pcre2
WORKDIR /tmp/freetype-dep
RUN rm -rf ./*
RUN wget https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-pcre2-10.44-1-any.pkg.tar.zst
RUN tar xvf *.zst
RUN mv mingw64/bin/libpcre2-8-0.dll /home/freetype/install/bin

# Install toolchain files & main packaging script
WORKDIR /home
COPY toolchain-host.cmake .
COPY toolchain-target.cmake .
COPY package.sh .
