mod config;
mod http;
mod udp;

use clap::Parser;
use config::Config;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
    let config = Config::parse();

    if config.http_tracker.as_ref().is_none_or(|h| h.is_empty())
        && config.udp_tracker.as_ref().is_none_or(|u| u.is_empty())
    {
        return Err("Provide at least one HTTP or UDP tracker(s)".into());
    }

    if let Some(http_tracker) = config.http_tracker {
        for url in http_tracker {
            if let Err(e) =
                http::handle(&config.info_hash, url.clone(), config.port, config.timeout)
            {
                eprintln!("{e}")
            }
        }
    }

    if let Some(udp_tracker) = config.udp_tracker {
        for address in udp_tracker {
            if let Err(e) = udp::handle(&config.info_hash, &address, config.port, config.timeout) {
                eprintln!("{e}")
            }
        }
    }

    Ok(())
}
