#! /bin/bash

# To be executed in a "${aula}/put/branches/${ra}" directory
# containing only the ".py" files created or changed by each student.

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
if [[ "/${ra}" == "/" ]]; then echo "{ra} not specified" 1>&2 ; exit 1; fi
  
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

tmp="/tmp/$$"

# Determine if student was absent:
vfile="${repdir}/${ra}/${aula}/avaliacao.txt"
if [[ -s ${vfile} ]]; then
  # Check whether student was present:
  aus="`grep -e 'AUSENTE' ${vfile} | wc -l`"
else
  # Cannot tell if present, assume he is:
  aus=0
fi

if [[ ${aus} -gt 0 ]]; then
  echo "!! ABSENTEE - skipped."
  exit 0
fi

# Check if required files exists:
klist="${repdir}/${ra}/${aula}/task-modules.txt"  # List of MODULES that student should have edited.
if [[ ! ( -r ${klist} )]]; then
  echo "** file ${klist} is missing or unreadable" 1>&2
  exit 1
fi
echo "modules that should have been edited by ${ra}:" 1>&2
cat ${klist} 1>&2

clist="${repdir}/${ra}/${aula}/changed-files.txt"  # List of FILES actually edited by student.
if [[ ! ( -r ${clist} )]]; then
  echo "** file ${clist} is missing or unreadable" 1>&2
  exit 1
fi
echo "files actually edited by ${ra}:" 1>&2
cat ${clist} 1>&2

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

  mlist="${repdir}/${ra}/${aula}/changed-modules.txt" # List of changed modules.
  alist="${repdir}/${ra}/${aula}/affected-sources.txt"  # List of directly affected ".py" files.
  tlist="${repdir}/${ra}/${aula}/modules-to-test.txt" # List of modules to be tested.
  rm -fv ${mlist} ${alist} ${tlist}

  echo "finding changed modules (including tests programs)..."  1>&2
  cat ${clist} \
    | egrep -e '[.]py$' \
    | sed \
        -e 's:^testes/::g' \
        -e 's:[.]py$::g' \
        -e 's:_IMP::g' \
        -e 's:_TST::g' \
        -e '/^servidor/d' \
        -e '/^processa_comando_http/d' \
    | sort | uniq \
    > ${mlist}
  cat ${mlist} 1>&2

  if [[ -s ${mlist} ]]; then

    echo "identifying source files directly affected by the changes..."  1>&2
    cat ${mlist} \
      | sed -e 's:^:\\b:g' -e 's:$:[;.]:g' \
      > ${tmp}.pats
    cat ${mlist} \
      | sed -e 's:^:modulo *= *:g' -e 's:$:\\b:g' \
      >> ${tmp}.pats
    # echo "pattern file:"
    # cat ${tmp}.pats 1>&2
    grep \
        --extended-regexp \
        --file ${tmp}.pats \
        --files-with-matches \
        {,../../trunk/}{,testes/}*.py \
      > ${alist}
    cat ${alist} 1>&2
  else
    echo "no modules were changed" 1>&2
    touch ${alist}
  fi

  echo "choosing modules to test ..."  1>&2
  # Modules that student should have edited
  rm -f ${tmp}.kmod
  touch ${tmp}.kmod
  for m in `cat ${klist}` ; do
    if [[ ! ( ( -s ${m}.py ) || ( -s ../trunk/${m}.py ) ) ]]; then
      echo "!! warning: module ${m} in ${klist} does not exist"
    fi
    echo "${m}" >> ${tmp}.kmod
  done

  # Test files that were actually created or modified by student:
  cat ${clist} \
    | egrep -e '_TST[.]py$' \
    > ${tmp}.tmod

  # Files that were directly affected by the changes:
  cat ${clist} ${alist} \
    | egrep -e '[.]py$' \
    > ${tmp}.amod

  cat ${tmp}.kmod ${tmp}.tmod ${tmp}.amod \
    | sed \
        -e 's:^.*trunk/::g' \
        -e 's:^testes/::g' \
        -e 's:[.]py$::g' \
        -e 's:[.][a-zA-Z0-9_]+$::g' \
        -e 's:_IMP::g' \
        -e 's:_TST::g' \
        -e '/^servidor/d' \
        -e '/^processa_comando_http/d' \
    | sort | uniq > ${tlist}

  cat ${tlist} 1>&2
  
  rm -f ${tmp}.*

popd
