#!perl
use Cassandane::Tiny;

sub test_cyr_expire_delete_findpaths_legacy
    :DelayedDelete :min_version_3_5 :MailboxLegacyDirs
{
    my ($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');

    xlog $self, "Delete $subfolder";
    $admintalk->unselect();
    $admintalk->delete($subfolder)
        or $self->fail("Cannot delete folder $subfolder: $@");
    $self->assert_str_equals('ok', $admintalk->get_last_completion_response());

    xlog $self, "Ensure we can't select $subfolder anymore";
    $admintalk->select($subfolder);
    $self->assert_str_equals('no', $admintalk->get_last_completion_response());
    $self->assert_matches(qr/Mailbox does not exist/i, $admintalk->get_last_error());

    my ($datapath) = $self->{instance}->folder_to_deleted_directories($subfolder);
    $self->assert_not_null($datapath);

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

    # the folder should not exist now!
    $self->assert_not_file_test($datapath, "-d");

    # Delete the entire user!
    $admintalk->delete($inbox);

    my $basedir = $self->{instance}{basedir};
    open(FH, "-|", "find", $basedir);
    my @files = grep { m{/user/magicuser/} and not m{/conf/lock/} } <FH>;
    close(FH);

    xlog $self, "DELETED files exists";
    $self->assert(scalar grep { m{/DELETED/} } @files);
    xlog $self, "no non-deleted paths";
    $self->assert(not scalar grep { not m{/DELETED/} } @files);

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

    open(FH, "-|", "find", $basedir);
    @files = grep { m{/user/magicuser/} and not m{/conf/lock/} } <FH>;
    close(FH);

    xlog $self, "no DELETED files exists";
    $self->assert(not scalar grep { m{/DELETED/} } @files);
    xlog $self, "no non-deleted paths";
    $self->assert(not scalar grep { not m{/DELETED/} } @files);
}
