extern crate cc;

use std::fs::File;
use std::io::{Result, Write};
use std::path::Path;

fn main() {
    if let Ok(file_path) = gen_payload_asm() {
        cc::Build::new().file(&file_path).compile("payload");
    }
}

fn gen_payload_asm() -> Result<std::path::PathBuf> {
    let out_dir = std::env::var("OUT_DIR").unwrap();
    let payload = std::env::var("PAYLOAD").unwrap();

    if !Path::new(&payload).is_file() {
        panic!("Kernel payload `{}` not found", payload)
    }

    let file_path = Path::new(&out_dir).join("payload.S");
    let mut f = File::create(&file_path).unwrap();

    println!("{:x?} {:x?}", payload, file_path);

    write!(f, "# generated by build.rs - do not edit")?;
    write!(f, r#"
    .section .payload,"a"
    .align 12
    .global _kernel_payload_start, _kernel_payload_end
_kernel_payload_start:
    .incbin "{}"
_kernel_payload_end:
"#, payload)?;

    println!("cargo:rerun-if-changed={}", payload);
    println!("cargo:rerun-if-env-changed=PAYLOAD");

    Ok(file_path)
}
