def install_ollama_to()

in src/databao_context_engine/llm/install.py [0:0]


def install_ollama_to(target: Path) -> None:
    """
    Ensure an Ollama binary exists.

    If it doesn't exist, this will:
    - detect OS
    - download the archive from GitHub
    - verify its SHA-256 checksum
    - extract into the installation directory
    - make the binary executable
    """
    target = target.expanduser()
    if target.parent.name == "bin":
        install_root = target.parent.parent
    else:
        install_root = target.parent

    install_root.mkdir(parents=True, exist_ok=True)

    platform_key = _detect_platform()
    try:
        artifact = ARTIFACTS[platform_key]
    except KeyError as e:
        raise RuntimeError(f"Unsupported platform: {platform_key}") from e

    url = f"https://github.com/ollama/ollama/releases/download/{DEFAULT_VERSION}/{artifact.name}"
    archive_path = _download_to_temp(url)

    try:
        _verify_sha256(archive_path, artifact.sha256)
        logger.info("Verified SHA256 for %s", archive_path.name)

        _extract_archive(archive_path, install_root)

        candidates: list[Path] = []
        if sys.platform.startswith("win"):
            candidates.extend(
                [
                    install_root / "ollama.exe",
                    install_root / "bin" / "ollama.exe",
                ]
            )
        else:
            candidates.extend(
                [
                    install_root / "ollama",
                    install_root / "bin" / "ollama",
                ]
            )

        binary: Path | None = None
        for c in candidates:
            if c.exists():
                binary = c
                break

        if binary is None:
            raise RuntimeError(f"Installed Ollama archive but could not find binary under {install_root}")

        if binary.resolve() != target.resolve():
            target.parent.mkdir(parents=True, exist_ok=True)
            shutil.copy2(binary, target)

        _ensure_executable(target)
        logger.info("Ollama installed at %s", target)

    finally:
        try:
            archive_path.unlink(missing_ok=True)
        except Exception:
            pass