#!/bin/csh -f

# Put letter grades into column headed by $3 in file $1.sc based on
# numeric scores in column headed by $2.  Requires corresponding up to
# date version of $1.txt.  (Scores may be recorded only as a
# formula in $1.sc.)  This script will make $1.txt before the main body
# of the script.  Also remakes it at the end to keep it consistent with
# $1.sc.  (See the shell script sc2txt.)  Any extension given in $1
# is ignored.

# Remaining arguments provide score-grade pairs (i.e., $4 is a score
# and $5 a corresponding grade, and so on) and must be in ascending
# order of score.  Each score is the score *above* which the
# corresponding grade is to be assigned.  Empty slots under the $2
# column are treated like 0.  They can be kept empty by keeping all
# scores arguments nonnegative or assigned a grade by using a negative
# value as the first score argument.

# Depends on column headings being rightstrings.

# A typical usage would be to create an executable file in the
# directory containing grades.sc and grades.txt such as the following
# (with the "# " at the beginning of each line removed).  (Note the
# blank line at the end, or leave out the backslash at the end of the
# last line.)

# #!/bin/csh -f
# 
# letsinsc grades \
# SEM LET \
# 0 F- \
# 30 F \
# 43 D \
# 51 D+ \
# 60 C \
# 66 C+ \
# 75 B \
# 82 B+ \
# 88 A \
# 

# If you don't want to mess with my bin/versctrl/del command, you can
# comment out the two indicated lines near the end of this file, but
# be forewarned that then letsinsc will destructively modify
# $1.sc, and it comes with no warranties.

set tempfile = /tmp/letsinsc.$USER.$$

if ($#argv<3) then
  echo "Usage: letsinsc fileroot srccolname dstcolname [score grade ...]"
  exit 1
endif

set fileroot = $1
shift

make $fileroot.txt

set col = `sed -n 's+^rightstring \(.\)[0-9]* = "'$2'"+\1+p' $fileroot.sc`
if ($col == "") then
  echo "letsinsc: Couldn't find column heading $2"
  exit 2
endif

grep -v '^rightstring '$col'[^0][0-9]* = ' $fileroot.sc > $tempfile
set gvres = $status

set srccolname = $1
shift; shift

echo '{grade=""} \' > $tempfile.awkprog
while ($#argv>0)
  echo '{if ($1>'$1') grade="'$2'"} \' >> $tempfile.awkprog
  shift; shift
end
echo '{if (grade != "") print "rightstring '$col'" NR " = \"" grade "\""}' \
  >> $tempfile.awkprog

cat $fileroot.txt | excol $srccolname | tail -n +2 | awk -f $tempfile.awkprog \
  >> $tempfile
set awkres = $status
# In old sc did: "echo q | sc $tempfile" to make it put lines into canonical order
# set scres = $status

set wcold = `cat $fileroot.sc | wc -l`
set wcnew = `cat $tempfile | wc -l`
set shrunk = `expr $wcold \> $wcnew`
if ($gvres || $awkres || $shrunk) then
  echo "Error within letsinsc (grep status/awk status/shrunk $gvres/$awkres/$shrunk).  Old $fileroot.sc to be kept."
else
  # Comment out next 2 lines if don't mind destructively modifying $1.sc.
  del . $fileroot.sc
  cp .del/$fileroot.sc .
  cp $tempfile $fileroot.sc  # keeps old protections of $fileroot.sc unlike mv
  rm -f $tempfile*
endif

make $fileroot.txt
