#!/usr/bin/env bash

# Aggregate student ratings of one another, e.g., for COMP 250 presentations.
# Run from hwsubmit/core directory.
# For now files assumed to come from courseFeedback.cgi or at least have a - in basename and extension .txt
# Group numbers of students to root out self-evaluation in file named "groupnum".

numgroups=$1
numcriteria=$2

pwd=`pwd -P`
submitdir=${pwd%/*}
coursedir=${submitdir%/*}
course=${coursedir##*/}
del .. 'group*'
for ratingfile in */${course}presentSurveys/*-*.txt; do
  groupnum=`cat ${ratingfile%%/*}/groupnum.txt`
  sed "s+.*'ratings': \[\(.*\)\]}+\1+" $ratingfile | tr -d "'" | \
    awk -F, '{for (i=1;i<='$numgroups';i++) {if (i == '$groupnum') continue; filename="../group"i; for (j=0;j<'$numcriteria';j++) {ratepos='j*$numgroups+i'; printf("%d ",$ratepos) >>filename};; print "" >>filename}}'
done
tempfile=../presenttemp.txt
sumfile=../presentsum.txt
for aggregate in ../group*; do
  cp $aggregate $tempfile
  cat $tempfile | total '$1' 1 '$2' 2 '$3' 3 '$4' 4 '$5' 5 '$6' 6 '$7' 7 '$8' 8 '$9' 9 '$10' 10 '$11' 11 '$12' 12 '$13' 13 '$14' 14 '$15' 15 '$16' 16 '$17' 17 '$18' 18 '$19' 19 '$20' 21 '$22' 22 '$23' 23 '$24' 24 '$25' 25 | \grep Avg | total '$1' | \grep Total >> $aggregate
  echo $aggregate >> $sumfile
  cat $aggregate >> $sumfile
done
rm -f $tempfile
