#!/bin/sh

result=0
count=0
fail=0
skip=0

result_path=$(pwd)
cd $(dirname "$0")
script_path=$(pwd)
cd "${result_path}"
result_path="${result_path}/logs"

if [ -n "$(command -v ncat)" ]
  then # ncat
    mynetcat="ncat"
  elif [ -n "$(command -v nc)" ]
    then # nc
      mynetcat="nc"
  else # # netcat not found
    mynetcat="null"
  fi
if [ "$mynetcat" != "null" ] # netcat is required
  then
    rm -rf "${result_path}"
    mkdir "${result_path}"
    cd "${result_path}"
    date > "results.log"
    ../../src/stunnel -version 2>> "results.log"
    printf "\n%s\n" "Testing..." >> "results.log"
    head -n5 "results.log"
    for plik in ${script_path}/recipes/*
      do
        /bin/sh $plik $mynetcat
        state=$?
        if [ "$state" -eq 0 ]
          then # $state=0
            count=$((count + 1))
          elif [ "$state" -eq 125 ]
            then # $state=125
              skip=$((skip + 1))
          else # $state=1
            fail=$((fail + 1))
            result=1
          fi
      done
    if [ $count -eq 0 ]
      then # no test was done
        result=1
      fi
    printf "%s\n" "summary: success $count, skip $skip, fail $fail"
    printf "%s\n" "summary: success $count, skip $skip, fail $fail" >> "results.log"
    printf "%s\n" "./make_test finished"
    cd ..
  else # netcat not found
    printf "%s\n" "./make_test skipped: ncat (nc) not found in \$PATH"
    #result=125
  fi
exit $result
