#!/usr/bin/env python3
"""Convert a binary file to a C source array for firmware embedding."""

import sys


def bin2c(in_path: str, out_path: str, var_name: str) -> None:
    with open(in_path, "rb") as infile:
        data = infile.read()

    with open(out_path, "w", encoding="ascii") as outfile:
        outfile.write('#include "fusion_12_font.h"\n\n')
        outfile.write(f"const uint8_t {var_name}_data[] = {{\n")

        for i, byte in enumerate(data):
            if i % 12 == 0:
                outfile.write("    ")
            outfile.write(f"0x{byte:02x},")
            if i % 12 == 11:
                outfile.write("\n")

        if len(data) % 12 != 0:
            outfile.write("\n")

        outfile.write("};\n\n")
        outfile.write(f"const size_t {var_name}_size = sizeof({var_name}_data);\n")


def main() -> int:
    if len(sys.argv) != 4:
        print(f"usage: {sys.argv[0]} <input.bin> <output.c> <var_name>", file=sys.stderr)
        return 1

    bin2c(sys.argv[1], sys.argv[2], sys.argv[3])
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
