#!/usr/bin/perl -w

# perl script to add users to Document Manager's .htusers file

use strict;
use Digest::MD5 qw(md5_hex);

my $htpasswd = shift @ARGV || undef;

if (! defined $htpasswd || defined $htpasswd && $htpasswd eq "-h") {
	print "Usage: $0 htusers_file [LOGIN] [full name] [password] [email]\n";
	exit;
}

my $login = shift @ARGV;
while (! $login) {
	print "login: ";
	$login=<STDIN>; chomp $login;
}

my $fullname = shift @ARGV;
while (! $fullname) {
	print "full name: ";
	$fullname=<STDIN>; chomp $fullname;
}

my $passwd = shift @ARGV;
if (! $passwd) {
	print "password [auth_pop3]: ";
	$passwd=<STDIN>; chomp $passwd;
	if ($passwd eq "") {
		$passwd="auth_pop3";
	}
}
if (substr($passwd,0,5) ne "auth_") {
		$passwd=md5_hex($login.$passwd);
}

my $email = shift @ARGV;
while (! $email || $email !~ /\w@\w/) {
	print "e-mail: ";
	$email=<STDIN>; chomp $email;
	print "e-mail address needs to have user\@domain for auth_pop3",
	      " to work!\n" if ($email !~ /\w@\w/);
}

if (! -e "$htpasswd") {
	print "creating $htpasswd\n";
	open(HTPASSWD,"> $htpasswd") || die "open $htpasswd: $!";
} else {
	open(HTPASSWD,">> $htpasswd") || die "append $htpasswd: $!";
}

print HTPASSWD "$login:$fullname:$passwd:$email\n";
close(HTPASSWD);
