use strict;
use Tie::DBI ();

my $DB_NAME = 'test_www';
my $DB_HOST = 'localhost';

my %test_users = (
		  #user_name        groups            level   passwd
		  'root'   =>  [qw(users,authors,admin  5     superman)],
		  'george'  => [qw(users                3     jetson)],
		  'winnie'  => [qw(users,authors,devel  3     thepooh)],
		  'andrew'  => [qw(users                2     llama23)],
		  'fred'    => [qw(users,devel          2     bisquet)],
		  'morgana' => [qw(users                1     lafey)]
   		  );

# Sometimes it's easier to invoke a subshell for simple things
# than to use the DBI interface.
open MYSQL, "|mysql -h $DB_HOST -f $DB_NAME" or die $!;
print MYSQL <<END;
    DROP TABLE user_info;
CREATE TABLE user_info (
			user_name   CHAR(20) primary key,
			passwd      CHAR(13) not null,
			level       TINYINT  not null,
			groups      CHAR(100)
			);
END

close MYSQL;

tie my %db, 'Tie::DBI', { 
    db => "mysql:$DB_NAME:$DB_HOST",
    table => 'user_info',
    key   => 'user_name',
    CLOBBER=>1,
} or die "Couldn't tie to $DB_NAME:$DB_HOST";

my $updated = 0;
for my $id (keys %test_users) {
    my($groups, $level, $passwd) = @{$test_users{$id}};
    $db{$id} = { 
	passwd  =>  crypt($passwd, salt()),
	level   =>  $level,
	groups  =>  $groups,
    };
    $updated++;
}
untie %db;
print STDERR "$updated records entered.\n";

# Possible BUG: Assume that this system uses two character
# salts for its crypt().
sub salt { 
    my @saltset = (0..9, 'A'..'Z', 'a'..'z', '.', '/');
    return join '', @saltset[rand @saltset, rand @saltset];
}
