#!/bin/sh
# vim: set filetype=sh :
#        file: run_tests
#   copyright: Bernd Schumacher <bernd.schumacher@hpe.com> (2007-2020)
#     license: GNU General Public License, version 3
# description: run shellia internal tests

usage()
{
  cat <<END >&2
Usage: run_tests [-v] [-t <testfile>]
  -v        be verbose
  -t <file> Instead of using all testfiles use only <file>
            Example:
            ./run_tests -t tests/test.hello_world -t tests/test.sshfd
END
  exit 1
}

TESTFILES=""
VERBOSE=""
while [ $# -gt 0 ]; do
  if [ "$1" = "-v" ]; then
    VERBOSE="$1"
    shift
  elif [ "$1" = "-t" ]; then
    TESTFILES="$TESTFILES $2"
    shift 2
  else
    echo "ERROR run_tests: unknown option <$1>" >&2
    usage
  fi
done

# show_indented_short_error
short_err()
{
  echo "/ shortened difference between expectation and result:"
  diff exp res | head -10 | sed "s/^/   /"

  # rotate old dbgerr files
  for n in $(seq 9 -1 1); do
    [ -f dbgerr/$i ] && mv dbgerr/$i dbgerr/$((n+1))
  done
  # save current dbgerr files
  for i in res exp r4s res.cmd res.org; do
    mkdir -p dbgerr/1
    cp $i dbgerr/1
  done
}

check_shellia_tmpfiles()
{
  local tmpn

  if [ "$1" = "--rm" ]; then
    rm -f timelist /tmp/shellia.??????????
  fi

  tmpn="$(ls /tmp/shellia.?????????? 2>/dev/null | wc -l)"
  if [ $tmpn -ne 0 ]; then
    echo "ERROR found $tmpn /tmp/shellia.* files" >&2
    exit 1
  fi
}

# test_with_sh <testfile> <sh>
test_with_sh()
{
  local out errmsg
  out="$("./$1" -s "$2" 2>&1)"
  if [ "$(echo "$out" | grep -e "^ERROR" | grep -v -e "^ERROR: ia premature exit")" ]; then
    errmsg="ERROR$(echo "$out"| sed -E -n -e "s/^ERROR\s+(\([0-9]+\)).*/ \1/p")"
    [ "$VERBOSE" ] && echo "$out" || echo "$errmsg \"./$1\" -s \"$2\" $(short_err)"
    err=$((err +1))
  elif [ "$(echo "$out" | grep -e "^WARN")" ]; then
    [ "$VERBOSE" ] && echo "$out" || echo "WARN \"./$1\" -s \"$2\" $(echo "$out" | sed -n "s/^WARN .* => /=> /p" | sort -u)"
  else
    [ "$VERBOSE" ] && echo "$out" || echo "OK \"./$1\" -s \"$2\""
  fi
}

if ./sshd2222 should-work; then
  ./sshd2222 start || exit $?
fi

rm -f timelist

[ "$TESTFILES" ] || TESTFILES="$(/bin/ls tests/test.*)"

err=0
check_shellia_tmpfiles --rm
for testfile in $TESTFILES; do
  testdir="$(dirname $testfile)"
  testfile="$(basename $testfile)"
  if [ "$testfile" = "test.prefix" ]; then
    # dash can not list functions, but bash can
    for sh in bash; do
      test_with_sh "$testdir/$testfile" "$sh"
      check_shellia_tmpfiles
    done
  elif [ "$testfile" = "test.ia_mktemp" -o "$testfile" = "test.ia_mktemp2" ]; then
    # posh Bug#910275
    for sh in mksh bash dash "busybox sh"; do
      test_with_sh "$testdir/$testfile" "$sh"
      check_shellia_tmpfiles
    done
  elif [ "$testfile" = "test.multiply" ]; then
    for sh in bash dash "busybox sh" mksh; do
      test_with_sh "$testdir/$testfile" "$sh"
      check_shellia_tmpfiles
    done
    # posh Bug#910275
    for sh in posh; do
      test_with_sh "$testdir/$testfile" "$sh"
      check_shellia_tmpfiles --rm
    done
  else
    #for sh in bash dash "busybox sh" mksh posh yash zsh sash; do
    for sh in bash dash "busybox sh" mksh posh; do
      test_with_sh "$testdir/$testfile" "$sh"
      check_shellia_tmpfiles
    done
  fi
done
check_shellia_tmpfiles

s="$(
( echo "( "
  cat timelist | sed "s/^real=\([0-9]\+\):.*/\1+/"
  echo "0 ) * 3600"

  echo "+"

  echo "( "
  cat timelist | sed "s/^real=[0-9]\+:\([0-9]\+\)\..*/\1+/"
  echo "0 ) * 60"

  echo "+"

  cat timelist | sed "s/^real=[0-9]\+:[0-9]\+\.\([0-9]\+\).*/\1+/"
  echo 0
) | xargs | bc)"
#echo "s=$s"
h=$((s/3600))
s=$((s-h*3600))
m=$((s/60))
s=$((s-m*60))
date "+%Y/%m/%d-%H:%M $h:$m.$s err=$err times=$(cat timelist | wc -l)" >> timelist.all

if ./sshd2222 should-work; then
  ./sshd2222 stop
fi

echo "$err Errors"
exit $err
