use std::path::Path;
use std::{env, process};

fn main() {
    #[cfg(feature = "examples")]
    {
        let out = env::var_os("OUT_DIR").unwrap();
        let out = Path::new(&out);
        let examples = Path::new("examples");

        println!("Compiling examples ...");
        for example in ["Basic", "BranchExpr"] {
            let src_file = Path::new(example).with_extension("hx");
            let source = examples.join(example).with_extension("hx");
            let dest = out.join(example).with_extension("hl");
            let src_path = source.display();
            println!("cargo:rerun-if-changed={src_path}");

            assert!(process::Command::new("haxe")
                .current_dir(examples)
                .arg("-hl")
                .arg(&dest)
                .arg("-main")
                .arg(&src_file)
                .status()
                .unwrap()
                .success());
        }
    }

    add_windows_icon();
}

#[cfg(target_os = "windows")]
fn add_windows_icon() {
    let icon_path = "../../assets/hlbc.ico";
    println!("cargo:rerun-if-changed={icon_path}");

    let mut res = winresource::WindowsResource::new();
    res.set_icon(icon_path);
    res.compile().unwrap();
}

#[cfg(not(target_os = "windows"))]
fn add_windows_icon() {}
