use clap::Parser;
use epics_ca_rs::repeater::run_repeater_with_debug;
#[derive(Parser)]
#[command(name = "ca-repeater-rs", about = "EPICS CA Repeater")]
struct Args {
#[arg(short = 'v', long = "verbose")]
verbose: bool,
#[arg(short = 'd', long = "debug", action = clap::ArgAction::Count)]
debug: u8,
}
#[cfg(unix)]
fn detach_stdio() {
let path = b"/dev/null\0";
let dn = unsafe { libc::open(path.as_ptr() as *const _, libc::O_RDWR) };
if dn < 0 {
return;
}
unsafe {
libc::dup2(dn, 0);
libc::dup2(dn, 1);
libc::dup2(dn, 2);
if dn > 2 {
libc::close(dn);
}
}
}
#[cfg(not(unix))]
fn detach_stdio() {
}
fn main() {
let args = Args::parse();
if !args.verbose && args.debug == 0 {
detach_stdio();
}
let rt = tokio::runtime::Runtime::new().expect("failed to create tokio runtime");
let debug = args.debug;
rt.block_on(async move {
if let Err(e) = run_repeater_with_debug(debug).await {
if e.kind() == std::io::ErrorKind::AddrInUse {
return;
}
eprintln!("ca-repeater: {e}");
std::process::exit(1);
}
});
}