use std::env;
use std::io::{Error, ErrorKind, Result};
use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus};
fn main() -> Result<()> {
let mut forwarded: Vec<String> = env::args().skip(1).collect();
forwarded.insert(0, "server".to_string());
exec_athena_rs(&forwarded)
}
fn exec_athena_rs(args: &[String]) -> Result<()> {
let athena_bin: PathBuf = resolve_sibling_athena_bin()?;
let status: ExitStatus = Command::new(&athena_bin)
.args(args)
.status()
.map_err(|err| {
Error::new(
ErrorKind::Other,
format!("failed to spawn {}: {}", athena_bin.display(), err),
)
})?;
if status.success() {
Ok(())
} else {
Err(Error::new(
ErrorKind::Other,
format!("{} exited with status {}", athena_bin.display(), status),
))
}
}
fn resolve_sibling_athena_bin() -> Result<PathBuf> {
let current_exe: PathBuf = env::current_exe()?;
let parent: &Path = current_exe.parent().ok_or_else(|| {
Error::new(
ErrorKind::NotFound,
"unable to resolve executable directory for server launcher",
)
})?;
let candidate = if cfg!(windows) {
parent.join("athena_rs.exe")
} else {
parent.join("athena_rs")
};
if candidate.is_file() {
Ok(candidate)
} else {
Err(Error::new(
ErrorKind::NotFound,
format!(
"could not find companion athena_rs binary at {}; run `cargo build` first",
candidate.display()
),
))
}
}