#!perl
use Cassandane::Tiny;

sub test_cyr_expire_rename_safe
    :DelayedDelete :NoMailboxLegacyDirs
    ($self)
{
    my $adminstore = $self->{adminstore};
    my $admintalk = $adminstore->get_client();

    my $inbox = "user.magicuser";
    my $subfolder = "$inbox.foo";

    $admintalk->create($inbox);
    $admintalk->setacl($inbox, admin => 'lrswipkxtecdan');
    $admintalk->create($subfolder);
    $self->assert_str_equals('ok', $admintalk->get_last_completion_response());

    $adminstore->set_folder($subfolder);
    $self->make_message("Email", store => $adminstore) or die;

    # Create the search database.
    xlog $self, "Run squatter";
    $self->{instance}->run_command({cyrus => 1}, 'squatter');

    # Reset the conv.db version to 1 (to force UUID-based MAILBOXIDs)
    my $dirs = $self->{instance}->run_mbpath(-u => 'magicuser');
    my $format = $self->{instance}->{config}->get('conversations_db');
    $self->{instance}->run_dbcommand($dirs->{user}{conversations}, $format,
                                     ['SET', '$VERSION', '1']);

    my $res = $admintalk->status($inbox, ['mailboxid']);
    my $inboxid = $res->{mailboxid}[0];
    $res = $admintalk->status($subfolder, ['mailboxid']);
    my $subid = $res->{mailboxid}[0];

    xlog $self, "Run cyr_expire -D now.";
    $self->{instance}->run_command({ cyrus => 1 }, 'cyr_expire', '-D' => '0', '-X' => 0, '-E' => 0 , '-v');

    # the conversationsdb file still exists
    $self->assert_file_test($dirs->{user}{conversations}, '-f');
    $self->assert_file_test($dirs->{user}{xapianactive}, '-f');

    # Rename the user
    $admintalk->rename($inbox, "user.manifold");

    my $basedir = $self->{instance}{basedir};
    open(FH, "-|", "find", $basedir);
    my @files = grep { m{/uuid/} } <FH>;
    close(FH);

    xlog $self, "files for the inbox still exist";
    $self->assert_contains(qr{$inboxid}, \@files);
    xlog $self, "files left for subfolder";
    $self->assert_contains(qr{$subid}, \@files);

    xlog $self, "the conversations db still exists";
    $self->assert_file_test($dirs->{user}{conversations}, '-f');
    $self->assert_file_test($dirs->{user}{xapianactive}, '-f');

    xlog $self, "Run cyr_expire -D now.";
    $self->{instance}->run_command({ cyrus => 1 }, 'cyr_expire', '-D' => '0', '-X' => 0, '-E' => 0 , '-v');

    open(FH, "-|", "find", $basedir);
    @files = grep { m{/uuid/} } <FH>;
    close(FH);

    use Data::Dumper;
    xlog $self, "files for the inbox still exist" . Dumper(\@files, $inboxid);;
    $self->assert_contains(qr{$inboxid}, \@files);
    xlog $self, "files left for subfolder";
    $self->assert_contains(qr{$subid}, \@files);

    xlog $self, "the conversations db still exists";
    $self->assert_file_test($dirs->{user}{conversations}, '-f');
    $self->assert_file_test($dirs->{user}{xapianactive}, '-f');

    # Now delete the user
    $admintalk->delete("user.manifold");

    $basedir = $self->{instance}{basedir};
    open(FH, "-|", "find", $basedir);
    @files = grep { m{/uuid/} } <FH>;
    close(FH);

    xlog $self, "files for the inbox still exist";
    $self->assert_contains(qr{$inboxid}, \@files);
    xlog $self, "files left for subfolder";
    $self->assert_contains(qr{$subid}, \@files);

    xlog $self, "Run cyr_expire -D now.";
    $self->{instance}->run_command({ cyrus => 1 }, 'cyr_expire', '-D' => '0', '-X' => 0, '-E' => 0 , '-v');

    open(FH, "-|", "find", $basedir);
    @files = grep { m{/uuid/} } <FH>;
    close(FH);

    use Data::Dumper;
    xlog $self, "no files for the inbox still exist" . Dumper(\@files, $inboxid);;
    $self->assert_not_contains(qr{$inboxid}, \@files);

    xlog $self, "the conversations db no longer exists";
    $self->assert_not_file_test($dirs->{user}{conversations}, '-f');

}
