fn main() {
    println!("cargo:rerun-if-changed=src/");
    println!("cargo:rerun-if-changed=ui/src/");
    println!("cargo:rerun-if-changed=ui/index.html");
    println!("cargo:rerun-if-changed=ui/package.json");
    println!("cargo:rerun-if-changed=ui/vite.config.js");
    println!("cargo:rerun-if-changed=ui/svelte.config.js");

    // pnpm may live under Homebrew on macOS; prepend common prefix to PATH.
    let path = std::env::var("PATH").unwrap_or_default();
    let path = format!("/opt/homebrew/bin:/usr/local/bin:{path}");

    // During `cargo publish` verification the package is extracted to
    // target/package/<name>/ and build.rs is re-run there. pnpm would write
    // node_modules into that directory (outside OUT_DIR), failing the check.
    // The dist is already in the tarball, so we can safely skip the build.
    let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_default();
    if manifest_dir.contains("/target/package/") {
        return;
    }

    // If pnpm is unavailable (e.g. crates.io build environment), accept a
    // pre-built dist rather than failing.
    let pnpm_found = std::process::Command::new("pnpm")
        .arg("--version")
        .env("PATH", &path)
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false);

    if !pnpm_found {
        if !std::path::Path::new("ui/dist/index.html").exists() {
            panic!(
                "pnpm not found and ui/dist/index.html is missing; \
                 run: pnpm --dir ui install && pnpm --dir ui build"
            );
        }
        return;
    }

    let installed = std::process::Command::new("pnpm")
        .args(["--dir", "ui", "install", "--frozen-lockfile"])
        .env("PATH", &path)
        .status()
        .map(|s| s.success())
        .unwrap_or(false);

    let built = installed
        && std::process::Command::new("pnpm")
            .args(["--dir", "ui", "build"])
            .env("PATH", &path)
            .status()
            .map(|s| s.success())
            .unwrap_or(false);

    if !built {
        panic!(
            "UI build failed; run manually: \
             pnpm --dir ui install && pnpm --dir ui build"
        );
    }
}
