mod builtin;
mod file;
mod interpreter;
mod lexer;
mod repl;

use std::env;
use std::process;

use file::run_file;
use repl::repl;

// These mirror the C macros.
const SLS_NAME: &str = "SLS_RUST";
const SLS_VER: &str = "0.0.1-alpha";

pub fn print_version() {
    let git_hash = option_env!("GIT_COMMIT_HASH").unwrap_or("unknown");
    let compiler = option_env!("COMPILER_NAME").unwrap_or("rustc");
    let compiler_ver = option_env!("COMPILER_VER").unwrap_or("unknown");
    let build_date = std::env::var("BUILD_DATE").unwrap_or_else(|_| "unknown".into());
    let build_time = std::env::var("BUILD_TIME").unwrap_or_else(|_| "unknown".into());

    println!("YREA SLS ({SLS_NAME}) {SLS_VER} ({git_hash})");
    println!("Compiled with {compiler} {compiler_ver} at {build_date} {build_time}");
}

fn main() {
    let mut args = env::args().skip(1);

    let mut version_flag = false;
    let mut filename: Option<String> = None;

    match args.len() {
        0 => {}
        1 => {
            let arg = args.next().unwrap();
            if arg == "--version" || arg == "-v" {
                version_flag = true;
            } else {
                filename = Some(arg);
            }
        }
        _ => {
            eprintln!("Too many arguments!");
            process::exit(1);
        }
    }

    if version_flag {
        print_version();
        process::exit(0);
    }

    if let Some(file) = filename {
        let status = run_file(&file);
        process::exit(status);
    }

    // Default to REPL
    let status = repl();
    process::exit(status);
}
