//! main.rs

use std::env;
use std::ffi::OsStr;
use std::path::Path;
use std::process::ExitCode;

/// Package name
const PROGRAM: &str = env!("CARGO_PKG_NAME");
/// Package version
const VERSION: &str = env!("CARGO_PKG_VERSION");
/// Package authors
const AUTHORS: &str = env!("CARGO_PKG_AUTHORS");

fn main() -> ExitCode {
    eprintln!("{PROGRAM} {VERSION} - Tool to extract strings and related pseudocode");
    eprintln!("Copyright (c) 2024-2026 {AUTHORS}");
    eprintln!();

    // Force IDA Pro to stay quiet
    idalib::force_batch_mode();

    // Parse command line arguments
    let mut args = env::args_os();
    let argv0 = args.next().unwrap_or_else(|| PROGRAM.into());
    let is_help = |a: &OsStr| a == OsStr::new("-h") || a == OsStr::new("--help");

    let prog = Path::new(&argv0)
        .file_name()
        .and_then(|s| s.to_str())
        .unwrap_or(PROGRAM);

    let filename = match (args.next(), args.next()) {
        (Some(arg), None) if !is_help(&arg) => arg,
        _ => return usage(prog),
    };

    // Let's do it
    match augur::run(Path::new(&filename)) {
        Ok(_) => ExitCode::SUCCESS,
        Err(err) => {
            eprintln!("[!] Error: {err:#}");
            ExitCode::FAILURE
        }
    }
}

/// Print usage information and exit
fn usage(prog: &str) -> ExitCode {
    eprintln!("Usage:");
    eprintln!("{prog} <binary_file>");

    ExitCode::FAILURE
}
