#!/bin/bash
# ─────────────────────────────────────────────────────────────────────────────
# YUMI GUI — Linux Universal Standalone Binary Script
# Builds directly on your host Ubuntu desktop via portable glibc-2.17 core binaries.
# Natively packages Python, Tkinter C extensions, distros.json, and YUMI4Linux.
# Requirements: None (Runs completely on the host system without Docker)
# Usage: bash build_linux_appimage.sh
# ─────────────────────────────────────────────────────────────────────────────
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE}")" && pwd)"
OUTPUT="$SCRIPT_DIR/dist/yumi_py"

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

# Clean up any old broken virtual environments or temporary staging structures
rm -rf "$SCRIPT_DIR/.venv" "$SCRIPT_DIR/build" "$SCRIPT_DIR/dist" "$SCRIPT_DIR/yumi_gui.spec"

echo "Step 1: Instantly fetching official 'uv' binary using host tools..."
if ! command -v uv &>/dev/null; then
    echo "  Installing uv via host Python pip subsystem..."
    python3 -m pip install --quiet --user uv || sudo apt install -y uv || snap install uv --classic
fi

export PATH="$HOME/.local/bin:$PATH"
echo "  uv version: $(uv --version)"

echo "Step 2: Provisioning isolated production glibc-2.17 Python environment..."
uv venv "$SCRIPT_DIR/.venv" --python 3.11 --python-preference only-managed
source "$SCRIPT_DIR/.venv/bin/activate"

echo "Step 3: Resolving structural shared library pathways for PyInstaller..."
# CRITICAL FIX: Scan uv's global machine toolchain directory directly to locate the genuine libpython3.11.so.1.0
REAL_SO_FILE=$(find "$HOME/.local/share/uv/python" -name "libpython3.11.so.1.0" -print -quit 2>/dev/null || find "$HOME" -name "libpython3.11.so.1.0" -print -quit 2>/dev/null)

if [ -z "$REAL_SO_FILE" ] || [ ! -f "$REAL_SO_FILE" ]; then
    echo "ERROR: Dynamic libpython3.11.so.1.0 not found anywhere on your system!"
    exit 1
fi

REAL_LIB_DIR=$(dirname "$REAL_SO_FILE")
echo "  Found shared dynamic library framework object at: $REAL_SO_FILE"

# Override PyInstaller's standard filesystem scanning layer to force-satisfy shared dependencies
export LD_LIBRARY_PATH="$REAL_LIB_DIR:$LD_LIBRARY_PATH"
export PYINSTALLER_PYTHON_LIBRARY="$REAL_SO_FILE"

echo "Step 4: Upgrading toolchain dependencies inside the clean workspace..."
uv pip install "setuptools<82.0.0" pyinstaller==5.13.2 certifi

# Pre-compute data flags as individual array elements for safe PyInstaller flag mapping
DATA_FLAGS=("--add-data=distros.json:.")
[ -f "$SCRIPT_DIR/yumi_icon.png" ] && DATA_FLAGS+=("--add-data=yumi_icon.png:.")
[ -d "$SCRIPT_DIR/YUMI4Linux" ]   && DATA_FLAGS+=("--add-data=YUMI4Linux:YUMI4Linux")
[ -f "$SCRIPT_DIR/YUMI-Copying.txt" ] && DATA_FLAGS+=("--add-data=YUMI-Copying.txt:.")

echo "Step 5: Executing PyInstaller standalone packaging pass..."
python3 -m PyInstaller \
    --onefile \
    --name=yumi_py \
    --hidden-import=tkinter \
    --hidden-import=tkinter.ttk \
    --hidden-import=tkinter.filedialog \
    --hidden-import=tkinter.messagebox \
    --hidden-import=tkinter.simpledialog \
    --collect-all=tkinter \
    "${DATA_FLAGS[@]}" \
    --noconfirm \
    --clean \
    yumi_gui.py

if [ -f "$OUTPUT" ]; then
    SIZE=$(du -h "$OUTPUT" | cut -f1)
    echo ""
    echo "Done: $OUTPUT ($SIZE)"
    echo "Run: sudo ./dist/yumi_py"
else
    echo ""
    echo "ERROR: Build failed — standalone binary was not created."
    exit 1
fi

