const std = @import("std");

pub fn installModule(b: *std.Build, plug: *std.Build.Step.Compile) void {
    std.debug.assert(plug.isDynamicLibrary());
    const install = b.addInstallArtifact(plug, .{
        .dest_dir = .{ .override = .{ .custom = "mods" } },
    });
    b.getInstallStep().dependOn(&install.step);
}

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const exedriver = b.addExecutable(.{
        .name = "driver",
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
        .link_libc = true,
    });
    b.installArtifact(exedriver);

    const libfoo = b.addSharedLibrary(.{
        .name = "foo",
        .root_source_file = b.path("src/foo.zig"),
        .target = target,
        .optimize = optimize,
        .link_libc = true,
    });
    installModule(b, libfoo);

    const libbar = b.addSharedLibrary(.{
        .name = "bar",
        .root_source_file = b.path("src/bar.zig"),
        .target = target,
        .optimize = optimize,
        .link_libc = true,
    });
    installModule(b, libbar);

    const run = b.addRunArtifact(exedriver);
    run.step.dependOn(b.getInstallStep());
    run.setEnvironmentVariable("MODS_DIR", b.getInstallPath(.{ .custom = "mods" }, ""));

    b.step("run", "").dependOn(&run.step);
}
