#! /bin/bash

# To be executed in a "${aula}/put" directory (downloaded at end of
# class, with trunk and un-merged branches,

aula="$1"; shift   # Date of class, "{YYYY}-{MM}-{DD}".
ra="$1"; shift;    # Student's ID.

if [[ "/${aula}" == "/" ]]; then echo "{aula} not specified" 1>&2 ; exit 1; fi

mc857_check_current_dir_2.sh "${aula}/put" || exit 1
 
repdir="${HOME}/cursos/mc857/2020-1/notas/ra"
if [[ ! ( -d ${repdir} ) ]]; then
  echo "** directory ${repdir} does not exist or is not a directory" 1>&2
  exit 1
fi
 
tlist="${repdir}/${ra}/${aula}/modules-to-test.txt"
if [[ ! ( -r ${tlist} ) ]]; then
  echo "** file ${tlist} does not exist or is not readable" 1>&2
  exit 1
fi

tmp="/tmp/$$"
  
# Report files:
ofile="${repdir}/${ra}/${aula}/test-outputs.txt" # Concatenation of test outputs.
blist="${repdir}/${ra}/${aula}/failed-tests.txt" # List of tests that had syntax or run-time faults.
hlist="${repdir}/${ra}/${aula}/testes-saidas-dir.txt" # List of HTML files generated by test.
hodir="${repdir}/${ra}/${aula}/testes-saidas"   # Directory containing HTML files generated by test.
imdir="${hodir}/imagens"  # Directory containing images used by the tests.
rm -fv ${ofile} ${blist} ${hlist}
rm -frv ${hodir} ${imdir}

touch ${ofile} ${blist} ${hlist}
mkdir -pv ${hodir} ${imdir}

bra=branches/${ra}
pushd ${bra}

  # Copy the images that may be needed by the HTML output files:
  cp -av ../../trunk/imagens/*.{png,jpg} ${imdir}/
  cp -av imagens/*.{png,jpg} ${imdir}/

  for mod in `cat ${tlist}`; do 

    echo "testing module ${mod} ..." 1>&2
    ok=1

    # Try to find the test program:
    prog=${mod}_TST.py
    if [[ -s testes/${prog} ]]; then
      dir="testes"
    elif [[ -s ../../trunk/testes/${prog} ]]; then
      dir="../../trunk/testes"
    else
      echo "can't find ${prog}" 1>&2
      dir="NONE"
    fi

    # Execute the program, send the output and errors to ${tmp}.testout:
    if [[ -d ${dir} ]]; then

      # Define the python search path:
      PYPATH_STD="/usr/lib/python3.6/site-packages/sos/plugins"
      PYPATH_MAIN=".:..:../../trunk:../../../trunk" # The top-level branch or trunk dir.
      PYPATH_TEST=".:testes:../../trunk/testes:../../../trunk/testes"  # The "testes" branch subdir.
      export PYTHONPATH="${PYPATH_MAIN}:${PYPATH_TEST}:${PYPATH_STD}"

      # Make sure that the required directories exist:
      rm -rfv testes/saida
      mkdir -p testes/saida
      rm -rfv DB
      mkdir DB

      # Run the program in the branch subdir:
      python3 ${dir}/${prog} > ${tmp}.testout 2>&1

      # List all HTML output files and move them to the ${hodir} directory:
      ( cd testes/saida/ && find ./ -type f -name '*.html' -print | sed -e 's:^[.]/::g' ) > ${tmp}.hlist
      cat ${tmp}.hlist >> ${hlist}
      if [[ -s ${tmp}.hlist ]]; then
        mv -vi testes/saida/*.html ${hodir}/
      fi

      # Check whether the test succeeded:

      if [[ ( ! ( -s ${tmp}.testout ) ) && ( ! ( -s ${hlist} ) ) ]]; then
        # Test program produced no output:
        echo "** test program produced no output" 1>&2
        echo "** TESTE NÃO PRODUZIU SAÍDA NENHUMA" > ${tmp}.testout
        ok=0
      elif grep -q -s -e 'File ' ${tmp}.testout ; then
        # Test program seems to have failed with error:
        echo "!! test output seems to contain errors" 1>&2
        grep -m 1 -A 2 -e 'File ' ${tmp}.testout | sed -e 's:^:  ! :g' 1>&2
        ok=0
      else
        # Test program seems to have done OK:
        echo "!! test output seems OK" 1>&2
      fi

    else
      # Test program not found -- fake the test output:
      echo "** test program is missing" 1>&2
      echo "** PROGRAMA DE TESTE NÃO ENCONTRADO" > ${tmp}.testout
      ok=0
    fi

    if [[ ${ok} -ne 1 ]]; then
      # One more test failed:
      echo "** test failed" 1>&2
      echo "=== testando ${prog} =============================" >> ${ofile}
      cat ${tmp}.testout >> ${ofile}
      echo "=== fim do teste de ${prog} ======================" >> ${ofile}
      echo "" >> ${ofile}
      echo "${prog}" >> ${blist}
    fi
  done
  
  rm -f ${tmp}.*

popd


