#![allow(clippy::disallowed_methods, reason = "build scripts are exempt")]

fn main() {
    println!("cargo::rustc-check-cfg=cfg(gles)");

    let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();

    if target_os == "windows" {
        #[cfg(feature = "windows-manifest")]
        embed_resource();
    }
}

#[cfg(feature = "windows-manifest")]
fn embed_resource() {
    let crate_dir = std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
    let manifest = crate_dir.join("resources/windows/gpui.manifest.xml");
    println!("cargo:rerun-if-changed={}", manifest.display());

    let out_dir = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap());
    
    // Copy resources to OUT_DIR so relative paths work during cross-compilation
    let manifest_out = out_dir.join("gpui.manifest.xml");
    std::fs::copy(&manifest, &manifest_out).expect("Failed to copy manifest file");
    
    // Create RC file with relative path to copied manifest
    let fixed_rc = out_dir.join("gpui-fixed.rc");
    let rc_content = "#define RT_MANIFEST 24\n1 RT_MANIFEST \"gpui.manifest.xml\"";
    std::fs::write(&fixed_rc, rc_content).unwrap();

    embed_resource::compile(&fixed_rc, embed_resource::NONE)
        .manifest_required()
        .unwrap();
}
