use eyre::Result;
use std::env;
use std::process::exit;

use init::cmd;
use init::dklog;

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
    dklog::LOG.spawn(dklog::LOG.copy_to(tokio::io::stderr()));

    dklog::init();

    let call_name = env::args().next();
    let call_name = call_name.as_deref().unwrap_or("init");
    let call_name = call_name.rsplit_once('/').map_or(call_name, |(_, n)| n);

    let result = match call_name {
        "init" => {
            cmd::init::run().await;
            Ok(())
        }
        "init-version" => {
            cmd::version::run();
            Ok(())
        }
        "init-connect" => {
            cmd::init_input::run().await;
            Ok(())
        }
        "bootstrap" => cmd::bootstrap::run().await,

        _ => {
            eprintln!("invalid call name: {call_name:?}");
            exit(1);
        }
    };

    dklog::LOG.close().await;
    result
}
