name := "HanjaKanaMun"
build_dir := "build"
odin_version := "dev-2026-09"
xwin_version := "0.10.0"

# Windows cross-compilation inputs (see `windows-deps`)
xwin := env("XWIN", build_dir / "xwin")
xwin_sdk := env("XWIN_SDK", build_dir / "xwin-sdk")
raylib_windows := env("RAYLIB_WINDOWS", build_dir / "raylib-windows")
lld_link := env("LLD_LINK", "lld-link")

# List available recipes
default:
    @just --list

# Debug build
build:
    odin build . -vet -debug -out:{{name}}

# Build and run (debug)
run:
    odin run . -vet -debug -out:{{name}}

# Type-check with vet, no binary
check:
    odin check . -vet

# Run the test suite (single-threaded: the tests share the memory game's global state)
test:
    mkdir -p {{build_dir}}
    # If the stack rlimit is "unlimited" (RLIM_INFINITY, as some CI/container hosts set it),
    # Odin's core:thread passes that straight to pthread_attr_setstacksize, pthread_create
    # then fails, and thread.create() returns nil - which core:testing's pool_init
    # dereferences unconditionally, segfaulting the whole test binary. Clamp it.
    ulimit -s 8192
    odin test tests -vet -define:ODIN_TEST_THREADS=1 -out:{{build_dir}}/tests

# Optimized native build into build/<os>/, with the assets it needs at runtime
release:
    mkdir -p {{build_dir}}/{{os()}}/{{name}}
    odin build . -vet -o:speed -out:{{build_dir}}/{{os()}}/{{name}}/{{name}}
    cp -R assets {{build_dir}}/{{os()}}/{{name}}/

# Native release build packed as build/<name>_<version>_<os>_<arch>.tar.gz
package version="dev": release
    tar -czf {{build_dir}}/{{name}}_{{version}}_{{os()}}_{{arch()}}.tar.gz -C {{build_dir}}/{{os()}} {{name}}
    @echo "{{build_dir}}/{{name}}_{{version}}_{{os()}}_{{arch()}}.tar.gz"

# Download the Windows SDK + CRT (via xwin, accepting Microsoft's license) and raylib.lib. Linux x86_64 only.
windows-deps:
    #!/usr/bin/env bash
    set -euo pipefail
    mkdir -p {{build_dir}}
    if [ ! -x "{{xwin}}" ]; then
        curl -fsSL https://github.com/Jake-Shadle/xwin/releases/download/{{xwin_version}}/xwin-{{xwin_version}}-x86_64-unknown-linux-musl.tar.gz \
            | tar -xz -C {{build_dir}} --strip-components=1 xwin-{{xwin_version}}-x86_64-unknown-linux-musl/xwin
    fi
    if [ ! -d "{{xwin_sdk}}/crt" ]; then
        "{{xwin}}" --accept-license --arch x86_64 --cache-dir {{build_dir}}/xwin-cache splat --output "{{xwin_sdk}}"
    fi
    # Odin's Linux/macOS releases don't ship raylib's Windows library, so take it from the Windows release
    if [ ! -f "{{raylib_windows}}/raylib.lib" ]; then
        mkdir -p "{{raylib_windows}}"
        curl -fsSL -o {{build_dir}}/odin-windows.zip \
            https://github.com/odin-lang/Odin/releases/download/{{odin_version}}/odin-windows-amd64-{{odin_version}}.zip
        unzip -o -j {{build_dir}}/odin-windows.zip 'dist/vendor/raylib/windows/raylib.lib' -d "{{raylib_windows}}"
    fi

# Cross-compile a Windows x86_64 build into build/windows/ (run `just windows-deps` first)
windows:
    #!/usr/bin/env bash
    set -euo pipefail
    obj_dir={{build_dir}}/obj-windows
    out_dir={{build_dir}}/windows/{{name}}
    mkdir -p "$obj_dir" "$out_dir"
    rm -f "$obj_dir"/*.obj
    odin build . -vet -o:speed -target:windows_amd64 -build-mode:obj -out:"$obj_dir/{{name}}.obj"
    # Odin can't link for Windows from other platforms, so link with lld-link.
    # The VC runtime is linked statically (no VCRUNTIME140.dll needed); the
    # Universal CRT is used from Windows itself (built into Windows 10+).
    {{lld_link}} /nologo /machine:x64 /subsystem:windows /entry:mainCRTStartup \
        /out:"$out_dir/{{name}}.exe" "$obj_dir"/*.obj "{{raylib_windows}}/raylib.lib" \
        /libpath:"{{xwin_sdk}}/crt/lib/x86_64" \
        /libpath:"{{xwin_sdk}}/sdk/lib/um/x86_64" \
        /libpath:"{{xwin_sdk}}/sdk/lib/ucrt/x86_64" \
        /nodefaultlib:msvcrt /nodefaultlib:vcruntime /nodefaultlib:libucrt \
        libcmt.lib libvcruntime.lib ucrt.lib \
        kernel32.lib user32.lib gdi32.lib winmm.lib shell32.lib opengl32.lib \
        advapi32.lib bcrypt.lib ntdll.lib synchronization.lib ole32.lib
    cp -R assets "$out_dir/"

# Windows build packed as build/<name>_<version>_windows_x86_64.zip
package-windows version="dev": windows
    cd {{build_dir}}/windows && zip -qr ../{{name}}_{{version}}_windows_x86_64.zip {{name}}
    @echo "{{build_dir}}/{{name}}_{{version}}_windows_x86_64.zip"

# Format all Odin files with odinfmt (settings in odinfmt.json)
fmt:
    odinfmt -w .

# Remove build output
clean:
    if [ -d {{build_dir}} ]; then rm -r -- {{build_dir}}; fi
    rm -f -- {{name}}
