#! /usr/local/bin/perl

# htpasswd2dbm.pl - convert htpasswd file to DBM format

# For details of how to use this program, see Apache Week issue 42
#     http://www.apacheweek.com/issues/96-11-22

while ($_ = $ARGV[0], /^-/) {
    shift;
    last if /^--$/;
    /^-htpasswd/ && ($htpasswd = shift, next);
    /^-group/ && ($group = shift, next);
}

if ($#ARGV != 0) {
    print "Usage: $0 [-htpasswd passwdfile] [-group groupname ] dbmfile\n";
    exit(1);
}

$dbmfile = $ARGV[0];

if ($htpasswd) {
    open($fh, $htpasswd) || die "Cannot open $htpasswd: $!\n";
} else {
    $fh = STDIN;
    $htpasswd = "-";
}

dbmopen(DBM, $dbmfile, 0644) || do { 
    close(IN); 
    die "Cannot dbmopen $dbmfile: $!\n"; };

while (<$fh>) {
    chop;
    ($user, $enc) = split(/:/);

    $value = $enc;
    $value .= ":$group" if ($group);

    $DBM{$user} = $value;

    print "user $user added to $dbmfile\n";
}
close($fh);
close(DBM);

