{
    use esmith::config;
    use esmith::db;

    $OUT = '';

    # Generate qmail user assignments for pseudonyms. These will
    # be handled by ~user/.qmail in the case of a user pseudonym
    # or by ~admin/.qmail in the case of a system pseudonym or by
    # alias/.qmail-groupname in the case of a group pseudonym.

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

    my (undef, undef, $uid, $gid, undef, undef, undef, $dir, undef)
	= getpwnam("alias");

    # It is almost impossible to get Text::Template to output nothing
    # on failure. It can be done by removing the newline at the end of
    # this file but that is messy. Therefore, we'll simply return an
    # error message that will make qmail-newu fail. Also send a
    # warning message that will be captured in the logs.

    unless (defined $uid && defined $gid && defined $dir)
    {
	my $msg =
	    "Failed to obtain user details for \'alias\' "
	    . "while processing pseudonym assignments.";

	warn "$msg\n";
	$OUT = $msg;
	return;
    }

    my $alias_assign = "alias:${uid}:${gid}:${dir}";

    undef $uid;
    undef $gid;
    undef $dir;

    (undef, undef, $uid, $gid, undef, undef, undef, $dir, undef)
	= getpwnam("admin");

    unless (defined $uid && defined $gid && defined $dir)
    {
	my $msg =
	    "Failed to obtain user details for \'admin\' "
	    . "while processing pseudonym assignments.";

	warn "$msg\n";
	$OUT = $msg;
	return;
    }

    my $admin_assign = "admin:${uid}:${gid}:${dir}";

    # Create assignments for each pseudonym.

    foreach (db_get(\%accounts))
    {
	next unless db_get_type(\%accounts, $_) eq "pseudonym";

	my $account = db_get_prop(\%accounts, $_, "Account");

        if (db_get_type(\%accounts, $account) eq "pseudonym")
        {
            my ($type, %props) = db_get(\%accounts, $account);
     
            $account = $props{Account};
        }

        if (db_get_type(\%accounts, $account) eq "pseudonym")
        {
            warn "users/assign: Skipping $_ - too many pseudonym levels\n";
            next;
        }

	if (db_get_type(\%accounts, $account) eq "user")
	{
	    my (undef, undef, $uid, $gid, undef, undef, undef, $dir, undef)
		= getpwnam($account);

	    unless (defined $uid && defined $gid && defined $dir)
	    {
		my $msg =
		    "Failed to obtain user details for \'$account\' "
		    . "while processing pseudonym assignments.";

		warn "$msg\n";
		$OUT = $msg;
		return;
	    }

	    $assign = "${account}:${uid}:${gid}:${dir}";

	    # Assign mail for user_pseudonym@
	    $OUT .= "=${_}:${assign}:::\n";

	    next;
	}

	if (db_get_type(\%accounts, $account) eq "group")
	{
	    # Assign mail for user_pseudonym@
	    $OUT .= "=${_}:${alias_assign}:-:${account}:\n";

	    next;
	}

	if ($account eq "shared")
	{
	    # Assign mail for the special shared account
	    $OUT .= "=${_}:${alias_assign}:-:${account}:\n";

	    next;
	}

	if (db_get_type(\%accounts, $account) eq "system")
	{
	    # Assign mail for system_pseudonym@
	    $OUT .= "=${_}:${admin_assign}:::\n";

	    next;
	}
    }

    # Need to remove the final newline character. Blank lines in
    # /var/qmail/users/assign are prohibited.

    chomp($OUT);

    # Failsafe: /var/qmail/users/assign cannot have blank lines.
    # Therefore, if $OUT is empty, simply set up an assign for the
    # alias user.

    $OUT = "=alias:${alias_assign}:::" unless $OUT;
}
