. "$BST_LIB"
# busybox ships a `patch` applet (CONFIG_PATCH=y) and busybox ash runs it in
# preference to $PATH, so a bare `patch` is busybox's, not this port. GNU patch
# installs under its update-alternatives name patch.patch (ALTERNATIVE_PRIORITY
# 100) — invoke that to prove this port specifically.
require patch.patch patch/present

tmp=${TMPDIR:-/tmp}/bst-patch.$$
printf 'line one\nline two\nline three\n' > "$tmp.orig" 2>/dev/null
printf 'line one\nline 2\nline three\n'   > "$tmp.new"  2>/dev/null

# unified diff orig->new, generated with busybox diff (always present; its -u
# output is patch-compatible). diff exits 1 when files differ; normalise it.
busybox diff -u "$tmp.orig" "$tmp.new" > "$tmp.diff" 2>/dev/null; true

# provenance: GNU patch prints "GNU patch"; the busybox applet does not.
check_out patch/version  "GNU patch"  patch.patch --version

# apply the diff via -o (write the result to a fresh output file, leaving the
# input untouched) and confirm the patched output equals new, byte-for-byte.
check     patch/apply      patch.patch -o "$tmp.out" "$tmp.orig" "$tmp.diff"
check_out patch/content    "line 2"    cat "$tmp.out"
check     patch/identical  cmp -s "$tmp.new" "$tmp.out"

# reverse the same diff (-R) against new to recover the original.
check     patch/reverse-ok  patch.patch -R -o "$tmp.rev" "$tmp.new" "$tmp.diff"
check     patch/reverse-eq  cmp -s "$tmp.orig" "$tmp.rev"

rm -f "$tmp.orig" "$tmp.new" "$tmp.diff" "$tmp.out" "$tmp.rev"
