use std::env;
use std::io::{self, Read};
use std::path::{Path, PathBuf};
use std::process::exit;
use balls::chore;
use balls::chore_cli::Cli;
use balls::layout::Xdg;
fn main() {
let args: Vec<String> = env::args().skip(1).collect();
if args.first().map(String::as_str) == Some("protocol") {
println!("{}", chore::PROTOCOL_JSON);
return;
}
if let Err(e) = run(&args) {
eprintln!("bl-chore: {e}");
exit(1);
}
}
fn run(args: &[String]) -> io::Result<()> {
let op = args.first().ok_or_else(|| io::Error::other("usage: bl-chore <op> <phase>"))?;
let phase = args.get(1).ok_or_else(|| io::Error::other("usage: bl-chore <op> <phase>"))?;
let mut stdin = String::new();
io::stdin().read_to_string(&mut stdin)?;
let plugin = env::var("BALLS_PLUGIN_NAME").unwrap_or_else(|_| "bl-chore".to_string());
let home = env::var("HOME").map_err(|_| io::Error::other("HOME is unset (set by balls per §6)"))?;
let xdg = Xdg::with(Path::new(&home), env::var("XDG_CONFIG_HOME").ok().as_deref(), env::var("XDG_STATE_HOME").ok().as_deref());
let bl = Cli::at(PathBuf::from("bl"));
chore::run(op, phase, &plugin, &xdg.plugin_territory(&plugin), &stdin, &bl)
}