# Source this file in shell scripts:
# . "$(dirname "$0")/functions.inc"
#
#  functions.inc - utility functions for shell-based test scripts
#
#  Copyright © 2002—2015 Sam Hocevar <sam@hocevar.net>
#
#  This library is free software. It comes without any warranty, to
#  the extent permitted by applicable law. You can redistribute it
#  and/or modify it under the terms of the Do What the Fuck You Want
#  to Public License, Version 2, as published by the WTFPL Task Force.
#  See http://www.wtfpl.net/ for more details.
#

DIR="$(dirname "$0")"
ZZUF="$DIR/../src/zzuf -E [.]ilist$"

ZZAT="$DIR/../src/zzat"
if [ ! -f "$ZZAT" ]; then
  echo "error: src/zzat is missing"
  exit 1
fi

ZZERO="$DIR/zzero"
if [ ! -f "$ZZERO" ]; then
  echo "error: test/zzero is missing"
  exit 1
fi

# file -L follows wrappers; also detect rust/uutils coreutils (github #31)
is_static_or_uutils() {
  bin="$1"
  if [ ! -x "$bin" ] && ! command -v "$bin" >/dev/null 2>&1; then
    return 1
  fi
  if file -L "$bin" 2>/dev/null | grep -E 'statically linked|cargo' >/dev/null 2>&1; then
    return 0
  fi
  if "$bin" --version 2>/dev/null | grep -qiE 'uutils|rust'; then
    return 0
  fi
  if "$bin" -V 2>/dev/null | grep -qiE 'uutils|rust'; then
    return 0
  fi
  return 1
}

if is_static_or_uutils /bin/cat; then
  STATIC_CAT=1
fi
if is_static_or_uutils /bin/dd; then
  STATIC_DD=1
fi
if is_static_or_uutils /bin/head || is_static_or_uutils "$(command -v head)"; then
  STATIC_HEAD=1
fi
if tail -n 1 /dev/null >/dev/null 2>&1; then
  TAILN="tail -n "
  TAILP="tail -n +"
else
  TAILN="tail -"
  TAILP="tail +"
fi

FAILED=0
TESTED=0

if [ -z "$1" ]; then
  seed=$(date | $ZZUF -m 2>/dev/null | sed 's/[^ ]* [abcdef0]*//' | tr -d abcdef | cut -b1-8)
else
  seed="$1"
fi

start_test() {
    echo ""
    echo "*** running $1 with seed $seed ***"
}

new_test() {
    echo "*** $1 *** "
}

pass_test() {
    TESTED="$(($TESTED + 1))"
    echo "$1"
}

fail_test() {
    TESTED="$(($TESTED + 1))"
    FAILED="$(($FAILED + 1))"
    echo "$1"
}

stop_test() {
    if [ "$FAILED" != 0 ]; then
        echo "*** $FAILED tests failed out of $TESTED ***"
        exit 1
    fi
    echo "*** all $TESTED tests OK ***"
}

