#! /bin/bash
#------------------------------------------------------------------------------
#$Author: saulius $
#$Date: 2018-12-20 17:58:57 +0200 (Thu, 20 Dec 2018) $
#$Revision: 5322 $
#$URL: svn+ssh://saulius@saulius-grazulis.lt/home/saulius/svn-repositories/paskaitos/VU/software/trunk/assignment-evaluation/scripts/merge-moodle-grading-worksheet $
#------------------------------------------------------------------------------
#*

# Merge student evaluation directory names with the Moodle grading
# worksheet (CSV format) student names..

# The grades (the first column) and student's name is separated by a
# single TAB character (ASCII 09). The sutdent's name is followed by a
# ' --' sequence (space + two hyphens). This sequence, if present, and
# everything that follows it is ignored.

# Input format of the 'grades*.csv' file:

## Identifier,"Full name","Email address",Status,Grade,"Maximum Grade","Grade can be changed","Last modified (submission)","Last modified (grade)"
## "Participant 256","Saulius Gražulis",sg@domain.lt,"Submitted for grading - Extension granted until: Tuesday, 20 March 2018, 12:00 PM",,100.00,Yes,"Tuesday, 20 March 2018, 11:29 AM","Tuesday, 20 March 2018, 11:29 AM"
## "Participant 268","Vardauskas Studentauskas",vs@domain.com,"Submitted for grading - Extension granted until: Tuesday, 20 March 2018, 12:00 PM",,100.00,Yes,"Tuesday, 20 March 2018, 11:39 AM","Tuesday, 20 March 2018, 11:39 AM"

# The first line contains column names and will be ignored.
#
# Student name is taken from the 2-nd comma-separated column and will be used
# for joining the two files.
#
# Grade (the first number in the 'evaluation.lst' file) will be inserted into
# the 5-th column of the work-sheet.

# Students' directories must be named after students' names and surnames, e.g.:

# Vardauskas_Pavardauskas/

#**

set -ue
## set -x

script() { echo "# $*"; cat; }
setvar() { eval $1="'$3'"; }

setvar Id = '$Id: merge-moodle-grading-worksheet 5322 2018-12-20 15:58:57Z saulius $'

setvar FILES = ""

setvar BASENAME = "$(basename "$0")"

setvar SCRIPT_DIR = "$(dirname "$0")"

#** USAGE:
#**   $0 --options grading/grades.csv ../01-darbas/
#**
#** OPTIONS:
#**  --help                   print short help message (this message) and exit
while [ $# -gt 0 ]
do
    case $1 in
        --version|--versio|--versi|--vers|--ver|--ve|--v)
            echo $Id
            exit
            ;;
        --help|--hel|--he|--h)
            awk '/#\*/,/#\*\*/ {
                    sub("^ *#[*]?[*]?", ""); \
                    gsub("\\$0","'$0'"); \
                    print $0
                }' $0
	    exit
	    ;;
        --options|--option)
            echo "$0: '--options' is a place-holder; " \
                 "please use '$0 --help' to get the list of available options."
            exit 2
            ;;
        -*) echo "$0: unknown option '$1'" >&2 ; exit 1 ;;
        *)  FILES="$FILES '$1'" ;;
    esac
    shift
done

## echo ${FILES}
eval set -- "${FILES}"

GRADE_BOOK="$1"
EVAL_DIR="$2"

## set -x

shopt -s globasciiranges

tail -n +2 ${GRADE_BOOK} \
    | awk -F, '{print $2}' \
    | sed 's/"//g' \
    | ${SCRIPT_DIR}/latinise-names \
    | paste - <(tail -n +2 ${GRADE_BOOK} | awk -F, '{print $1}') \
    | sort -t $'\t' -k 1,1 \
    | join -t $'\t' \
           <(ls -1 ${EVAL_DIR} \
                    | ${SCRIPT_DIR}/latinise-names \
                    | sort -t $'\t' -k 1,1 \
            ) \
           - \
    | awk -F"\t" '{print $2"\t"$3"\t"$4}'
