#!/bin/bash
# ─────────────────────────────────────────────────────────────────────────────
# YUMI GUI — Linux Nuitka Onefile Build Script (containerized glibc-2.17 build)
#
# Drop-in alternative to build-onefile.sh / builder_compat.sh. Instead of
# provisioning an isolated Python via uv on your host machine, this compiles
# INSIDE a CentOS 7 (glibc 2.17) container, so the resulting binary's actual
# glibc symbol requirements are old too — not just its bundled Python.
#
# Requirements on the HOST: only Docker (or Podman aliased as docker). No
# python3-tk, no build-essential, no patchelf needed on the host itself —
# all of that lives inside the container image.
#
# Usage: bash build-onefile-docker.sh
# ─────────────────────────────────────────────────────────────────────────────
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE}")" && pwd)"
IMAGE_TAG="yumi-build-compat:latest"
OUTPUT="$SCRIPT_DIR/yumi_py"

if [ ! -f "$SCRIPT_DIR/yumi_gui.py" ]; then
    echo "ERROR: yumi_gui.py not found in $SCRIPT_DIR"
    exit 1
fi

# --- pick a container runtime ------------------------------------------------
if command -v docker &>/dev/null; then
    RUNTIME="docker"
elif command -v podman &>/dev/null; then
    RUNTIME="podman"
else
    echo "ERROR: neither docker nor podman found."
    echo "       Install Docker (https://docs.docker.com/engine/install/) or"
    echo "       Podman (https://podman.io/docs/installation) and rerun."
    exit 1
fi
echo "Using container runtime: $RUNTIME"

# --- build (or reuse) the compat image --------------------------------------
if [ "$1" == "--rebuild-image" ] || ! "$RUNTIME" image inspect "$IMAGE_TAG" &>/dev/null; then
    echo "Step 1: Building glibc-2.17 build image ($IMAGE_TAG)..."
    "$RUNTIME" build -t "$IMAGE_TAG" -f "$SCRIPT_DIR/Dockerfile.compat" "$SCRIPT_DIR"
else
    echo "Step 1: Reusing existing build image ($IMAGE_TAG)."
    echo "        (pass --rebuild-image to force a rebuild, e.g. after editing"
    echo "         Dockerfile.compat or bumping dependency versions)"
fi

# --- assemble data-file flags (same logic as build-onefile.sh) -------------
DATA_FLAGS=()
[ -f "$SCRIPT_DIR/distros.json" ] && DATA_FLAGS+=("--include-data-files=/build/distros.json=distros.json")
[ -f "$SCRIPT_DIR/yumi_icon.png" ] && DATA_FLAGS+=("--include-data-files=/build/yumi_icon.png=yumi_icon.png")
[ -f "$SCRIPT_DIR/YUMI-Copying.txt" ] && DATA_FLAGS+=("--include-data-files=/build/YUMI-Copying.txt=YUMI-Copying.txt")
if [ -d "$SCRIPT_DIR/YUMI4Linux" ]; then
    DATA_FLAGS+=("--include-raw-dir=/build/YUMI4Linux=YUMI4Linux")
else
    echo "WARNING: YUMI4Linux folder not found - it will not be bundled."
fi

ICON_FLAG=()
[ -f "$SCRIPT_DIR/yumi_icon.png" ] && ICON_FLAG=("--linux-icon=/build/yumi_icon.png")

# --- run the actual Nuitka compile inside the container ---------------------
echo "Step 2: Compiling with Nuitka (onefile) inside $IMAGE_TAG..."
[ -f "$OUTPUT" ] && rm -f "$OUTPUT"

"$RUNTIME" run --rm \
    -v "$SCRIPT_DIR:/build" \
    -w /build \
    "$IMAGE_TAG" \
    python3.6 -m nuitka \
        --onefile \
        --onefile-tempdir-spec="{CACHE_DIR}/YUMI/runtime" \
        --enable-plugin=tk-inter \
        --include-package-data=tkinterdnd2 \
        --output-filename="yumi_py" \
        --output-dir="/build" \
        "${ICON_FLAG[@]}" \
        --assume-yes-for-downloads \
        --show-progress \
        --remove-output \
        "${DATA_FLAGS[@]}" \
        yumi_gui.py

if [ -f "$OUTPUT" ]; then
    chmod +x "$OUTPUT"
    SIZE=$(du -h "$OUTPUT" | cut -f1)
    echo ""
    echo "Done: $OUTPUT ($SIZE)"
    echo "Run: ./yumi_py"
    echo ""
    echo "This binary was compiled inside a CentOS 7 container (glibc 2.17"
    echo "floor), so its actual glibc requirement should match — not just"
    echo "its bundled Python. Verify with: ldd --version (on an old target"
    echo "machine) vs. the GLIBC_x.xx symbols the binary needs:"
    echo "  objdump -T ./yumi_py | grep -o 'GLIBC_[0-9.]*' | sort -Vu | tail -5"
else
    echo ""
    echo "ERROR: Build failed - standalone binary was not created."
    exit 1
fi
