const std = @import("std");
const linux = std.os.linux;
const mods = @import("mods.zig");

const common = @import("common.zig");

pub fn main() !void {
    var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
    defer _ = gpa.deinit();
    const alloc = gpa.allocator();

    var root = try std.fs.cwd().openDir(".mod-cache", .{});
    defer root.close();

    var index = try mods.ModIndex.init(alloc, root);
    defer index.deinit();

    var env = try std.process.getEnvMap(alloc);
    defer env.deinit();

    if (env.get("MODS_DIR")) |mods_dir| {
        var dir = try std.fs.cwd().openDir(mods_dir, .{ .iterate = true });
        defer dir.close();

        try index.load(dir);
    } else {
        const exe_dir_path = try std.fs.selfExeDirPathAlloc(alloc);
        defer alloc.free(exe_dir_path);

        const mods_dir_path = try std.fs.path.resolve(
            alloc,
            &.{ exe_dir_path, "..", "mods" },
        );
        defer alloc.free(mods_dir_path);

        var dir = try std.fs.cwd().openDir(mods_dir_path, .{});
        defer dir.close();

        try index.load(dir);
    }

    for (index.libs.items) |*lib| {
        const sym = lib.lib.lookup(
            common.Perform,
            "_perform",
        ).?;
        sym(.quack);
    }
}
