const std = @import("std");

pub fn main() !void {
    var args = std.process.args();
    defer args.deinit();

    std.debug.assert(args.skip());

    const filename = args.next() orelse {
        std.debug.print("Invalid usage.\n", .{});
        return;
    };

    const cwd = std.fs.cwd();
    const file = try cwd.openFileZ(filename, .{
        .mode = .read_only,
        .lock = .none,
    });
    defer file.close();

    var rb = std.io.bufferedReader(file.reader());
    const in = rb.reader();

    var stdout = std.io.getStdOut();
    var bw = std.io.bufferedWriter(stdout.writer());

    const out = bw.writer();

    const N = 16;
    const B = 2;

    var idx: usize = 0;
    while (in.readByte() catch null) |b| {
        idx += 1;
        try out.print("{x:0>2}", .{b});
        if (idx % N == 0) {
            try out.print("\n", .{});
            idx = 0;
        } else if (idx % B == 0) {
            try out.print(" ", .{});
        }
    }

    if (idx != 0) {
        try out.print("\n", .{});
    }
    try out.writeByteNTimes('=', 2 * N + N / B - 1);

    try bw.flush();
}
