#!/usr/bin/perl -w

#----------------------------------------------------------------------
# copyright (C) 1999-2001 e-smith, inc.
#		
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#		
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
# GNU General Public License for more details.
#		
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
# 
# Technical support for this program is available from e-smith, inc.
# Please visit our web site www.e-smith.com for details.
#----------------------------------------------------------------------

package esmith;

use strict;
use Errno;
use esmith::config;
use esmith::util;
use esmith::db;

my %conf;
tie %conf, 'esmith::config';

my %accounts;
tie %accounts, 'esmith::config', '/home/e-smith/accounts';

#------------------------------------------------------------
# This program should be called by the post-upgrade or post-restore
# events. It will add missing First.Last and First_Last pseudonyms
# for users that don't already have them. It should be called AFTER
# the update-uids action.
#------------------------------------------------------------

sub byUid
{
    db_get_prop(\%accounts, $a, 'Uid') <=> db_get_prop(\%accounts, $b, 'Uid');
}

sub makePseudonyms ($$)
{
    #------------------------------------------------------------
    # Generate First.Last and First_Last pseudonyms
    #------------------------------------------------------------

    my ($firstName, $lastName) = @_;

    my $dot_pseudonym = "$firstName $lastName";

    $dot_pseudonym =~ s/^\s+//;		# Strip leading whitespace
    $dot_pseudonym =~ s/\s+$//;		# Strip trailing whitespace
    $dot_pseudonym =~ s/\s+/ /g;	# Multiple spaces become single spaces
    $dot_pseudonym =~ s/\s/./g;		# Change all spaces to dots
    $dot_pseudonym = lc $dot_pseudonym;	# Change to lower case

    my $underbar_pseudonym = $dot_pseudonym;
    $underbar_pseudonym =~ s/\./_/g;	# Change dots to underbars

    return ($dot_pseudonym, $underbar_pseudonym);
}

###########################################################################

# Handle each of the users. Process each user account in order of
# increasing user id. This will hopefully preserve the order in which they
# were created thereby ensuring seniority in the creation of pseudonyms.

my %users;

foreach my $account (keys %accounts)
{
    my $type = db_get_type(\%accounts, $account);

    next unless ($type =~ /^user$/);

    ++$users{$account};
}

foreach my $account (sort byUid keys %users)
{
    my $firstName = db_get_prop(\%accounts, $account, 'FirstName');
    my $lastName = db_get_prop(\%accounts, $account, 'LastName');

    my ($dot_pseudonym, $underbar_pseudonym)
	= makePseudonyms($firstName, $lastName);
    
    if (defined db_get(\%accounts, $dot_pseudonym))
    {
	my $user = db_get_prop(\%accounts, $dot_pseudonym, 'Account');

	if ($user ne $account)
	{
	    warn "$dot_pseudonym pseudonym is assigned to $user"
		. " instead of $account.\n";
	}
    }
    else
    {
	db_set(\%accounts, $dot_pseudonym, 'pseudonym');
	db_set_prop(\%accounts, $dot_pseudonym, 'Account', $account);
    }
    
    if (defined db_get(\%accounts, $underbar_pseudonym))
    {
	my $user = db_get_prop(\%accounts, $underbar_pseudonym, 'Account');

	if ($user ne $account)
	{
	    warn "$underbar_pseudonym pseudonym is assigned to $user"
		. " instead of $account.\n";
	}
    }
    else
    {
	db_set(\%accounts, $underbar_pseudonym, 'pseudonym');
	db_set_prop(\%accounts, $underbar_pseudonym, 'Account', $account);
    }

}

exit (0);
