use std::process::Command;
use std::io::{self, Write};
use std::path::Path;

fn main() {
    println!("cargo::rerun-if-changed=shaders/cube.frag");
    println!("cargo::rerun-if-changed=shaders/cube.geom");
    println!("cargo::rerun-if-changed=shaders/cube.vert");

    println!("cargo::rerun-if-changed=shaders/cuboid.frag");
    println!("cargo::rerun-if-changed=shaders/cuboid.geom");
    println!("cargo::rerun-if-changed=shaders/cuboid.vert");

    println!("cargo::rerun-if-changed=shaders/rt_quad.vert");
    println!("cargo::rerun-if-changed=shaders/rt_quad.frag");

    println!("cargo::rerun-if-changed=shaders/rt_compute_rasterize.comp");
    println!("cargo::rerun-if-changed=shaders/rt_compute_grow_one.comp");
    println!("cargo::rerun-if-changed=shaders/rt_compute_grow_two.comp");

    std::fs::remove_file("shaders/compiled/geo_cube.spv").unwrap_or(());
    std::fs::remove_file("shaders/compiled/frag_cube.spv").unwrap_or(());
    std::fs::remove_file("shaders/compiled/vert_cube.spv").unwrap_or(());
    std::fs::remove_file("shaders/compiled/geo_cuboid.spv").unwrap_or(());
    std::fs::remove_file("shaders/compiled/frag_cuboid.spv").unwrap_or(());
    std::fs::remove_file("shaders/compiled/vert_cuboid.spv").unwrap_or(());
    std::fs::remove_file("shaders/compiled/vert_rt_quad.spv").unwrap_or(());
    std::fs::remove_file("shaders/compiled/frag_rt_quad.spv").unwrap_or(());
    std::fs::remove_file("shaders/compiled/rt_compute_rasterize.spv").unwrap_or(());
    std::fs::remove_file("shaders/compiled/rt_compute_grow_one.spv").unwrap_or(());
    std::fs::remove_file("shaders/compiled/rt_compute_grow_two.spv").unwrap_or(());
    std::fs::remove_file("shaders/compiled/rt_compute_grow_three.spv").unwrap_or(());

    if std::env::consts::OS == "windows" {
        let mut command = Command::new("./shaders/compile.bat");
        let output = command.output().expect("Failed to execute command");
        println!("status: {}", output.status);
        io::stdout().write_all(&output.stdout).unwrap();
        io::stderr().write_all(&output.stderr).unwrap();
        
        assert!(output.status.success());    
    } else if std::env::consts::OS == "linux" {
        let mut command = Command::new("./shaders/compile.sh");
        let output = command.output().expect("Failed to execute command");
        println!("status: {}", output.status);
        io::stdout().write_all(&output.stdout).unwrap();
        io::stderr().write_all(&output.stderr).unwrap();
        
        assert!(output.status.success());
    }

    assert!(Path::new("shaders/compiled/geo_cube.spv").exists());
    assert!(Path::new("shaders/compiled/frag_cube.spv").exists());
    assert!(Path::new("shaders/compiled/vert_cube.spv").exists());
    assert!(Path::new("shaders/compiled/geo_cuboid.spv").exists());
    assert!(Path::new("shaders/compiled/frag_cuboid.spv").exists());
    assert!(Path::new("shaders/compiled/vert_cuboid.spv").exists());
    assert!(Path::new("shaders/compiled/vert_rt_quad.spv").exists());
    assert!(Path::new("shaders/compiled/frag_rt_quad.spv").exists());
}