SRC_DIR := "std/src"
PATCH_DIR := "std/patches"
RUST_SRC := `rustc --print sysroot` / "lib/rustlib/src/rust/library"
ROOT_LINKS := "\
    Cargo.toml backtrace std_detect panic_abort panic_unwind \
    windows_link unwind alloc rustc-std-workspace-alloc \
    rustc-std-workspace-core rustc-std-workspace-std \
    compiler-builtins core stdarch portable-simd proc_macro \
    profiler_builtins test coretests alloctests sysroot"
REAL_CP_FILES := "\
    sys/args/mod.rs \
    sys/io/error/mod.rs \
    sys/pal/mod.rs \
    sys/random/mod.rs \
    sys/thread_local/mod.rs"
STD_FILES := "\
    ../tests ../benches \
    alloc.rs ascii.rs backtrace.rs bstr.rs env.rs error.rs fs.rs \
    keyword_docs.rs lib.rs macros.rs panic.rs panicking.rs pat.rs \
    path.rs process.rs random.rs rt.rs tests_helpers.rs time.rs \
    backtrace collections ffi fs hash io net num os/raw/mod.rs \
    os/raw/tests.rs os/mod.rs prelude process sync sys/alloc/mod.rs \
    sys/args/unsupported.rs sys/args/common.rs sys/env/mod.rs sys/env/common.rs \
    sys/env/unsupported.rs sys/fd/mod.rs sys/fs/mod.rs \
    sys/fs/common.rs sys/fs/unsupported.rs sys/helpers/mod.rs \
    sys/helpers/small_c_string.rs sys/helpers/tests.rs sys/helpers/wstr.rs \
    sys/io/error/generic.rs sys/io/io_slice/unsupported.rs \
    sys/io/is_terminal/unsupported.rs sys/io/kernel_copy/mod.rs \
    sys/io/mod.rs sys/net/connection/mod.rs sys/net/connection/unsupported.rs \
    sys/net/hostname/mod.rs sys/net/hostname/unsupported.rs sys/net/mod.rs \
    sys/os_str/bytes/tests.rs sys/os_str/bytes.rs sys/os_str/mod.rs \
    sys/pal/unsupported/mod.rs sys/pal/unsupported/common.rs \
    sys/pal/unsupported/os.rs sys/path/mod.rs sys/path/unix.rs \
    sys/personality/dwarf/eh.rs sys/personality/dwarf/mod.rs \
    sys/personality/dwarf/tests.rs sys/personality/mod.rs sys/pipe/mod.rs \
    sys/pipe/unsupported.rs sys/platform_version/mod.rs sys/process/mod.rs \
    sys/process/env.rs sys/process/unsupported.rs sys/random/unsupported.rs \
    sys/stdio/mod.rs sys/stdio/unsupported.rs sys/sync/condvar/mod.rs \
    sys/sync/condvar/no_threads.rs sys/sync/mutex/mod.rs \
    sys/sync/mutex/no_threads.rs sys/sync/once/mod.rs \
    sys/sync/once/no_threads.rs sys/sync/rwlock/mod.rs \
    sys/sync/rwlock/no_threads.rs sys/sync/thread_parking/mod.rs \
    sys/sync/thread_parking/unsupported.rs sys/sync/mod.rs \
    sys/sync/once_box.rs sys/thread/mod.rs sys/thread/unsupported.rs \
    sys/thread_local/no_threads.rs sys/thread_local/os.rs sys/time/mod.rs \
    sys/time/unsupported.rs sys/backtrace.rs sys/cmath.rs \
    sys/configure_builtins.rs sys/env_consts.rs sys/exit.rs sys/mod.rs thread"
KEEP_FILES := "survos.rs ffi.rs"

setup-std:
    @echo "🔗 Linking root directories..."
    @for dir in {{ ROOT_LINKS }}; do ln -fs {{ RUST_SRC }}/$dir "."; done

    @echo "📂 Processing std/src files..."
    @for f in {{ STD_FILES }}; do {{ just_executable() }} cp_std $f; done
    @for f in {{ REAL_CP_FILES }}; do {{ just_executable() }} real_cp_std $f; done

    @{{ just_executable() }} cp_std "../build.rs"
    @{{ just_executable() }} cp_std "../Cargo.toml"
    @sed -i '59a\        || target_os == "survos"' std/src/../build.rs

update-std: setup-std patch-std

patch-std:
    @echo "🛠️ Patching the std..."
    @find {{ PATCH_DIR }} -type f -name "*.sed" | while read -r patch_file; do \
        rel="${patch_file#{{ PATCH_DIR }}/}"; \
        target="{{ SRC_DIR }}/${rel%.sed}.rs"; \
        if [ -f "$target" ]; then \
            echo " [SED] ${rel%.sed}.rs"; \
            sed -i -f "$patch_file" "$target"; \
        else \
            echo "⚠ [WARN] target missing: $target"; \
        fi; \
    done

build-sysroot:
    rm -rf target/riscv64/debug/deps/
    rm -rf ../target/riscv64/
    RUSTFLAGS="-Zforce-unstable-if-unmarked -C relocation-model=pic -C link-arg=-pie" \
        cargo build --package std --target ../riscv64.json --features compiler-builtins-mem
    mkdir -p ../sysroot/lib/rustlib/riscv64/lib
    rm -rf ../sysroot/lib/rustlib/riscv64/lib/*
    cp target/riscv64/debug/deps/*.rlib ../sysroot/lib/rustlib/riscv64/lib

clean:
    cargo clean
    rm -rf ../sysroot/lib/rustlib/riscv64/lib/*
    @for item in $(find std/src -type f); do \
        basename_item=$(basename "$item"); \
        keep=0; \
        for protected in {{ KEEP_FILES }}; do \
            if [ "$basename_item" = "$protected" ]; then keep=1; break; fi; \
        done; \
        if [ "$keep" -eq 0 ]; then \
            rm -rf "$item"; \
        fi; \
    done
    rm -f std/build.rs std/Cargo.toml Cargo.toml
    @for item in {{ ROOT_LINKS }}; do rm -rf $item; done

# --- Helpers ---

cp_std path:
    @mkdir -p {{ SRC_DIR / parent_directory(path) }}
    @ln -fs {{ RUST_SRC / "std/src" / path }} {{ parent_directory(SRC_DIR / path) }}

real_cp_std path:
    @mkdir -p {{ SRC_DIR / parent_directory(path) }}
    @cp -r {{ RUST_SRC / "std/src" / path }} {{ SRC_DIR / path }}
