fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Prefer a toolchain-provided protoc (e.g. via `nix develop` which sets PROTOC),
    // but fall back to a vendored protoc when PROTOC isn't set.
    if std::env::var_os("PROTOC").is_none() {
        let protoc = protoc_bin_vendored::protoc_bin_path()?;
        std::env::set_var("PROTOC", protoc);
    }

    tonic_build::configure()
        .build_server(true)
        .build_client(true)
        .bytes(["."])
        .compile_protos(&["proto/node.proto"], &["proto"])?;

    println!("cargo:rerun-if-changed=proto/node.proto");
    println!("cargo:rerun-if-changed=proto");

    Ok(())
}
