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

use gba_cart::Cart;

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

pub fn main() {
    let args: Vec<String> = env::args().collect();
    println!("[");
    for (i, filename) in args[1..].iter().enumerate() {
        let mut f = File::open(filename).unwrap();
        let cart = Cart::try_from(&mut f);
        if let Err(ref err) = cart {
            eprintln!("Error: {err}");
            continue;
        }
        let mut cart = cart.unwrap();
        let computed_header_checksum = cart.header_checksum().unwrap();

        let header = &cart.metadata;

        println!("\t{{");

        println!("\t\t\"entry point\": 0x{:08X},", header.entry_point);
        println!("\t\t\"title\": \"{}\",", header.title);
        println!("\t\t\"game code\": \"{}\",", header.game_code);
        println!(
            "\t\t\"manufacturer code\": \"{}\",",
            header.manufacturer_code
        );
        println!("\t\t\"main unit code\": {},", header.main_unit_code);
        println!("\t\t\"device type\": {},", header.device_type);
        println!("\t\t\"version\": {},", header.software_version);
        println!(
            "\t\t\"header checksum\": \"0x{:02X}\",",
            header.header_checksum
        );
        println!("\t\t\"computed header checksum\": \"0x{computed_header_checksum:02X}\"");

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