#!/usr/bin/perl -w -I/opt/ep2stable/perl_lib

######################################################################
#
#  This file is part of GNU EPrints 2.
#  
#  Copyright (c) 2000-2004 University of Southampton, UK. SO17 1BJ.
#  
#  EPrints 2 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.
#  
#  EPrints 2 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 EPrints 2; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
######################################################################

=pod

=head1 NAME

B<create_user> - add a new user to an EPrint archive

=head1 SYNOPSIS

B<create_user> I<archiveid> [B<options>] I<username> I<email> I<usertype> [I<password>]

=head1 DESCRIPTION

Create a new user to an eprint archive. 

This is handy for making the initial admin user at the very least.

=head1 ARGUMENTS

=over 8

=item I<archiveid> 

The ID of the EPrint archive to add a user to.

=item I<username> 

The requested username for the new user. If a user with this name already exists then the script will abort with a error.

=item I<email> 

The email address of the new user.

=item I<usertype> 

The type of the new user. The type of a user sets how much they can do in the system. The default EPrints configuration provides 3 types of users:

=over 8

=item I<user>

Normal everyday joe public users.

=item I<editor>

Editors may approve eprints for addition, browse the submission buffer, and check the archive status.

=item I<admin>

Administrators may do everything. That's probably what for yourself if you are the person setting up the archive.

=back

=item I<password>

The initial password for this user. You don't have to specify it here if you don't want to. It will be
encrypted, by EPrints, using UNIX crypt. This should not be a problem unless you are using a different
method to authenticate users.

=item I<archiveid> 

The ID of the EPrint archive to add a user to.

=back

=head1 OPTIONS

=over 8

=item B<--help>

Print a brief help message and exit.

=item B<--man>

Print the full manual page and then exit.

=item B<--quiet>

Be vewwy vewwy quiet. This option will supress all output unless an error occurs.

=item B<--verbose>

Explain in detail what is going on.
May be repeated for greater effect.

=item B<--version>

Output version information and exit.

=back   

=head1 AUTHOR

This is part of this EPrints 2 system. EPrints 2 is developed by Christopher Gutteridge.

=head1 VERSION

EPrints Version: 2.3.13.1

=head1 CONTACT

For more information goto B<http://www.eprints.org/> which give information on mailing lists and the like.

Chris Gutteridge may be contacted at B<support@eprints.org>

Should you need a real world address for some reason, EPrints can be contacted in the real world at

 EPrints c/o Christopher Gutteridge
 Department of Electronics and Computer Science
 University of Southampton
 SO17 1BJ
 United Kingdom

=head1 COPYRIGHT

This file is part of GNU EPrints 2.

Copyright (c) 2000-2004 University of Southampton, UK. SO17 1BJ.

EPrints 2 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.

EPrints 2 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 EPrints 2; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

=cut

#cjg Does not use noise levels

use EPrints::Database;
use EPrints::User;
use EPrints::Session;

use strict;
use Getopt::Long;
use Pod::Usage;

my $verbose = 0;
my $version = 0;
my $quiet = 0;
my $help = 0;
my $man = 0;

GetOptions( 
	'help|?' => \$help,
	'man' => \$man,
	'version' => \$version,
	'verbose+' => \$verbose,
	'silent' => \$quiet,
	'quiet' => \$quiet
) || pod2usage( 2 );
EPrints::Utils::cmd_version( "create_user" ) if $version;
pod2usage( 1 ) if $help;
pod2usage( -exitstatus => 0, -verbose => 2 ) if $man;
pod2usage( 2 ) if( scalar @ARGV != 4 && scalar @ARGV != 5 ); 

my $noise = 1;
$noise = 0 if( $quiet );
$noise = 1+$verbose if( $verbose );

# Set STDOUT to auto flush (without needing a \n)
$|=1;

my $session = EPrints::Session->new( 1 , $ARGV[0], $noise );
exit unless( defined $session );

my $user_ds = $session->get_archive()->get_dataset( "user" );

my $valid_access_level = 0;
my $type;
foreach $type (@{ $user_ds->get_types() })
{
	$valid_access_level = 1 if( $ARGV[3] eq $type );
}

if( defined EPrints::User::user_with_username( $session, $ARGV[1] ) )
{
	print STDERR "User with username '$ARGV[1]' already exists.\n";
}
elsif( $valid_access_level )
{
	my $new_user = EPrints::User::create_user( $session, $ARGV[3] );

	$new_user->set_value( "username" , $ARGV[1] );
	$new_user->set_value( "email" , $ARGV[2] );
	if( $#ARGV == 4 )
	{
		$new_user->set_value( "password" , EPrints::Utils::crypt_password( $ARGV[4], $session ) );
		$new_user->commit;
	}

	if( defined $new_user )
	{
		if( $noise >= 1 )
		{
			print "Successfully created new user:\n";
			print "       ID: ".$new_user->get_value( "userid" )."\n";
		}
		if( $noise >= 2 )
		{
			print " Username: ".$new_user->get_value( "username" )."\n";
			print "     Type: ".$new_user->get_value( "usertype" )."\n";
		}
	}
	else
	{
		my $db_error = $session->get_db()->error();
		print STDERR "Error creating user: $db_error\n";
	}
}
else
{
	print STDERR "Invalid access level. Valid access levels are:\n";
	foreach (@{ $user_ds->get_types() })
	{
		print "  $_\n";
	}
}

$session->terminate();
