#! /usr/bin/env bash

# Extract column headed by $2 from file $1 and produce a histogram on
# $2-hist.txt.  It is assumed that all scores are integral; in most
# cases, nonintegral scores will be ignored.

# Each student's score is shown with the corresponding symbol for that
# student under CLS if the CLS column is present (default "*").

# An example usage is "gradehist grades.txt EX1".

if [[ $# -lt 2 ]]; then
  echo "Usage: gradehist gradesfile colname"
  exit 1
fi

errcheckfile=/tmp/gradehist.$USER.$$

cat $1 | stripgradestats | excol "$2" CLS | tee $errcheckfile | hist > $2-hist.txt
if (( ! `cat $errcheckfile | wc | sed s':\([0-9][0-9]*[ 	]*\)\([0-9][0-9]*[ 	]*\)[0-9][0-9]*:expr \2= \1+ \1:' | /bin/bash` )); then
   echo gradehist: Error: at least one score or CLS value missing
   rm $2-hist.txt
else
checkhist $2-hist.txt
fi

rm $errcheckfile

