//! Pattern 3: Load a plugin from a directory
//!
//! The directory must contain `init.lua` (or `main.lua`).
//! All files in the directory are available via `require`.
//! This enables multi-module Lua plugins.
//!
//! Usage:
//!   cargo run --example plugin_dir -- examples/plugins/counter count 5
//!   cargo run --example plugin_dir -- examples/plugins/counter sum 3 7

use senl::SenlApp;
use std::{env, process};

fn main() {
    let args: Vec<String> = env::args().collect();
    let plugin_dir = match args.get(1) {
        Some(d) if !d.starts_with('-') => d,
        _ => {
            eprintln!("usage: plugin_dir <dir> [command] [args...]");
            process::exit(1);
        }
    };

    let remaining: Vec<&str> = args.iter().skip(2).map(|s| s.as_str()).collect();

    let code = SenlApp::from_dir(plugin_dir)
        .unwrap_or_else(|e| {
            eprintln!("{e}");
            process::exit(1);
        })
        .run_with_args(remaining)
        .unwrap_or_else(|e| {
            eprintln!("{e}");
            process::exit(1);
        });
    process::exit(code);
}
