#!/bin/sh
# Check the installed package: every utility starts, and the most used ones
# give the expected output. The upstream test suite runs at build time; this
# catches packaging breakage (missing links, libstdbuf path, locales, ...).
set -u

BIN=/usr/lib/cargo/bin/coreutils
fail=0

ok() { echo "PASS: $1"; }
ko() { echo "FAIL: $1"; fail=1; }

# expect NAME EXPECTED COMMAND...
expect() {
    name=$1 want=$2
    shift 2
    got=$("$@" 2>&1) || { ko "$name (exit $?): $got"; return; }
    if [ "$got" = "$want" ]; then ok "$name"; else ko "$name: got '$got', want '$want'"; fi
}

[ -x /usr/bin/coreutils ] || { echo "FAIL: /usr/bin/coreutils missing"; exit 1; }

# Every installed utility link must run. "[" needs a closing bracket,
# "false --version" exits 1 and "test --version" prints nothing (as in GNU),
# so they are checked below instead.
# hostname, kill, more and uptime are built but deliberately not linked.
count=0
for path in "$BIN"/*; do
    util=${path##*/}
    case $util in "[" | false | test) continue ;; esac
    out=$("$path" --version 2>&1) || { ko "$util --version: $out"; continue; }
    # An unknown name ending in a known one runs that one instead (a stale
    # "sha3sum" link runs "sum"), so check which utility actually answered.
    case $out in
        "$util "*) count=$((count + 1)) ;;
        *) ko "$util --version answered as: ${out%%
*}" ;;
    esac
done
[ "$count" -gt 90 ] && ok "$count utilities run" || ko "only $count utilities run"
for util in cat chmod chown cp date dd df du echo env install ln ls mkdir \
        mktemp mv readlink rm sort stat stdbuf tail touch tr uname; do
    [ -x "$BIN/$util" ] || ko "$util: $BIN/$util missing"
done

WORK=$(mktemp -d)
trap 'rm -rf "$WORK"' EXIT
cd "$WORK"

printf 'b,2\na,1\nc,3\na,1\n' > in.csv
expect "sort" "$(printf 'a,1\na,1\nb,2\nc,3')" "$BIN/sort" in.csv
expect "sort -u" "$(printf 'a,1\nb,2\nc,3')" "$BIN/sort" -u in.csv
expect "cut" "$(printf 'b\na\nc\na')" "$BIN/cut" -d, -f1 in.csv
expect "wc -l" "4" sh -c "$BIN/wc -l < in.csv"
expect "head" "b,2" "$BIN/head" -n1 in.csv
expect "tail" "a,1" "$BIN/tail" -n1 in.csv
expect "tr" "B,2" sh -c "$BIN/head -n1 in.csv | $BIN/tr b B"
expect "uniq" "$(printf 'a,1\nb,2\nc,3')" sh -c "$BIN/sort in.csv | $BIN/uniq"
expect "seq" "$(printf '1\n2\n3')" "$BIN/seq" 3
expect "expr" "6" "$BIN/expr" 2 \* 3
expect "basename" "c" "$BIN/basename" /a/b/c
expect "dirname" "/a/b" "$BIN/dirname" /a/b/c
expect "echo" "hello" "$BIN/echo" hello
expect "printf" "x=42" "$BIN/printf" 'x=%d' 42
expect "true" "" "$BIN/true"
if "$BIN/false"; then ko "false"; else ok "false"; fi
expect "test" "" "$BIN/[" -d / ]
expect "factor" "12: 2 2 3" "$BIN/factor" 12
expect "md5sum" "900150983cd24fb0d6963f7d28e17f72  -" sh -c "printf abc | $BIN/md5sum"
expect "sha256sum" "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad  -" sh -c "printf abc | $BIN/sha256sum"
expect "base64" "YWJj" sh -c "printf abc | $BIN/base64"
expect "date" "1970-01-01" "$BIN/date" -u -d @0 +%F

# Files
"$BIN/mkdir" -p d/e && [ -d d/e ] && ok "mkdir -p" || ko "mkdir -p"
"$BIN/touch" d/f && [ -f d/f ] && ok "touch" || ko "touch"
"$BIN/cp" in.csv d/copy && cmp -s in.csv d/copy && ok "cp" || ko "cp"
"$BIN/mv" d/copy d/moved && [ -f d/moved ] && [ ! -e d/copy ] && ok "mv" || ko "mv"
"$BIN/ln" -s moved d/link && [ "$("$BIN/readlink" d/link)" = moved ] && ok "ln -s/readlink" || ko "ln -s/readlink"
"$BIN/chmod" 600 d/f && [ "$("$BIN/stat" -c %a d/f)" = 600 ] && ok "chmod/stat" || ko "chmod/stat"
expect "ls" "$(printf 'e\nf\nlink\nmoved')" "$BIN/ls" d
expect "du" "d/e" sh -c "$BIN/du d | $BIN/head -n1 | $BIN/cut -f2"
"$BIN/rm" -r d && [ ! -e d ] && ok "rm -r" || ko "rm -r"

# stdbuf preloads libstdbuf.so from the path set at build time.
expect "stdbuf" "a,1" sh -c "$BIN/stdbuf -oL $BIN/tail -n1 in.csv"

# Translations are installed and picked up (not required to be complete).
if [ -d /usr/share/coreutils/locales ]; then
    ok "locales installed"
else
    ko "/usr/share/coreutils/locales missing"
fi

exit $fail
