#! /usr/bin/env bash

# Update roster.txt file to contain a "STUDENT ID" column of appropriate width containing IDs from "EADDR" column;
# Will need to tweak some manually to match Sakai IDs.

# $var:t    ${var##*/}
# $var:h    ${var%/*}
# $var:e    ${var##*.}
# $var:r    ${var%.*}

tempfile=/tmp/$progname.$USER.$$

baselen=`cat roster.txt | wc -L`
idlen=`cat roster.txt | sed -n 's+.* \([^ ]*\)@.*+\1+p' | tee $tempfile | wc -L`
ids=`cat $tempfile | tr '\012' ' '`
len=`expr $baselen + $idlen + 2`
echo $len
cat roster.txt \
  | awk 'BEGIN {split("'"$ids"'",ids);}\
         {deficit = '$len'-length($0)}\
         NR==1 {printf "%s%*3$s\n",$0,"STUDENTID",deficit;}\
         NR>1 {printf "%s%*3$s\n",$0,ids[NR-1],deficit;}' > $tempfile

del . roster.txt
mv $tempfile roster.txt

rm -f $tempfile
