#! /opt/bin/perl
#
# usage:
# > perl rename.pl <student-record> <directory-name>
# 
#

use File::Copy;

use strict;

#
# read "class_MMDD.csv"
#


#
# 1. read the file
#
open(FIN, $ARGV[0]) || die "error: name file not found";
my @records = <FIN>; 
close(FIN);

#
# 2. store column names
#
my $attributes = shift(@records);
chomp($attributes);

my @attributes = split(/,/,$attributes);


#
# 3. read and store the remaining info in the name list.
#
my $lmax;     # max length of the last name 
my $fmax;     # max length of the first name
my $umax;     # max lenght of the user id
my $smax;     # max length of the section name
 
my %records;  # keep students' info


#
# reach each student' record
# 
foreach my $rec (@records) {
  chomp($rec);
  my @fields = split(/,/,$rec);
  
  my ($sec, $lname, $fname, $uid) = (undef,undef,undef,undef);
  
  # 
  # read each field of the record
  #
  for( my $i = 0; $i < scalar @fields; $i++ ) {

    if( $attributes[$i] eq "Section" ) {
      $sec   = $fields[$i];
    } elsif( $attributes[$i] eq "Last Name" ) {
      $lname = $fields[$i];
      $lname =~ s/\s/_/g
    } elsif( $attributes[$i] eq "First Name" ) {
      $fname = $fields[$i];
      $fname =~ s/\s/_/g
    } elsif( $attributes[$i] eq "User ID" ) {
      $uid   = $fields[$i];
    }
  }
  
  if( ( ! defined $sec   || $sec   eq "" ) &&
      ( ! defined $lname || $lname eq "" ) && 
      ( ! defined $fname || $fname eq "" ) &&
      ( ! defined $uid   || $uid   eq "" )
    ) {
    next
  } elsif( ! defined $sec || $sec eq "" ) {
    $sec = "X";
  }
  
  $lmax = length($lname) if( $lmax < length($lname) );
  $fmax = length($fname) if( $fmax < length($fname) );
  $umax = length($uid)   if( $umax < length($uid)   );
  $smax = length($sec)   if( $smax < length($sec)   );
  
  ${$records{$uid}}{"sec"}   = $sec;
  ${$records{$uid}}{"lname"} = $lname;
  ${$records{$uid}}{"fname"} = $fname;

  # printf("%s %s, %s\n", $sec, $lname, $fname);
  
}





#
# 4. rename directory from user name to the real name
#

# insert gaps between last name, first name, or user id.
$lmax += 2;
$fmax += 2;
$umax += 2;
$smax += 2;

#
my $path = $ARGV[1] . "/";
$path =~ s/\/\/$/\//;

if( ! -d $path ) {
  printf(stderr "error: 2nd arg. should be the dir name.\n\n");
  exit(1);
}

foreach my $uid (sort keys %records) {
  
  my $sec   = ${$records{$uid}}{"sec"};
  my $lname = ${$records{$uid}}{"lname"};
  my $fname = ${$records{$uid}}{"fname"};

  # printf("%3s %s\n", $sec, $uid);

  if( ! -r $path.$uid ) {
    printf(stderr "warning: dir not exists \"%s\"\n", $path.$uid);
    next;
  }
  
  my $newfile;

  $newfile = sprintf("%s", $sec);

  for( my $i = length($sec); $i < $smax; $i++ ) {
    $newfile .= "_";
  }
  
  $newfile .= sprintf("%s", $lname);
  
  for( my $i = length($lname); $i < $lmax; $i++ ) {
    $newfile .= "_";
  }
  
  $newfile .= sprintf("%s", $fname);

  for( my $i = length($fname); $i < $fmax; $i++ ) {
    $newfile .= "_";
  }

  $newfile .= sprintf("%s", $uid);

  for( my $i = length($uid); $i < $umax; $i++ ) {
    $newfile .= "_";
  }
  
  #
  #
  #
  
  if( -r $path.$newfile ) {
    printf("warning: file \"%s\" already exists.\n\n", $path.$newfile);
  } elsif( move($path.$uid, $path.$newfile) ) {
    printf("move: %-24s --> %s\n", $path.$uid, $path.$newfile);    
  } else {
    printf("error: mv %s %s, failed.\n\n", $path.$uid, $path.$newfile);    
  }
}

