fn main() {
    // Tell rustc that `#[cfg(bazel)]` is a valid (but never set) cfg key when
    // building via Cargo, so it doesn't emit unexpected_cfgs warnings.
    println!("cargo:rustc-check-cfg=cfg(bazel)");

    // Default builds use pre-generated sources in src/conjure/;
    // codegen only runs when generate-bindings is explicitly enabled.
    #[cfg(feature = "generate-bindings")]
    generate_all();

    // Without this, Cargo re-runs the build script on every invocation when no
    // rerun-if-changed lines are emitted (default builds don't emit any).
    #[cfg(not(feature = "generate-bindings"))]
    println!("cargo:rerun-if-changed=build.rs");
}

#[cfg(feature = "generate-bindings")]
fn generate_all() {
    use std::env;
    use std::path::{Path, PathBuf};

    let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());

    let conjure_input = Path::new("definitions/conjure/scout-service-api.conjure.json");
    println!("cargo:rerun-if-changed={}", conjure_input.display());

    if !conjure_input.exists() {
        panic!(
            "generate-bindings: {} not found; this is a bug with nominal-api-conjure",
            conjure_input.display()
        );
    }

    conjure_codegen::Config::new()
        .strip_prefix("io.nominal".to_string())
        .generate_files(conjure_input, out_dir.join("conjure"))
        .unwrap();
}
