use std::ffi::OsString;
use std::path::PathBuf;
use std::process::{Command, ExitCode};
const TARGET: &str = "surject";
const NOTICE: &str = "loadout is now surject";
fn main() -> ExitCode {
run()
}
pub fn run() -> ExitCode {
let args: Vec<OsString> = std::env::args_os().skip(1).collect();
if !is_silent(&args) {
eprintln!("{NOTICE}");
}
let program = sibling().unwrap_or_else(|| PathBuf::from(TARGET));
match Command::new(&program).args(&args).status() {
Ok(status) => match status.code() {
Some(code) => ExitCode::from(u8::try_from(code & 0xff).unwrap_or(1)),
None => ExitCode::from(1),
},
Err(err) => {
eprintln!("loadout: cannot run {}: {err}", program.display());
ExitCode::from(127)
}
}
}
fn sibling() -> Option<PathBuf> {
let exe = std::env::current_exe().ok()?;
let path = exe
.parent()?
.join(format!("{TARGET}{}", std::env::consts::EXE_SUFFIX));
path.is_file().then_some(path)
}
fn is_silent(args: &[OsString]) -> bool {
let mut words = Vec::new();
let mut iter = args.iter().map(|arg| arg.to_string_lossy());
while let Some(arg) = iter.next() {
if arg == "--home" {
iter.next();
} else if !arg.starts_with('-') {
words.push(arg);
if words.len() == 2 {
break;
}
}
}
match words.first().map(AsRef::as_ref) {
Some("exec") => true,
Some("secret") => words.get(1).map(AsRef::as_ref) == Some("get"),
_ => false,
}
}