#!/usr/pkg/bin/perl
#
require './functions-lib.pl';

use CGI; $q = new CGI;
if ($q->param("rebuild")==1)
	{
	&ui_print_header(undef,$text{rebuild}." ".$text{mbox_tree}, undef);
# reset permissions
    my $cyrus = &cyrus_connect();
    @list = $cyrus->list("*"); # Fetch all mailboxes below the current path
    foreach $entry (@list) 
    {
    %acls=$cyrus->listacl($entry->[0]); 
    $cyrus->setacl($entry->[0], $config{admin_user}=>"all") unless $acls{$config{admin_user}} eq "lrswipcda";
    }
# reconstruct    
	$rebuild=$config{lib_path}."/reconstruct";
	if (-f $rebuild)
		{
        @res=split /\n/, `su -c '$rebuild' cyrus`;
		print name_decode(join " - ",@res); 
        &webmin_log("rebuild all mailboxes");
        }
    else 
        {
        print "<BR><BR>$rebuild ", $text{not_found}, "<BR>", $text{go_to_config};
        exit;
        }
	print "<HR><FORM><INPUT TYPE=BUTTON VALUE=Ok onClick=\"location.href='mailbox.cgi'\"></FORM>";
	}
else
	{
	&ui_print_header(undef,$text{'mbox_tree'}, undef, "help", 1, 1, 0, undef, undef, undef,
	"<A HREF=mailbox.cgi?rebuild=1>".$text{rebuild}."</A>"
	);

	print "
<SCRIPT>
h=screen.availHeight; 
if (h<=600) h=h-300; 
else h=h-330;
document.write('<IFRAME ID=iframe SRC=frames.html HEIGHT='+h+' WIDTH=98%></IFRAME>');
</SCRIPT>
";
	}

&ui_print_footer("index.cgi", $text{'index_title'});
