// SPDX-FileCopyrightText: 2024 Christoph Sax <c_sax@mailbox.org>
//
// SPDX-License-Identifier: EUPL-1.2
//

mod config;
mod ls;
mod protocol;
mod stdiotrans;
mod t32;
mod utils;

pub use config::Config;

#[derive(Debug, PartialEq)]
pub enum ReturnCode {
    OkExit = 0,
    ErrExit = 1,
    UsageErr = 65,
    NoInputErr = 66,
    UnavailableErr = 69,

    SoftwareErr = 70,
    IoErr = 74,
    ProtocolErr = 76,
}

use config::OperationMode;

pub fn run(args: Vec<String>) -> ReturnCode {
    let cfg = match config::Config::build(&args) {
        Ok(conf) => conf,
        Err(rc) => return rc,
    };

    if cfg.mode == OperationMode::Server {
        ls::serve(cfg)
    } else {
        #[cfg(any(windows, unix))]
        stdiotrans::receive_excl();

        #[cfg(all(target_os = "wasi", target_env = "p1"))]
        unreachable!("WASI has no API support for spawning of new processes.")
    }
}
