pub mod analyzer;
/* 1:1 */
use std::process;
/* 2:1 */
use std::env;
/* 3:1 */
use std::fs;
/* 4:1 */
use mist_api::builder;
/* 5:1 */
use mist_api::transpiler;
/* 7:1 */
fn main() -> () {
    /* 8:5 */
    let args: Vec<String> = env::args().collect();
    /* 10:5 */
    if args.len()<2 {
        /* 11:9 */
        print_usage();
        /* 12:9 */
        process::exit(1);
    }
    /* 15:5 */
    match args[1].as_str() {        /* 16:9 */

        "run"
         | "build"
         | "check"
         | "r"
         | "b"
         | "c" => {
            /* 17:13 */
            let root = transpiler::transpile();
            /* 19:13 */
            println!("");
            /* 21:13 */
            if builder::build(args, root) {
                /* 22:17 */
                println!("\x1b[32m\nBuild successful\x1b[0m");
            } else {
                /* 24:17 */
                println!("\x1b[31m\nBuild failed\x1b[0m");
            }
        }        /* 27:9 */

        "transpile"
         | "t" => {
            /* 28:13 */
            transpiler::transpile();
        }        /* 30:9 */

        "init" => {
            /* 31:13 */
            init_project(args);
        }        /* 33:9 */

        "version"
         | "--version"
         | "-v" => {
            /* 34:13 */
            println!("mist {}", env!("CARGO_PKG_VERSION"));
        }        /* 36:9 */

        "help"
         | "--help"
         | "-h" => {
            /* 37:13 */
            print_usage();
        }        /* 39:9 */

        unknown => {
            /* 40:13 */
            eprintln!("error: unknown command '{}'\n", unknown);
            /* 41:13 */
            print_usage();
            /* 42:13 */
            process::exit(1);
        }
    }
}/* 47:1 */
fn print_usage() -> () {
    /* 48:5 */
    println!("Mist - the mist compiler\n");
    /* 49:5 */
    println!("󰖟 View our documentation! \x1b[35mhttps://mist.selimaj.dev\x1b[0m");
    /* 50:5 */
    println!(" Follow us on github!    \x1b[35mhttps://github.com/mist-go\x1b[0m\n");
    /* 51:5 */
    println!("usage:");
    /* 52:5 */
    println!(" \x1b[36m mist run\x1b[0m,\x1b[36m r\x1b[0m            run the project in the current directory\n");
    /* 53:5 */
    println!(" \x1b[36m mist build\x1b[0m,\x1b[36m b\x1b[0m          build the project in the current directory\n");
    /* 56:5 */
    println!(" \x1b[36m mist transpile\x1b[0m,\x1b[36m t\x1b[0m      transpile the project in the current directory\n");
    /* 59:5 */
    println!(" \x1b[36m mist check\x1b[0m,\x1b[36m c\x1b[0m          check the project in the current directory\n");
    /* 62:5 */
    println!(" \x1b[36m mist init\x1b[0m              initialize a project in the current folder\n");
    /* 65:5 */
    println!(" \x1b[36m mist version \x1b[0m          print the compiler version\n");
    /* 66:5 */
    println!(" \x1b[36m mist help\x1b[0m              print this message\n");
}/* 69:1 */
fn init_project(mut args: Vec<String>) -> () {
    /* 70:5 */
    args.remove(0);
    /* 72:5 */
    process::Command::new("cargo").args(args).stdout(process::Stdio::inherit()).stderr(process::Stdio::inherit()).stdin(process::Stdio::inherit()).spawn().expect("Failed to run cargo").wait().expect("Unable to wait");
    /* 80:5 */
    let is_lib: bool = fs::metadata("src/lib.rs").is_ok();
    /* 82:5 */
    if is_lib {
        /* 83:9 */
        fs::remove_file("src/lib.rs").expect("Failed to remove src/lib.rs");
        /* 84:9 */
        fs::write("src/lib.mist", "void test() {\n}").expect("Failed to create src/lib.mist");
    } else {
        /* 86:9 */
        fs::remove_file("src/main.rs").expect("Failed to remove src/main.rs");
        /* 87:9 */
        fs::write("src/main.mist", "void main() {\n    println!(\"Hello, World!\");\n}\n").expect("Failed to create src/main.mist");
    }
    /* 90:5 */
    inject_mist_target(is_lib);
    /* 92:5 */
    println!("\n\x1b[34minfo\x1b[0m: Please open Cargo.toml and adjust `\x1b[35mname\x1b[0m = ` as needed, Thank you for using Mist!\n");
}/* 95:1 */
fn inject_mist_target(is_lib: bool) -> () {
    /* 96:5 */
    let toml_content: String = fs::read_to_string("Cargo.toml").expect("Failed to read Cargo.toml");
    /* 98:5 */
    let inject: &str = if is_lib {
        /* 99:9 */
        "\n[lib]\nname = \"my_lib\"\npath = \".mist/src/lib.rs\"\n"
    } else {
        /* 101:9 */
        "\n[[bin]]\nname = \"my_bin\"\npath = \".mist/src/main.rs\"\n"
    };
    /* 104:5 */
    fs::write("Cargo.toml", toml_content+inject).expect("Failed to inject to Cargo.toml");
}