#! /bin/bash
# Last edited on 2009-07-23 07:29:24 by stolfilocal

PROG_NAME=${0##*/}
PROG_DESC="Creates initial grade files for students"
PROG_HELP=(
  "${PROG_NAME} {LABDIR}.."
)
PROG_INFO=(
  "\nNAME"
  "\n  ${PROG_NAME} - ${PROG_DESC}."
  "\n"
  "\nSYNOPSIS"
  "\n  ${PROG_HELP[@]}"
  "\n"
  "\nDESCRIPTION"
  "\n"
  "Copies initial grade files for all students and the specified labs.\n"
  "\n"
  "The grade files are "{LABDIR}/aula/{RA}/nota.txt" and "{LABDIR}/casa/{RA}/nota.txt".\n"
  "\n"
  "Assumes that there are files "aus.txt" and "rausrs.txt" in the current directory.\n"
)

PATH="tools:../tools:../../tools:../../../tools:../../../../tools:${PATH}"

bogusop=0;
while [[ ( $# -ge 1 ) && ( "/$1" =~ "/-.*" ) ]]; do
  if [[ ( $# -ge 1 ) && ( "/$1" == "/-bogus" ) ]]; then
    bogusop=1; shift; 
  else
    echo "usage: ${PROG_HELP}" 1>&2; exit 1;
  fi
done

if [[ $# -lt 1 ]]; then
  echo "no labs?" 1>&2;
  echo "usage ${PROG_HELP}" 1>&2; exit 1
fi

labs=( "$@" )

for tp in "${labs[@]}" ; do 
  echo "=== ${tp} ==="
  pushd ${tp}
  # Create the default grade files for absentees:
  for ra in `cat aus.txt` ; do
    # Grade for in-class 'aula' work:
    aufile="aula/${ra}/nota.txt"
    if [[ -r ${aufile} ]]; then
      # Reject any 'aula' grade if absent:
      printf "file ${aufile} already exists = «" 1>&2
      cat ${aufile} | tr '\012' '¦' 1>&2
      printf "», replacing\n" 1>&2
      echo "000 (ausente)" > ${aufile}
    else
      echo "file ${aufile} does not exist, creating it" 1>&2
      echo "000 (ausente)" > ${aufile}
    fi

    # Grade for at-home 'casa' work:
    cafile="casa/${ra}/nota.txt"
    if [[ ! -r ${cafile} ]]; then
      # May accept casa grade even if ausente:
      echo "file ${cafile} already exists = «" 1>&2
      cat ${aufile} | tr '\012' '¦' 1>&2
      printf "»\n" 1>&2
    else
      echo "file ${cafile} does not exist, creating it" 1>&2
      echo "000 (ausente)" > ${cafile}
    fi
  done

  # Create a dummy grade files for other students:
  for ra in `cat rausrs.txt | gawk '/[0-9]/{ print $2; }'` ; do
    aufile="aula/${ra}/nota.txt"
    cafile="casa/${ra}/nota.txt"
    if [[ -r ${aufile} ]]; then
      echo "file ${aufile} already exists" 1>&2
    else
      echo "file ${aufile} does not exist, creating it" 1>&2
      echo "??? ()" > ${aufile}
    fi
    if [[ -r ${cafile} ]]; then
      echo "file ${cafile} already exists" 1>&2
    else
      echo "file ${cafile} does not exist, creating it" 1>&2
      echo "??? ()" > ${cafile}
    fi
  done
  popd
done