// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR MPL-2.0
// SPDX-FileCopyrightText: 2024 Gabriel Marcano <gabemarcano@yahoo.com>

use n64_cart::Cart;

use std::env;
use std::fs::File;
use std::str;

pub fn main() {
    let args: Vec<String> = env::args().collect();

    println!("[");
    for (i, filepath) in args[1..].iter().enumerate() {
        let f = File::open(filepath).unwrap();
        let mut cart = match Cart::new(f) {
            Ok(cart) => cart,
            Err(err) => {
                eprintln!("Error with file \"{filepath}\": {err}");
                continue;
            }
        };
        let computed_check_code = cart.compute_checksum().unwrap();
        let header = &cart.header;

        println!("\t{{");

        println!(
            "\t\t\"PI DOM1 configuration flags\": \"0x{:08X}\",",
            header.pi_dom1_configuration_flags
        );
        println!("\t\t\"clock rate\": \"0x{:X}\",", header.clock_rate);
        println!("\t\t\"boot address\": \"0x{:08X}\",", header.boot_address);
        println!("\t\t\"libultra version\": \"{}\",", header.libultra_version);
        println!("\t\t\"check code\": \"0x{:016X}\",", header.check_code);
        println!("\t\t\"computed check code\": \"0x{computed_check_code:016X}\",");
        println!(
            "\t\t\"reserved (maybe?)\": \"0x{:X}\",",
            header.reserved_maybe
        );
        println!(
            "\t\t\"game title\": \"{}\",",
            str::from_utf8(&header.game_title).unwrap().trim()
        ); // This will blow up in Japanese
        println!("\t\t\"reserved2\": {:X?},", header.reserved2);
        println!("\t\t\"game code\": \"{}\",", header.game_code);
        println!("\t\t\"ROM version\": \"0x{:02X}\"", header.rom_version);
        // FIXME
        //println!("\t\t\"IPL3\": 0x{:02X},", header.ipl3));

        if i == (args[1..].len() - 1) {
            println!("\t}}");
        } else {
            println!("\t}},");
        }
    }
    println!("]");
}
