#! /bin/bash 
# Last edited on 2013-09-05 22:00:52 by stolfilocal

USAGE="${cmd} aula|casa"

# Finds all "main-orig.pov" files under current directory;
# writes the list to ".${which}-stulabs-present".
# 
# Then reads the files "rausrs.txt" and "labs-info.txt" in the current directory
# and checks whether any of the {TPNAME}s given are missing from any student, 
# and whether the dates of the 'main-orig' files are correct.  Writes the 
# missing file/date pairs to ".${which}-stulabs-missing" and unexpected file/date pairs
# to ".${which}-stulabs-unexpected".
# 
# Also writes a more compact summary to ".${which}-summary"

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

which="$1"; shift;

# Grab the names and dates of the labs:
tpnames=( )
tpdates=( )
for name in `cat labs-info.txt | gawk '/^[ ]*([\#]|$)/{ next; } (NF>=2){ print $1; }'` ; do
  tpnames=( "${tpnames[@]}" "${name}" );
done
i=0
for date in `cat labs-info.txt | gawk '/^[ ]*([\#]|$)/{ next; } (NF>=2){ print $2; }'` ; do
  tpdates=( "${tpdates[@]}" "${date}" );
done
for i in `count 0 $(( ${#tpdates[@]} - 1 ))` ; do
  echo ${tpnames[$i]} ${tpdates[i]} 1>&2
done

# Enumerate all students, create ".${which}-stulabs-expected" (expected main-orig.pov files) 
# and ".${which}-summary" (map of which of them are present, with date flags):
/bin/rm -f .tmp-expected
/bin/rm -f .${which}-summary

# ${Which}-Summary header:
echo "# created on `yyyy-mm-dd-hhmmss` by list-missing-labs" >> .${which}-summary
echo "# Last edited on DATE TIME by USER" >> .${which}-summary
echo "# " >> .${which}-summary
printf "#       " >> .${which}-summary
for tp in "${tpnames[@]}" ; do
  printf ' %s*' "${tp}"  >> .${which}-summary
done
printf "\n" >> .${which}-summary
printf "#       " >> .${which}-summary
for tp in "${tpnames[@]}" ; do
  printf ' %s-' "`echo ${tp} | tr '.a-z0-9' '.-'`"  >> .${which}-summary
done
printf "\n" >> .${which}-summary
printf "# Num RA" >> .${which}-summary
for td in "${tpdates[@]}" ; do
  printf ' %s' "`echo ${td} | sed -e 's:^20[0-9][0-9]-::g'`"  >> .${which}-summary
done
printf "\n" >> .${which}-summary
printf "# ------" >> .${which}-summary
for td in  "${tpdates[@]}"; do
  printf ' %s' "`echo ${td} | sed -e 's:^20[0-9][0-9]-::g' | tr '.a-z0-9' '.-'`"  >> .${which}-summary
done
printf "\n" >> .${which}-summary

for ra in `cat rausrs.txt | gawk '//{ print $2; }' | sort` ; do
  echo ${ra}
  printf "| %s" ${ra} >> .${which}-summary
  for i in `count 0 $(( ${#tpnames[@]} - 1))` ; do
    tp="${tpnames[$i]}"
    td="${tpdates[$i]}"
    # Get a blank string with the same width as ${tp}:
    bk="`echo ${tp} | tr 'a-z0-9' '.'`"
    fno="${tp}/${which}/${ra}/main-orig.pov"
    fnn="${tp}/${which}/${ra}/main.pov"
    printf "%s %s\n" "${fno}" "${td}" >> .tmp-expected
    # Check for presence of "main-orig.pov":
    if [[ -s ${fno} ]]; then
      printf " %s" ${tp} >> .${which}-summary
      # Get actual date {ad} of "main-orig.pov":
      ad=`find ${fno} -printf '%TY-%Tm-%Td\n'`
      if [[ "/${ad}" != "/${td}" ]]; then flag='*'; else flag=' '; fi
      printf "%s" "${flag}" >> .${which}-summary
      # Check for "main.pov": 
      if [[ ! -s ${fnn} ]]; then
        echo "** ${fno} present but ${fnn} missing" 1>&2
      fi
    else
      printf " %s." ${bk} >> .${which}-summary
      if [[ -s ${fnn} ]]; then
        echo "** ${fno} missing but ${fnn} present" 1>&2
      fi
    fi
  done
  printf "\n" >> .${which}-summary
done
sort .tmp-expected > .${which}-stulabs-expected 

# Get list ".${which}-stulabs-present" of all "main-orig.pov" files and their dates:

find ./*/${which}/ -name 'main-orig.pov' \
    -printf '%p %TY-%Tm-%Td\n' \
  | egrep -v -e 'SAVE' \
  | sed -e 's:^[.][\/]::' \
  | sort \
  > .${which}-stulabs-present

# List excess pairs:

echo "=== bogus main-orig.pov files ==="
bool 1-2 .${which}-stulabs-present .${which}-stulabs-expected > .${which}-stulabs-unexpected
join -j1 1 -j2 1 -a1 -e ??? -o0,1.2,2.2 .${which}-stulabs-unexpected .${which}-stulabs-expected \
  | gawk '//{ print $1, $2, "should be", $3 }'

# List missing pairs:

echo "=== missing main-orig.pov files ==="
bool 1-2 .${which}-stulabs-expected .${which}-stulabs-present > .${which}-stulabs-missing
cat .${which}-stulabs-missing
echo " "

# print map of all homeworks:

cat .${which}-summary

echo "=== done ==="