//
// Omtoy build script
//
// Build required OMT parts and link against our code
//
// Run `zig build build-omt` to build dependencies before anything else.
//
// TODO: I don't know if libvmx needs to be in the same directory as libomt _for libomt.so_
//       or for the final executables. Right now I just move it next to libomt which is also
//       passed as libLibrary for the executables
// TODO: Use install commands to move the libomt artifacts to a more sensible place
const std = @import("std");

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

    const triple = target.result;
    if (triple.os.tag != .linux or triple.cpu.arch != .x86_64) {
        std.log.warn("The build file only supports Linux x86-64 right now.\n", .{});
        std.log.warn(
            "You can extend the build.zig to call the appropriate platform scripts " ++
                "in 3rd/libomtnet/build/ and 3rd/libomt/build.\n",
            .{},
        );
    }

    // Build libomtnet
    const libomtnet_build = b.addSystemCommand(&.{"bash"});
    libomtnet_build.cwd = b.path("3rd/libomtnet/build");
    libomtnet_build.addFileArg(b.path("3rd/libomtnet/build/buildall.sh"));

    // Build libomt
    const libomt_build = b.addSystemCommand(&.{"bash"});
    libomt_build.cwd = b.path("3rd/libomt/build");
    libomt_build.addFileArg(b.path("3rd/libomt/build/buildlinuxx64.sh"));
    libomt_build.step.dependOn(&libomtnet_build.step);
    // TODO: install libomt.so and libomt.h in a more convenient location

    const libvmx_build = b.addLibrary(.{
        .root_module //todo
        .name = "libvmx",
        .linkage = .dynamic,
    });
    libvmx_build.linkLibCpp();
    libvmx_build.root_module.addCSourceFiles(.{
        .root = b.path("3rd/libvmx/build/"),
        .files = .{ "../src/vmxcodec_x86.cpp", "../src/vmxcodec_avx2.cpp", "../src/vmxcodec.cpp" },
        .flags = .{ "-O3", "-std=c++17", "-fdeclspec", "-fPIC", "-mlzcnt", "-mavx2", "-mbmi", "-shared", "-Wl,-rpath,'$ORIGIN'" },
        .language = null,
    });

    // Copy libvmx to a place of happiness
    // TODO: Runs but doesn't do what I'd expect it to. What's going on?
    // const libvmx_copy = b.addSystemCommand(&.{"cp"});
    // libvmx_copy.addFileArg(b.path("3rd/libvmx/build/libvmx.so"));
    // _ = libvmx_copy.addOutputFileArg("3rd/libomt/bin/Release/net8.0/linux-x64/publish/libvmx.so");
    // libvmx_copy.step.dependOn(&libvmx_build.step);

    // Define executable
    const sender_exe = b.addExecutable(.{
        .name = "omtoy-sender",
        .root_module = b.createModule(.{
            .root_source_file = b.path("src/sender.zig"),
            .target = target,
            .optimize = optimize,
        }),
    });

    const receiver_exe = b.addExecutable(.{
        .name = "omtoy-receiver",
        .root_module = b.createModule(.{
            .root_source_file = b.path("src/receiver.zig"),
            .target = target,
            .optimize = optimize,
        }),
    });

    // Link libomt with the sender and receiver
    {
        const omt_output_dir = b.path("3rd/libomt/bin/Release/net8.0/linux-x64/publish");
        sender_exe.addIncludePath(omt_output_dir);
        sender_exe.addLibraryPath(omt_output_dir);
        sender_exe.linkSystemLibrary("omt");
        sender_exe.linkLibC();
        receiver_exe.addIncludePath(omt_output_dir);
        receiver_exe.addLibraryPath(omt_output_dir);
        receiver_exe.linkSystemLibrary("omt");
        receiver_exe.linkLibC();
    }

    b.installArtifact(sender_exe);
    b.installArtifact(receiver_exe);

    // Manual build step for libomt dependencies
    const build_omt_step = b.step("build-omt", "Build libomt and libomtnet");
    build_omt_step.dependOn(&libomt_build.step);
    // build_omt_step.dependOn(&libvmx_copy.step);

    // Add run step for sender
    const run_sender_step = b.step("sender-run", "Run the sender client");
    const run_sender_cmd = b.addRunArtifact(sender_exe);
    run_sender_step.dependOn(&run_sender_cmd.step);

    const run_receiver_step = b.step("receiver-run", "Run the receiver client");
    const run_receiver_cmd = b.addRunArtifact(receiver_exe);
    run_receiver_step.dependOn(&run_receiver_cmd.step);

    // Add run step for receiver

    run_sender_cmd.step.dependOn(b.getInstallStep());
    run_receiver_cmd.step.dependOn(b.getInstallStep());

    if (b.args) |args| {
        run_sender_cmd.addArgs(args);
        run_receiver_cmd.addArgs(args);
    }

    // TODO: write tests and stuff
    // const exe_tests = b.addTest(.{
    //     .root_module = sender_exe.root_module,
    // });

    // const run_exe_tests = b.addRunArtifact(exe_tests);

    // const test_step = b.step("test", "Run tests");
    // test_step.dependOn(&run_exe_tests.step);
}
