#!/usr/bin/perl
# Fred Blaise
# 02.09.2005
# Destined to be called from smbldap-useradd, to create mailbox automatically, upon successful creation of posix, Samba, OX user.
# watch the permission of this file, as the cyrus user passwd is in clear.

use Expect;

$|++;

$user=$ARGV[0]; chomp $user;

$cyrus_pass="cyrus";

$command = "cyradm --user cyrus localhost";
$timeout = 10;

my $exp = Expect->spawn("$command\n >/dev/null") || die $!;

$exp->expect($timeout,
	[ qr/IMAP Password: / => sub {
					my $fh = shift;
					$fh->send("$cyrus_pass\n");
					exp_continue;
				}
	],
	[ qr/localhost.localdomain> / => sub {
					my $fh = shift;
					$fh->send("cm user/$user\n");
					exp_continue;
					$fh->send("setacl user/$user cyrus all\n");
					exp_continue;
					$fh->send("quit\n");
				}
	],
	[ eof => sub {
			die "Error: $!\n";
			exit 1;
		}
	],
);
$exp->soft_close();

exit 0;
