//! Entry point for the FIFA 17 redirector host.

use openfut_redirector_host::{serve, RedirectorConfig};

fn main() {
    // Identity must be readable WITHOUT valid configuration: the launcher has
    // to verify which commit a binary came from before deciding whether to run
    // it at all, and refusing to reveal that until the environment is right
    // would invert the check.
    if std::env::args().any(|a| a == "--identity") {
        println!("{}", openfut_redirector_host::identity());
        return;
    }

    let cfg = match RedirectorConfig::from_env() {
        Ok(c) => c,
        Err(e) => {
            eprintln!("openfut-redirector-host: {e}\n");
            eprintln!("Required:");
            eprintln!(
                "  OPENFUT_ADVERTISE               address the game machine reaches this host at"
            );
            eprintln!(
                "  OPENFUT_REDIRECTOR_HOST_PORT    listen port (no default: runs beside Python)"
            );
            eprintln!(
                "  OPENFUT_REDIRECTOR_CERT         RSA certificate (reuse the oracle's for A/B)"
            );
            eprintln!("  OPENFUT_REDIRECTOR_KEY          matching private key");
            eprintln!("Optional:");
            eprintln!("  OPENFUT_REDIRECTOR_HOST_BIND    listener bind (defaults to OPENFUT_BIND)");
            eprintln!("  OPENFUT_BLAZE_ADVERTISED_PORT   Blaze port put in the redirect");
            eprintln!("  OPENFUT_REDIRECTOR_CIPHERS      override the cipher list");
            eprintln!("  OPENFUT_REDIRECTOR_SECURITY_LEVEL  only with evidence it is needed");
            std::process::exit(2);
        }
    };
    if let Err(e) = serve(cfg) {
        eprintln!("openfut-redirector-host: fatal: {e}");
        std::process::exit(1);
    }
}
