const std = @import("std");

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const ally = gpa.allocator();

    std.debug.print("All your {s} are belong to us.\n", .{"codebase"});

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

    try stdout.print("Run `zig build test` to run the tests.\n", .{});

    try bw.flush();

    const exe_dir_path = try std.fs.selfExeDirPathAlloc(ally);
    defer ally.free(exe_dir_path);

    const res_dir_path = try std.fs.path.resolve(ally, &.{ exe_dir_path, "..", "res" });
    defer ally.free(res_dir_path);

    // std.debug.print("exe path: '{s}'\n", .{exe_dir_path});

    var res_dir = try std.fs.openDirAbsolute(res_dir_path, .{});
    defer res_dir.close();

    {
        var fb = try res_dir.openFile("fizzbuzz", .{});
        defer fb.close();

        const content = try fb.reader().readAllAlloc(ally, 1024);
        defer ally.free(content);

        std.debug.print("Read: '{s}'\n", .{content});
    }

    {
        var stamp = try res_dir.openFile("foobar", .{});
        defer stamp.close();

        const content = try stamp.reader().readAllAlloc(ally, 1024);
        defer ally.free(content);

        std.debug.print("generated foobar: '{s}'\n", .{content});
    }
}

test "simple test" {
    var list = std.ArrayList(i32).init(std.testing.allocator);
    defer list.deinit();
    try list.append(42);
    try std.testing.expectEqual(@as(i32, 42), list.pop());
}
