const std = @import("std");
const builtin = @import("builtin");

pub fn build(b: *std.Build) !void {
    const test_step = b.step("test", "Test it");
    b.default_step = test_step;

    const optimize: std.builtin.OptimizeMode = .Debug;
    const target = b.graph.host;

    if (builtin.os.tag != .windows) return;

    const echo_args = b.addExecutable(.{
        .name = "echo-args",
        .root_module = b.createModule(.{
            .root_source_file = b.path("echo-args.zig"),
            .optimize = optimize,
            .target = target,
        }),
    });

    const bat_files = b.addWriteFiles();
    {
        const echo_args_basename = "echo-args.exe";
        const preamble = std.fmt.allocPrint(b.allocator,
            \\@echo off
            \\"{s}"
        , .{echo_args_basename}) catch @panic("OOM");
        // Trailing newline intentionally omitted above so we can add args.

        _ = bat_files.add("args1.bat", std.mem.concat(b.allocator, u8, &.{ preamble, " %*" }) catch @panic("OOM"));
        _ = bat_files.add("args2.bat", std.mem.concat(b.allocator, u8, &.{ preamble, " %1 %2 %3 %4 %5 %6 %7 %8 %9" }) catch @panic("OOM"));
        _ = bat_files.add("args3.bat", std.mem.concat(b.allocator, u8, &.{ preamble, " \"%~1\" \"%~2\" \"%~3\" \"%~4\" \"%~5\" \"%~6\" \"%~7\" \"%~8\" \"%~9\"" }) catch @panic("OOM"));

        _ = bat_files.addCopyFile(echo_args.getEmittedBin(), echo_args_basename);
    }

    const test_exe = b.addExecutable(.{
        .name = "test",
        .root_module = b.createModule(.{
            .root_source_file = b.path("test.zig"),
            .optimize = optimize,
            .target = target,
        }),
    });

    const run = b.addRunArtifact(test_exe);
    run.setCwd(bat_files.getDirectory());
    run.expectExitCode(0);
    run.skip_foreign_checks = true;

    test_step.dependOn(&run.step);

    const fuzz = b.addExecutable(.{
        .name = "fuzz",
        .root_module = b.createModule(.{
            .root_source_file = b.path("fuzz.zig"),
            .optimize = optimize,
            .target = target,
        }),
    });

    const fuzz_max_iterations = b.option(u64, "iterations", "The max fuzz iterations (default: 100)") orelse 100;
    const fuzz_iterations_arg = std.fmt.allocPrint(b.allocator, "{}", .{fuzz_max_iterations}) catch @panic("oom");

    const fuzz_seed = b.option(u64, "seed", "Seed to use for the PRNG (default: random)") orelse seed: {
        var buf: [8]u8 = undefined;
        b.graph.io.random(&buf);
        break :seed std.mem.readInt(u64, &buf, builtin.cpu.arch.endian());
    };
    const fuzz_seed_arg = std.fmt.allocPrint(b.allocator, "{}", .{fuzz_seed}) catch @panic("oom");

    const fuzz_run = b.addRunArtifact(fuzz);
    fuzz_run.setCwd(bat_files.getDirectory());
    fuzz_run.addArgs(&.{ fuzz_iterations_arg, fuzz_seed_arg });
    fuzz_run.expectExitCode(0);
    fuzz_run.skip_foreign_checks = true;

    test_step.dependOn(&fuzz_run.step);
}
