#!/usr/local/bin/perl5 -w

require HTTPD::UserAdmin;
use Getopt::Std;
use File::Basename;

getopts('ht:s:e:');

$db = $dbtype = $server = undef;
$dlm = '\s+'; 

$db = shift;
usage($opt_h) if defined $opt_h || !defined $db;
$dbtype = $opt_t if defined $opt_t;
$server = $opt_s if defined $opt_s;
$enc    = $opt_e if defined $opt_e;

$admin = new HTTPD::UserAdmin 
    DB     => $db,
    DBType => $dbtype,
    Server => $server,
    Encrypt => $enc;

while (<>) {
    chomp;
    ($username,$password) = split(/$dlm/);
    ($rc,$msg) = $admin->add($username,$password);
    die $msg unless $rc > 0;
}

sub usage {
    my $script = basename $0;
    print <<"USAGE";
usage:
$script <database> <options>
options:
-t <dbtype> where dbtype is one of 'Text', 'DBM' or 'SQL', default is 'DBM'
-s <server> where server is the HTTP server name e.g. 'cern'
            default works with ncsa, apache, netscape and possibly others.
-e <encrypt> To specify MD5, or other method.
USAGE

}

__END__

=head1 NAME

bhtpasswd - Batch updates for HTTP server user databases

=head1 DESCRIPTION

This script is meant for adding many users at a time to a user database.  
From stdin, it reads newline delimited records and whitespace delimited fields.  
Where required fields are: 'username' 'password'.
Run bhtpasswd -h for options.

=head1 CAVEATS

This script is not complete.

=cut
