use std::{
    io::{self, BufRead},
};

mod bucket;
mod bucket_remote;
mod git_remote;
mod parser;

#[tokio::main]
async fn main() {
    let stdin = io::stdin();

    let address = std::env::args().nth(2).unwrap_or_else(|| {
        eprintln!("No j3 remote address was specified");
        std::process::exit(1);
    });

    let address = if address.starts_with("j3://") {
        &address[5..]
    } else if address.starts_with("j3::") {
        &address[4..]
    } else {
        eprintln!("Invalid j3 remote address: {}", address);
        std::process::exit(1);
    };

    let mut lines_iter = stdin.lock().lines().map(|res| match res {
        Ok(v) => v,
        Err(e) => {
            eprintln!("Error reading input: {}", e);
            std::process::exit(1);
        }
    });
    let bucket = bucket::Bucket::new_s3(address).await;
    let mut remote = bucket_remote::BucketRemote::new(bucket);
    parser::parse(&mut lines_iter, &mut remote).await;
}
