use static_files::{resource_dir, NpmBuild};
use std::{
    fs::{self, copy},
    path::Path,
};

fn main() -> std::io::Result<()> {
    built::write_built_file().expect("Failed to acquire build-time information");

    NpmBuild::new("./static").change_detection().install()?.run("build-bulma")?;

    let dist_path = Path::new("./static/dist");
    let nm_path = Path::new("./static/node_modules");

    if fs::metadata(dist_path).is_err() {
        fs::create_dir(dist_path)?;
    }

    copy(
        nm_path.join("feather-icons/dist/feather-sprite.svg"),
        dist_path.join("feather-sprite.svg"),
    )?;
    copy(
        nm_path.join("htmx.org/dist/htmx.min.js"),
        dist_path.join("htmx.min.js"),
    )?;
    copy(
        nm_path.join("htmx.org/dist/ext/response-targets.js"),
        dist_path.join("response-targets.js"),
    )?;
    copy(
        nm_path.join("sweetalert2-neutral/dist/sweetalert2.min.js"),
        dist_path.join("sweetalert2.min.js"),
    )?;
    copy(
        Path::new("./static/utils.js"),
        dist_path.join("utils.js"),
    )?;
    copy(
        Path::new("./static/brass.jpeg"),
        dist_path.join("brass.jpeg"),
    )?;

    resource_dir("./static/dist").build()
}
