#!/usr/bin/perl
use Getopt::Std;
use Cyrus::IMAP::Admin;

getopts('s:');
use vars qw($opt_s);
$msg="\n\nThis script cleans up the /var/spool/imap/users directory";
$msg.="\n\n- You must pass the -s option with the ldap root_pw";
$msg.="\n- Kolab must be running\n";
$msg.="\n*** WARNING! *** ALL MAIL USERS WILL BE DELETED!!!\n\n\n";
$opt_s || die($msg);


$pw=$opt_s;
$login="manager";

sub dolog
{
  #print $_[0]."\n";
  #syslog('info', "$_[0]");
}

my $cyrus = Cyrus::IMAP::Admin->new('localhost')
	|| die "could not connect to Cyrus IMAP daemon";
$cyrus->authenticate('User' => $login, 'Password' => $pw, 'mechanisms' => "plaintext")
        || die "could not authenticate with Cyrus IMAP daemon ($cyrus->{'error'})";

@mailboxes=$cyrus->list('*');
foreach(@mailboxes) {
$cyruid=@$_[0];
print "Deleting $cyruid\n";
$cyrus->setaclmailbox($cyruid, 'manager', 'c')
    || dolog("could not reset acl to delete imap user $cyruid");
$cyrus->deletemailbox($cyruid) || dolog("could not delete imap user $cyruid");
}

