//! Build script for coolprop-sys.
//!
//! This compiles the CoolProp C++ library statically.

use std::env;
use std::path::PathBuf;

fn coolprop_src_path() -> Option<PathBuf> {
    // Try to find CoolProp source in common locations
    let possible_paths = vec![
        // Vendor directory (recommended)
        PathBuf::from("../../vendor/coolprop").canonicalize().unwrap_or(PathBuf::from("../../../vendor/coolprop")),
        // External directory
        PathBuf::from("external/coolprop"),
        // System paths
        PathBuf::from("/usr/local/src/CoolProp"),
        PathBuf::from("/opt/CoolProp"),
    ];

    possible_paths
        .into_iter()
        .find(|path| path.join("CMakeLists.txt").exists())
}

fn main() {
    let static_linking = env::var("CARGO_FEATURE_STATIC").is_ok() || true; // Force static linking for python wheels

    // Check if CoolProp source is available
    if let Some(coolprop_path) = coolprop_src_path() {
        println!("cargo:rerun-if-changed={}", coolprop_path.display());

        // Build CoolProp using CMake
        let dst = cmake::Config::new(&coolprop_path)
            .define("COOLPROP_SHARED_LIBRARY", "OFF")
            .define("COOLPROP_STATIC_LIBRARY", "ON")
            .define("COOLPROP_CATCH_TEST", "OFF")
            .define("COOLPROP_C_LIBRARY", "ON")
            .define("COOLPROP_MY_IFCO3_WRAPPER", "OFF")
            .build();

        println!("cargo:rustc-link-search=native={}/build", dst.display());
        println!("cargo:rustc-link-search=native={}/lib", dst.display());
        println!("cargo:rustc-link-search=native={}/build", coolprop_path.display()); // Fallback
        
        // Link against CoolProp statically
        println!("cargo:rustc-link-lib=static=CoolProp");
        
        // On macOS, force load the static library so its symbols are exported in the final cdylib
        if cfg!(target_os = "macos") {
            println!("cargo:rustc-link-arg=-Wl,-force_load,{}/build/libCoolProp.a", dst.display());
        }
    } else {
        println!(
            "cargo:warning=CoolProp source not found in vendor/. 
                  For full static build, run: 
                  git clone https://github.com/CoolProp/CoolProp.git vendor/coolprop"
        );
        // Fallback for system library
        if static_linking {
            println!("cargo:rustc-link-lib=static=CoolProp");
        } else {
            println!("cargo:rustc-link-lib=dylib=CoolProp");
        }
    }

    // Link required system libraries for C++ standard library
    #[cfg(target_os = "macos")]
    println!("cargo:rustc-link-lib=dylib=c++");
    #[cfg(not(target_os = "macos"))]
    println!("cargo:rustc-link-lib=dylib=stdc++");

    println!("cargo:rustc-link-lib=dylib=m");

    // Tell Cargo to rerun if build.rs changes
    
    // Force export symbols on macOS for static building into a dynamic python extension
    println!("cargo:rustc-link-arg=-Wl,-all_load");

    println!("cargo:rerun-if-changed=build.rs");
}
