#!/usr/bin/env modbuild

# Open-source PyMOL, compiled from the GitHub source tarball into a uv venv
# (careless-style uv route; conda unreachable from Ra). Default pbuild::prep
# downloads + sha-verifies + unpacks both urls from files/config.yaml.
# GLEW/libpng/freetype/GL headers must exist as system RPMs on the build host;
# msgpack/MMTF, libxml2 (COLLADA) and VMD plugins (libnetcdf) are disabled
# because those libs are not on Ra.

pbuild::configure() {
    # modbuild's shell strips ~/.local/bin from PATH; override with UV=/path/to/uv if needed
    UV="${UV:-$HOME/.local/bin/uv}"
    # ~/.cache/uv hits the home disk quota; shared cache reuses wheels across MX builds
    export UV_CACHE_DIR="/opt/psi/MX/.uv-cache"
    # keep the standalone CPython inside PREFIX so venv symlinks never point at a home cache
    export UV_PYTHON_INSTALL_DIR="$PREFIX/python"
    "$UV" venv --python 3.12 "$PREFIX/venv" || std::die 42 "uv venv failed - is uv at \$UV?"

    # modbuild -f re-untars over the old src dir without deleting extras; a stale
    # setuptools build/ tree from a failed run then gets packed into the wheel as-is
    # (pure-python, no _cmd.so). Wipe it so every build compiles from scratch.
    rm -rf "$SRC_DIR/build"

    # stage glm where setup.py's PREFIX_PATH lookup expects it: <prefix>/include/glm.
    # strip_dirs applies to the glm tarball too, so headers land at $SRC_DIR/glm directly
    mkdir -p "$SRC_DIR/deps/include"
    cp -r "$SRC_DIR/glm" "$SRC_DIR/deps/include/" || \
        std::die 42 "glm staging failed - glm tarball missing from SRC_DIR?"

    # both tarballs unpack stripped into SRC_DIR and their top-level files
    # collide; the glm tarball once shadowed pymol's CMakeLists.txt and cmake
    # silently built glm's tests instead of _cmd. Guard: pymol's CMakeLists
    # passes TARGET_NAME from setup.py, glm's does not.
    grep -q TARGET_NAME "$SRC_DIR/CMakeLists.txt" || \
        std::die 42 "SRC_DIR/CMakeLists.txt is not pymol's - glm overwrote it; check url order in files/config.yaml (glm must unpack first)"
}

pbuild::compile() {
    :
}

pbuild::install() {
    UV="${UV:-$HOME/.local/bin/uv}"
    export UV_CACHE_DIR="/opt/psi/MX/.uv-cache"
    # force the gcc module's compilers; uv's standalone CPython records clang in sysconfig
    export CC=gcc CXX=g++
    export PREFIX_PATH="$SRC_DIR/deps"
    # -C k=v reaches setup.py as `build_ext --k=v` (_custom_build/backend.py).
    # str2bool accepts ONLY true/false (yes/no -> "Boolean value expected").
    # no-vmd-plugins=false is deliberate: upstream dest is vmd_plugins, false disables them.
    # upstream's documented install is `pip install --no-build-isolation .`; with
    # default isolation the wheel builds in an env upstream never tests and comes
    # out pure-python (no _cmd.so) WITHOUT any error. Build deps must therefore
    # be present in the venv itself.
    "$UV" pip install --python "$PREFIX/venv/bin/python" setuptools numpy || \
        std::die 42 "build deps (setuptools, numpy) install failed"
    # --no-cache: never reuse a cached pymol wheel — a poisoned pure-python wheel
    # (built from a half-failed compile) otherwise gets reinstalled forever
    "$UV" pip install --no-cache --no-build-isolation --python "$PREFIX/venv/bin/python" \
        -C use-msgpackc=no -C no-libxml=true -C no-vmd-plugins=false \
        "$SRC_DIR" || \
        std::die 42 "pymol build failed - check system devel RPMs (glew, libpng, freetype, mesa-libGL)"
    # Qt GUI; without it pymol falls back to CLI-only
    "$UV" pip install --python "$PREFIX/venv/bin/python" PyQt5 || \
        std::die 42 "PyQt5 install failed"
    [[ -x "$PREFIX/venv/bin/pymol" ]] || std::die 42 "pymol launcher missing from venv"
    # launcher can exist while the compiled ext is missing (pure-python wheel from a
    # poisoned uv cache); import _cmd directly so a broken build dies here, not at runtime
    "$PREFIX/venv/bin/python" -c "import pymol._cmd" || \
        std::die 42 "pymol._cmd import failed - ext .so missing; try 'uv cache clean pymol' (dist name is pymol, not pymol-open-source) and rebuild"
}
