. "$BST_LIB"
require gzip gzip/present

# The shipped /bin/gzip must be GNU gzip (update-alternatives winner, priority
# 100 over the busybox applet's 50). GNU gzip --version prints the FSF banner;
# busybox gzip has no --version.
check_out gzip/gnu-version "Free Software Foundation" sh -c "gzip --version 2>&1"

tmp=${TMPDIR:-/tmp}/bst-gzip.$$
line="the quick brown fox jumps over the lazy dog 0123456789"
i=0
while [ $i -lt 64 ]; do printf "%s\n" "$line"; i=$((i+1)); done > "$tmp.in" 2>/dev/null

# Compress via the stream form (gzip -c > file), NOT the named-file form
# (gzip file): the named-file path restores owner/perms/mtime on the output and
# unlinks the input (fchmod/fchown/utimes/unlink), historically ENOSYS on the
# wasm32-hwjs runtime (brintos/linux#8). The -c stream form touches no file
# attributes and exits clean. This is also the form the ticket specifies.
check     gzip/compress   sh -c "gzip -c '$tmp.in' > '$tmp.gz'"
# real gzip container: the 2-byte magic is 1f 8b (od, since busybox head has no -c).
check_out gzip/magic      "1f 8b"  sh -c "od -An -tx1 -N2 '$tmp.gz'"
# integrity self-check of the compressed stream
check     gzip/test       gzip -t "$tmp.gz"
# decompress round-trip recovers the original text (gunzip -c)
check_out gzip/roundtrip  "$line"  sh -c "gunzip -c '$tmp.gz'"
# zcat alternative decompresses too (also a GNU gzip update-alternatives link)
check_out gzip/zcat       "$line"  sh -c "zcat '$tmp.gz'"
# byte-for-byte identity of the round-trip
gunzip -c "$tmp.gz" > "$tmp.out" 2>/dev/null
check     gzip/identical  cmp -s "$tmp.in" "$tmp.out"
# the ticket's explicit requirement: gzip -c | gunzip -c round-trips a stream
check_out gzip/pipe       "$line"  sh -c "printf '%s\n' '$line' | gzip -c | gunzip -c"

# Named-file mode (gzip -kf FILE -> FILE.gz) restores attributes on the output
# and unlinks the input -- the fchmod/fchown/utimes/unlink path of brintos/linux#8
# (fixed on joel-dev 2026-07-14, kernel 790070a9c31e + hardwarejs 0d86fea). Exercise
# it as a real check so the suite confirms the fix is live on the deployed runtime;
# if it still ENOSYS's, this flips to a skip against #8 (see the design doc).
check_out gzip/named-file "$line"  sh -c "cp '$tmp.in' '$tmp.nf' && gzip -kf '$tmp.nf' && gunzip -c '$tmp.nf.gz'"

rm -f "$tmp.in" "$tmp.gz" "$tmp.out" "$tmp.nf" "$tmp.nf.gz"
