#!/usr/local/bin/webwork-perl

## $Id$

BEGIN {
	my $useLibDir = '.';
	if    ($0 =~ m|^(/.*)/|)  { $useLibDir = $1; }
	elsif ($0 =~ m|^(\..*)/|) { $useLibDir = $1; }
	elsif ($0 =~ m|^(.+)/|)   { $useLibDir = "./$1"; }
	unshift @INC, $useLibDir;
}

use webworkInit; # WeBWorKInitLine
use Global;


## readURClassList
##
## this is a specific routine for reading class lists which come from the registrar's
## office at the Univ of Rochesterand producing a classlist file usable by WeBWorK

## IT IS ASSUMED THAT THE REGISTRAR'S LIST IS DELIMITED WITH SEMICOLONS (;)

##
## Takes three parameters.
## First, the full file name of the Registrar's class list file with the header
##	material stripped off.
## Second, the full file name of the output WeBWorK classlist file
## Third, the name of the section, e.g. Pizer or "Gage MWF" or "" (blank).
##   For example classlist files from multiple sections can be concatonated
##   one large classlist file for a whole multisection course
##
## NOTE:  Be very careful.  The registrar's file may get corrupted by e-mail.


require 5.000;


require "$Global::scriptDirectory$Global::FILE_pl";

$0 =~ s|.*/||;
die "\n usage: $0 registrar's-list outputfile  sectionName\n
     e.g. readURClassList.pl  ClassRoster.txt mth140A.lst 'Gage MWF9'\n\n" unless (@ARGV == 3);

my ($infile, $outfile, $section) = @ARGV;

open(REGLIST, "$infile") || die "can't open $infile: $!\n";
open(OURLIST, ">$outfile")
    || die "can't write $outfile: $!\n";

while (<REGLIST>) {
    chomp;
    next unless($_=~/\w/);	        ## skip blank lines
    s/;$/; /;				## make last field non empty
    my @regArray=split(/;/);		## get fields from registrar's file

    foreach (@regArray) {		## clean 'em up!
	($_) = m/^\s*(.*?)\s*$/;        ## (remove leading and trailing spaces)
    }

  ## extract the relevant fields

   my($crn, $id, $grade, $name, $school, $gradyear,
      $major, $degree, $hours, $status, $login )
     = @regArray;


  ## massage the data a bit

    my($lname, $fname) = ($name =~ /^(.*),\s*(.*)$/);
    if ($login =~/\w/) {$email = "$login".'@mail.rochester.edu';}
    else
	{
	$email= " ";
	$login = $id;
	}
  ## dump it in our classArray format
  ## our format is: $id, $lname, $fname, $status, 'comment ', $dept, $course, $section,
  ## $hours, $crn, $year, $semester, $school, $gradyear, $major, $degree, $email, $login

  ## At the U of R 'comment' is blank
  ## At present only $id, $lname, $fname, $status, $email, $section, $recitation and $login are used by WeBWorK

    my @classArray=($id, $lname, $fname, $status, ' ', $section, ' ',$email, $login);

  ## and print that sucker!

    print OURLIST join("$Global::delim", @classArray) , "\n";
}
  close(OURLIST);

  ## arrange the columns nicely

   &columnPrint("$outfile","$outfile");



