#! /bin/bash

# To be executed in a "${aula}/put" directory (checkout of project just
# after class, with trunk (master branch) as handed out and all student
# branches still un-merged.

aula="$1"; shift # Date of class, "{YYYY}-{MM}-{DD}"
if [[ "/${aula}" == "/" ]]; then echo "{aula} not specified" 1>&2 ; exit 1; fi

ra="$1"; shift # Date of class, "{YYYY}-{MM}-{DD}"
if [[ "/${ra}" == "/" ]]; then echo "{ra} not specified" 1>&2 ; exit 1; fi

repdir=${HOME}/cursos/mc857/2020-1/notas/ra # Directory of lab reports
if [[ ! ( -d ${repdir} ) ]]; then
  echo "** directory ${repdir} does not exist or is not a directory" 1>&2
  exit 1
fi

# Get student IDs:
rasfile="${HOME}/cursos/mc857/2020-1/listas/ras.txt"
ras=( `cat ${rasfile} | egrep -e '^[0-9][0-9][0-9][0-9][0-9][0-9]([ #]|$)' | sort` )

mc857_check_current_dir_2.sh "${aula}/put" || exit 1

bra="branches/${ra}"

echo "=== comparing ${bra} to trunk ===" 1>&2

# List of significant changed files:
ulist="${repdir}/${ra}/${aula}/changed-files.txt"

# Changes in uploaded source files:
dfile="${repdir}/${ra}/${aula}/changes.txt"

rm -fv ${ufile} ${dfile}

if [[ -d ${bra} ]]; then

  # Enumerate significant uploaded files:
  ( cd ${bra} && \
    find ./ -type f -print \
      | egrep -v -e 'README|LICENSE|Makefile|testa[.]sh|[.]html|.gitignore|[.]sql|pycache' \
      | sed -e 's:^[.]/::g' \
      | sort \
  ) > ${ulist}

  # Run diff on uploaded source files:
  touch ${dfile}
  for file in `cat ${ulist} | egrep -e '[.]py$' ` ; do 
    if [[ -r trunk/${file} ]]; then
      echo "=== alteracoes em ${file} ===" >> ${dfile}
      echo "" >>${dfile}
      prdiff -Bb trunk/${file} ${bra}/${file} >> ${dfile}
    else
      echo "=== novo arquivo ${file} ===" >> ${dfile}
      echo "" >> ${dfile}
      cat ${bra}/${file} | sed -e 's:^:  > :g' >> ${dfile}
    fi
    echo "" >> ${dfile}
  done
else
  echo "** ${bra} missing or not a directory" 1>&2
  echo "** BRANCH MISSING **" >  ${dfile}
  touch ${ulist}
  touch ${dfile}
fi
echo "significant changed files:" 1>&2
cat ${ulist} 1>&2
echo "differences found:" 1>&2
wc -l ${dfile} 1>&2


