. "$BST_LIB"
require sed sed/present

# Busybox also provides a sed applet; this suite covers the GNU sed this ticket
# ships. Assert the shipped /bin/sed is GNU sed (update-alternatives winner).
check_out sed/gnu-version "GNU sed" sed --version

tmp=${TMPDIR:-/tmp}/bst-sed.$$
printf 'alpha\nbeta\ngamma\n' > "$tmp"

# The ticket's named criterion: stream mode, stdin -> stdout.
check_eq sed/subst-pipe "Xbc"   "$(printf 'abc\n' | sed 's/a/X/')"
check_eq sed/subst-file "alpha,BETA,gamma," "$(sed 's/beta/BETA/' "$tmp" | tr '\n' ',')"
check_eq sed/delete     "alpha,gamma,"      "$(sed '/beta/d' "$tmp" | tr '\n' ',')"
check_eq sed/print-n    "beta"              "$(sed -n '2p' "$tmp")"
check_eq sed/ere        "aMATCHz"           "$(printf 'a123z\n' | sed -E 's/[0-9]+/MATCH/')"

# sed -i commits through a temp file + rename(). rename/renameat2 were ENOSYS on
# the wasm32 kernel until brintos/linux#5 (kernel 790070a9c31e + hardwarejs
# 0d86fea), so this case doubles as the on-target witness for that fix.
cp "$tmp" "$tmp.i"
sed -i 's/beta/BETA/' "$tmp.i" 2>/dev/null; rc=$?
out=$(tr '\n' ',' < "$tmp.i" 2>/dev/null)
if [ "$rc" -eq 0 ] && [ "$out" = "alpha,BETA,gamma," ]; then
  bst_ok sed/in-place
else
  bst_notok sed/in-place "rc=$rc got:'$out' — rename path, brintos/linux#5"
fi

rm -f "$tmp" "$tmp.i"
