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

# usage: cat .htusers | sendlogins.pl http://docmain.domain.com/

#my $debug=1;

my $in_mail=$0;
$in_mail=~s/\.pl/.txt/;

my $url=$ARGV[0];

if (! $url) {
	print STDERR "To send announcement to all users in .htusers file use\n";
	print STDERR "$0 http://docman.site.com/ < .htusers\n";
	exit 1;
}

$|++;

while(<STDIN>) {
	chomp;
	chomp;

	($login,$fullname,undef,$email) = split(/:/,$_);
	my $host=$email;
	$host=~s/^[^@]*@//g;

	if (defined $debug) {
		open(MAIL,">> /tmp/mailfoo") || die "$!";
	} else {
		open(MAIL,"| /usr/lib/sendmail -t") || die "sendmail: $!";
	}

	print MAIL "To: $fullname <$email>\n";

	open(IN,"$in_mail") || die "in mail: $!";
	while(<IN>) {
		chomp;
		chomp;
		s/##login##/$login/g;
		s/##url##/$url/g;
		s/##pop3host##/$host/g;
		print MAIL "$_\n";
	}
	close(IN);
	close(MAIL);
	print "." if ($debug);
}

