#!/bin/sh

#*
# Print evaluations of the students' assignemnts based on the
# evaluation points in the "comments.txt" files of each student's
# assignment.
#
# Usage:
#    $0
#    $0 */comments.txt
#    $0 -m 85
#    $0 --max 85 */comments.txt
#
# If no arguments are provided, all "comments.txt" files in
# subdirectories of the current directory are avaluated.
#**

#** OPTIONS:
#**   -m, --max 85      Maximum score (in points) for this assignment
#**   --help            Print a short help message and exit

evaluate="$(dirname $0)/evaluate-assignment"

for program in $evaluate
do
    if [ ! -x $program ]; then
        echo "$0: need program '$program' located in the same directory as $0" >&2
        exit 1
    fi
done

set -ue

MAX_SCORE=85
FILES=""

while [ $# -gt 0 ]
do
  case $1 in
      -m|--max|--ma|--m)
            MAX_SCORE="$2"
            shift
            ;;
      --help|--hel|--he|--h)
            awk '/#\*/,/#\*\*/{sub("^ *#[*]?[*]?", ""); print $0}' $0
            exit
            ;;
      -*) echo "`basename $0`: unknown option $1" >&2 ; exit 1 ;;
      *)  FILES="$FILES '$1'" ;;
    esac
    shift
done

eval set -- "${FILES}"

if [ $# -eq 0 ]
then
    set */comments.txt
fi

for COMMENT
do
    echo -n $(expr $MAX_SCORE + $($evaluate "$COMMENT"))"\t"
    echo -n $(basename "$(cd "$(dirname "$COMMENT")"; pwd)")" -- "
    if [ $($evaluate "$COMMENT") -eq 0 ]
    then
        echo -n galutinis įvertinimas." "
    else
        echo -n preliminarus įvertinimas." "
    fi
    echo -n Komentarai prisegtame faile "\"$(basename "$COMMENT")\"".
    echo ""
done
