const std = @import("std");

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

    const optimize = b.standardOptimizeOption(.{});

    // TODO: rewrite as zig tool
    // TODO: reproducible build
    const assets = b.addSystemCommand(&[_][]const u8{
        "tar",
        "-cf",
        b.path("assets.tar").getPath(b),
        "-C",
        b.path("assets").getPath(b),
    });
    assets.addArgs(&[_][]const u8{
        "write.tmx",
        "sprites.png",
        "walrus.png",
        "player.png",
        "menu.qoi",
        "ui.qoi",
        "Shantell_Sans-Informal_Regular.otf",
        "Shantell_Sans-Informal_Bold.otf",
        "cp863.png",
    });

    const tmx_dep = b.dependency("tmx", .{ .target = target, .optimize = optimize });
    const tmx = b.addStaticLibrary(.{ .name = "tmx", .target = target, .optimize = optimize });
    tmx.addCSourceFiles(.{ .root = tmx_dep.path("src/"), .files = &[_][]const u8{ "tmx.c", "tmx_utils.c", "tmx_err.c", "tmx_xml.c", "tmx_mem.c", "tmx_hash.c" }, .flags = &[_][]const u8{ "-Wall", "-O2", "-pthread", "-fno-sanitize=undefined", "-DWANT_ZSTD", "-DWANT_ZLIB" } });
    tmx.linkLibC();
    tmx.linkSystemLibrary("z");
    tmx.linkSystemLibrary("zstd");
    tmx.linkSystemLibrary2("libxml-2.0", .{ .use_pkg_config = .force });
    tmx.installHeader(tmx_dep.path("src/tmx.h"), "tmx.h");
    b.installArtifact(tmx);

    const exe = b.addExecutable(.{
        .name = "walrus",
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
    });

    const strip = b.option(
        bool,
        "strip",
        "Strip debug info to reduce binary size, defaults to false",
    ) orelse false;
    exe.root_module.strip = strip;

    const useupx = b.option(
        bool,
        "upx",
        "Use UPX to compress the binary further",
    ) orelse false;

    exe.root_module.addAnonymousImport("assets", .{ .root_source_file = b.path("assets.tar") });

    const raylib = b.dependency("raylib", .{ .target = target, .optimize = optimize });
    // const raylib = @import("raylib");
    // const raylib_artifact = try raylib.addRaylib(b, target, optimize, .{
    //     .raygui = false,
    // });
    //exe.addModule("raylib", raylib.module("raylib"));
    //exe.installLibraryHeaders(raylib.artifact("raylib"));
    //exe.installLibraryHeaders(tmx);

    b.installArtifact(exe);

    exe.linkLibC();
    exe.linkLibrary(raylib.artifact("raylib"));
    exe.linkLibrary(tmx);
    exe.step.dependOn(&assets.step);

    if (useupx) {
        const upx = b.addSystemCommand(&[_][]const u8{ "upx", "--brute", "--no-lzma", "--no-progress" });
        upx.addArtifactArg(exe);
        upx.step.dependOn(&exe.step);
        b.default_step.dependOn(&upx.step);
    }

    const run_cmd = b.addRunArtifact(exe);

    run_cmd.step.dependOn(b.getInstallStep());

    if (b.args) |args| {
        run_cmd.addArgs(args);
    }

    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);
}
