FROM --platform=linux/arm64 ubuntu:22.04

# GTK4 + SDL2 for Raspberry Pi (ARM64 / aarch64)
# Targets Pi 3 / 4 / 5 running 64-bit Raspberry Pi OS or Ubuntu 22.04.
#
# GTK4 requires a desktop environment / Wayland compositor at runtime.
# SDL2 works on bare framebuffer or KMS/DRM without a full desktop.
#
# Usage:
#   docker build --platform linux/arm64 \
#       -f Dockerfile.pi --tag el-native-pi-arm64 ./build-docker/
#   docker create --name pi-extract el-native-pi-arm64
#   docker cp pi-extract:/build/native-hello-linux-pi ./build/native-hello-pi-arm64
#   docker rm pi-extract
#
# The GTK4 build is the default. SDL2 is available as an alternative
# for headless / bare-metal Pi deployments.

RUN apt-get update && apt-get install -y \
    build-essential \
    libgtk-4-dev \
    libsdl2-dev \
    libsdl2-ttf-dev \
    libsdl2-image-dev \
    pkg-config \
    libcurl4-openssl-dev \
    binutils \
    file \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /build

# All sources arrive in /build
COPY runtime/ runtime/
COPY el_native_vessel.c el_native_vessel.h native_hello.c ./

# ── GTK4 build ────────────────────────────────────────────────────────────────

# Compile el_gtk4.c
RUN gcc -DEL_TARGET_LINUX $(pkg-config --cflags gtk4) \
    -std=c11 -I/build/runtime \
    -c /build/runtime/el_gtk4.c -o /build/el_gtk4.o && \
    echo "el_gtk4.c compiled OK"

# Compile el_seed.c
RUN gcc -DEL_TARGET_LINUX $(pkg-config --cflags gtk4) \
    -std=c11 -I/build/runtime \
    -c /build/runtime/el_seed.c -o /build/el_seed.o && \
    echo "el_seed.c compiled OK"

# Compile el_runtime.c
RUN gcc -std=c11 -I/build/runtime \
    -c /build/runtime/el_runtime.c -o /build/el_runtime.o && \
    echo "el_runtime.c compiled OK"

# Patch el_runtime.o: localise __-prefixed duplicate symbols (el_seed.o is canonical)
RUN nm /build/el_seed.o    | awk '/[0-9a-f]+ T /{print $3}' | sort > /build/.seed_T.txt && \
    nm /build/el_runtime.o | awk '/[0-9a-f]+ T /{print $3}' | sort > /build/.rt_T.txt && \
    comm -12 /build/.seed_T.txt /build/.rt_T.txt > /build/.dups.txt && \
    if [ -s /build/.dups.txt ]; then \
        ARGS=$(awk '{printf "--localize-symbol=%s ", $1}' /build/.dups.txt); \
        eval "objcopy $ARGS /build/el_runtime.o"; \
        echo "Patched $(wc -l < /build/.dups.txt) duplicate symbols in el_runtime.o"; \
    fi

# Compile vessel
RUN gcc -DEL_TARGET_LINUX -std=c11 -I/build/runtime \
    -include /build/runtime/el_native_target.h \
    -c /build/el_native_vessel.c -o /build/el_native_vessel.o && \
    echo "vessel compiled OK"

# Compile app
RUN gcc -DEL_TARGET_LINUX -std=c11 -I/build/runtime \
    -include /build/runtime/el_native_target.h \
    -include /build/el_native_vessel.h \
    -c /build/native_hello.c -o /build/native_hello.o && \
    echo "app compiled OK"

# Patch el_native_vessel.o: localise shared globals (main + TOKEN_* etc.)
RUN nm /build/native_hello.o     | awk '/[0-9a-f]+ [TBb] /{print $3}' | sort > /build/.app_syms.txt && \
    nm /build/el_native_vessel.o | awk '/[0-9a-f]+ [TBb] /{print $3}' | sort > /build/.vessel_syms.txt && \
    comm -12 /build/.app_syms.txt /build/.vessel_syms.txt > /build/.vessel_dups.txt && \
    echo "main" >> /build/.vessel_dups.txt && \
    sort -u /build/.vessel_dups.txt -o /build/.vessel_dups.txt && \
    ARGS=$(awk '{printf "--localize-symbol=%s ", $1}' /build/.vessel_dups.txt) && \
    eval "objcopy $ARGS /build/el_native_vessel.o" && \
    echo "Patched $(wc -l < /build/.vessel_dups.txt) symbols in el_native_vessel.o"

# Link (GTK4)
RUN gcc \
    /build/native_hello.o \
    /build/el_native_vessel.o \
    /build/el_gtk4.o \
    /build/el_runtime.o \
    /build/el_seed.o \
    $(pkg-config --libs gtk4) -ldl -lpthread -lcurl -lm \
    -o /build/native-hello-linux-pi && \
    echo "==> Linked: native-hello-linux-pi (GTK4)" && \
    file /build/native-hello-linux-pi
