# Ryzom client build environment
# Based on Steam Runtime 1 'scout' SDK for maximum glibc compatibility
FROM registry.gitlab.steamos.cloud/steamrt/scout/sdk:latest

LABEL maintainer="Ryzom Community"
LABEL description="Build environment for the Ryzom MMORPG client"

ENV DEBIAN_FRONTEND=noninteractive

# System dependencies for NeL engine and Ryzom client
RUN apt-get update && apt-get install -y --no-install-recommends \
	build-essential \
	gcc-4.8 \
	g++-4.8 \
	pkg-config \
	git \
	wget \
	ca-certificates \
	libx11-dev \
	libxrandr-dev \
	libxrender-dev \
	libxxf86vm-dev \
	libxcursor-dev \
	libgl1-mesa-dev \
	libglu1-mesa-dev \
	libopenal-dev \
	libvorbis-dev \
	libogg-dev \
	libfreetype6-dev \
	libpng-dev \
	libjpeg-dev \
	libgif-dev \
	libcurl4-openssl-dev \
	libssl-dev \
	libxml2-dev \
	zlib1g-dev \
	libreadline-dev \
	libkrb5-dev \
	libboost-dev \
	libboost-system-dev \
	libboost-filesystem-dev \
	libboost-thread-dev \
	libboost-date-time-dev \
	libboost-regex-dev \
	libboost-program-options-dev \
	&& rm -rf /var/lib/apt/lists/*

# Force GCC/G++ 4.8 to match the official build toolchain
ENV CC=gcc-4.8
ENV CXX=g++-4.8

# Install a modern CMake (3.22+) matching the official build environment.
# Scout's bundled CMake is too old (3.18) and produces different OpenGL detection results.
RUN wget -q https://github.com/Kitware/CMake/releases/download/v3.22.6/cmake-3.22.6-linux-x86_64.sh -O /tmp/cmake.sh \
	&& sh /tmp/cmake.sh --skip-license --prefix=/usr/local \
	&& rm /tmp/cmake.sh \
	&& cmake --version

WORKDIR /tmp/build

# Build Lua 5.3 from source (not packaged in Scout)
ENV LUA_VERSION=5.3.6
RUN wget -q https://www.lua.org/ftp/lua-${LUA_VERSION}.tar.gz \
	&& tar xzf lua-${LUA_VERSION}.tar.gz \
	&& cd lua-${LUA_VERSION} \
	&& make linux MYCFLAGS="-fPIC" \
	&& make install INSTALL_TOP=/usr/local \
	&& cd .. \
	&& rm -rf lua-${LUA_VERSION}*

# Build Luabind from source (nimetu fork, C++11 + Lua 5.3/5.4 support)
# Note: this fork has no option to disable tests, so we build only the library target
RUN git clone --depth 1 https://github.com/nimetu/luabind.git \
	&& cd luabind \
	&& mkdir build && cd build \
	&& cmake .. \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr/local \
		-DLUA_INCLUDE_DIR=/usr/local/include \
		-DLUA_LIBRARIES=/usr/local/lib/liblua.a \
	&& make -j$(nproc) luabind \
	&& find . -name "libluabind*.a" -exec cp -v {} /usr/local/lib/ \; \
	&& find . -name "libluabind*.so*" -exec cp -v {} /usr/local/lib/ \; \
	&& ln -sf $(ls /usr/local/lib/libluabind*.a 2>/dev/null | head -1) /usr/local/lib/libluabind.a \
	&& cp -r ../luabind /usr/local/include/ \
	&& find . -name "build_information.hpp" -exec cp -v {} /usr/local/include/luabind/ \; \
	&& cd /tmp/build \
	&& rm -rf luabind

# Build Google Breakpad from source (GCC 4.8 compatible commit)
ENV BREAKPAD_COMMIT=b988fa74ec18de6214b18f723e48331d9a7802ae
RUN git clone https://chromium.googlesource.com/breakpad/breakpad.git \
	&& cd breakpad \
	&& git checkout ${BREAKPAD_COMMIT} \
	&& git clone --depth 1 https://chromium.googlesource.com/linux-syscall-support src/third_party/lss \
	&& ./configure --prefix=/usr/local \
	&& make -j$(nproc) \
	&& make install \
	&& cd /tmp/build \
	&& rm -rf breakpad

# Build libsquish from source (without strip-components and disabled OpenMP to prevent linking errors)
ENV SQUISH_VERSION=1.15
RUN mkdir libsquish \
	&& wget -qO libsquish.tar.gz https://sourceforge.net/projects/libsquish/files/libsquish-${SQUISH_VERSION}.tgz/download \
	&& tar xzf libsquish.tar.gz -C libsquish \
	&& cd libsquish \
	&& cmake -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DBUILD_SQUISH_WITH_OPENMP=OFF . \
	&& make -j$(nproc) \
	&& make install \
	&& cd /tmp/build \
	&& rm -rf libsquish*

# Cleanup
RUN rm -rf /tmp/build

# Working directories
# /src    : ryzom-core source (cloned at runtime or mounted)
# /build  : CMake build directory
# /output : final artifacts copied here (mounted by compose)
RUN mkdir -p /src /build /output

# Runtime helper files shipped to /output with each build
RUN mkdir -p /usr/local/share/ryzom-docker

WORKDIR /work

# Entrypoint script handles clone, build, and packaging
COPY docker-scripts/build-ryzom.sh /usr/local/bin/build-ryzom.sh
RUN chmod +x /usr/local/bin/build-ryzom.sh

ENTRYPOINT ["/usr/local/bin/build-ryzom.sh"]
