#!/usr/bin/perl -w

use strict;
use Cyrus::IMAP::Admin;
use Getopt::Long;
Getopt::Long::Configure('bundling');

my ($hostname, $cyr_user, $cyr_pass, $mailkonto, $errstr);

GetOptions(
        "H=s" => \$hostname, "hostname=s" => \$hostname,
        "U=s" => \$cyr_user, "user=s" => \$cyr_user,
        "P=s" => \$cyr_pass, "password=s" => \$cyr_pass,
        "K=s" => \$mailkonto, "mailbox=s" => \$mailkonto,
);

if (!$hostname) { $errstr .= "Hostname fehlt\n"; }
if (!$cyr_user) { $errstr .= "cyrus-ServiceAccount fehlt\n"; }
if (!$cyr_pass) { $errstr .= "ServiceAccount Passwort fehlt\n"; }
if (!$mailkonto) { $errstr .= "Mailkonto fehlt\n"; }

if ($errstr) {
        print $errstr."\n";
        print_usage();
        exit 2;
}

my $mailbox = "user.$mailkonto";

my $cyrus = Cyrus::IMAP::Admin->new($hostname);
$cyrus->authenticate(
        -user => $cyr_user,
        -mechanism => 'login',
        -password => $cyr_pass
) or die "Couldn't bind to cyrus server on host $hostname";

$cyrus->setacl($mailbox, $mailkonto => "all", cyrus => "all");
warn $cyrus->error if $cyrus->error;

#####################################################################
sub print_usage {
#####################################################################
print "Usage: cyr_acl.pl -H <hostname> -U <cyrus> -P <pass> -K <mailbox>\n";
print " -H|--hostname <hostname>\n";
print " -U|--cyr_user <cyrus>\n";
print " -P|--cyr_pass <pass>\n";
print " -K|--mailbox <mailbox>\n";
}

