use std::path::{Path, PathBuf};

use common::{build::Builder, logging::info};
use compiler::Compiler;

fn main() {
    // read from input file: syntax "c_compiler <src.c> [output.dsa]"
    let args: Vec<String> = std::env::args().collect();
    if args.len() < 2 {
        eprintln!("Usage: c_compiler [--lib | --bin] <src.dsc> [output.dsa]");
        return;
    }

    let input_file = &args[1];
    let output_file = if args.len() > 2 {
        &args[2]
    } else {
        "output.dsa"
    };

    {
        let is_lib = args.contains(&"--lib".to_string());

        let mut builder = Compiler::new(PathBuf::from(input_file));
        builder.start(is_lib);
        let result = builder.output().unwrap();

        std::fs::write(output_file, &result).expect("Failed to write output");

        info(&format!(
            "Compilation Successful ✅ \n\tSource: {}\n\tOutput: {}\n",
            input_file, output_file,
        ));
    }
}
