RUST_SRC := `rustc --print sysroot` / "lib/rustlib/src/rust/library/std/src"

copy-io:
    #!/usr/bin/env bash
    set -e

    FILES=(
        "io/error/repr_unpacked.rs"
        "io/error/repr_bitpacked.rs"
        "io/error.rs"
        "io/buffered/mod.rs"
        "io/buffered/linewriter.rs"
        "io/buffered/linewritershim.rs"
        "io/buffered/bufwriter.rs"
        "io/buffered/bufreader.rs"
        "io/buffered/bufreader/buffer.rs"
        "io/copy.rs"
        "io/cursor.rs"
        "io/impls.rs"
        "io/prelude.rs"
        "io/util.rs"
        "io/mod.rs"
    )

    for f in "${FILES[@]}"; do
        echo "Processing $f..."
        DEST="src/$f"

        mkdir -p "$(dirname "$DEST")"

        cp "{{ RUST_SRC }}/$f" "$DEST"
        sed -i -E -n '$!N; /^#\[cfg\(test\)\]\nmod tests/d; P; D' "$DEST"

        if [[ "$f" == "io/error.rs" ]]; then
            sed -i "s/alloc::/alloc_crate::/g" "$DEST"
        fi

        if [[ "$f" == "io/mod.rs" ]]; then
            sed -i "/mod pipe/d" "$DEST"
            sed -i "/self::pipe/d" "$DEST"
            sed -i "/mod stdio/d" "$DEST"
            sed -i "/stdio::/d" "$DEST"
            sed -i "/self::stdio/d" "$DEST"
            sed -i "/feature = \"is_terminal\"/d" "$DEST"
            sed -i "/feature = \"print_internals\"/d" "$DEST"
            sed -i "/feature = \"internal_output_capture\"/d" "$DEST"
            sed -i "/feature = \"anonymous_pipe\"/d" "$DEST"
        fi
    done
