#!/usr/bin/perl

# Check for data in the name_knownas field and split/copy into alias_lastname, alias_firstname

# Update Disabled.

use DBI;

eval require "./admin.conf";
if ( $@ ) {
    print $lex{Error}. " $@<br>\n";
    die $lex{Error}. " $@\n";
}

my @dbase = sort keys %alldbase;

push @dbase, 'eagleview';
push @dbase, 'chieftaylor';
push @dbase, 'khwc';
push @dbase, 'pewasenakwan';
push @dbase, 'poundmaker';

my $user = 'root';
my $password = 'MYSQL Root Password';

my $count++;
foreach $database (@dbase) { #defined in global config file.

    print "Database:$database\n";
    
    my $dsn = "DBI:mysql:$database";
    my $dbh = DBI->connect($dsn,$user,$password);

    
    my $sth1 = $dbh->prepare("update student set alias_lastname = ?, alias_firstname = ? 
			     where studnum = ?");

    my $sth = $dbh->prepare("select * from student order by lastname, firstname");
    if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; }
    $sth->execute;

    while ( my $ref = $sth->fetchrow_hashref ) {
	my %r = %$ref;
	
	my $studnum = $r{studnum};

	if ( $r{'name_knownas'} ) {
	    my ($ln,$fn);
	    if ( $r{'name_knownas'} =~ m/\s+/ ) { # we have spaces, split up name.
		my @alias = split(/\s+/, $r{'name_knownas'});
		$ln = pop @alias;
		$fn = join(' ', @alias);
	    } else { # no spaces; assume value is firstname;
		$fn = $r{'name_knownas'};
	    }
	
	    print qq{$database - LN:$ln FN:$fn - ALIAS:$r{'name_knownas'} - $r{firstname} $r{lastname}\n};

#	    $sth1->execute($ln, $fn, $studnum);
	    if ( $DBI::errstr ) { print $DBI::errstr; die $DBI::errstr; }
	}
	    
    }


} # end of database loop

print "Done!\n\n";
