@Initial = (
    sub {
        $RT::Logger->warning(
            "Going to add [OLD] prefix to all templates in approvals queue."
            ." If you never used approvals then you can delete all these"
            ." templates with [OLD] prefix. Leave the new ones there because"
            ." maybe you will eventually want to use start using approvals."
        );

        my $approvals_q = RT::Queue->new( RT->SystemUser );
        $approvals_q->Load('___Approvals');
        unless ( $approvals_q->id ) {
            $RT::Logger->error("You have no approvals queue.");
            return 1;
        }

        my $templates = RT::Templates->new( RT->SystemUser );
        $templates->LimitToQueue( $approvals_q->id );
        while ( my $tmpl = $templates->Next ) {
            my ($status, $msg) = $tmpl->SetName( "[OLD] ". $tmpl->Name );
            unless ( $status ) {
                $RT::Logger->error("Couldn't rename template #". $tmpl->id .": $msg");
            }
        }
        return 1;
    },
);
@ACL = (
    { GroupDomain => 'SystemInternal',
      GroupType => 'privileged',
      Right  => 'ShowApprovalsTab', },
);

@Templates = (
    {  Queue       => '___Approvals',
       Name        => "New Pending Approval",    # loc
       Description =>
         "Notify Owners and AdminCcs of new items pending their approval", # loc
       Content => 'Subject: New Pending Approval: {$Ticket->Subject}

Greetings,

There is a new item pending your approval: "{$Ticket->Subject()}", 
a summary of which appears below.

Please visit {RT->Config->Get(\'WebURL\')}Approvals/Display.html?id={$Ticket->id}
to approve or reject this ticket, or {RT->Config->Get(\'WebURL\')}Approvals/ to
batch-process all your pending approvals.

-------------------------------------------------------------------------
{$Transaction->Content()}
'
    },
    {  Queue       => '___Approvals',
       Name        => "Approval Passed",    # loc
       Description =>
         "Notify Requestor of their ticket has been approved by some approver", # loc
       Content => 'Subject: Ticket Approved: {$Ticket->Subject}

Greetings,

Your ticket has been approved by { eval { $Approval->OwnerObj->Name } }.
Other approvals may be pending.

Approver\'s notes: { $Notes }
'
    },
    {  Queue       => '___Approvals',
       Name        => "All Approvals Passed",    # loc
       Description =>
         "Notify Requestor of their ticket has been approved by all approvers", # loc
       Content => 'Subject: Ticket Approved: {$Ticket->Subject}

Greetings,

Your ticket has been approved by { eval { $Approval->OwnerObj->Name } }.
Its Owner may now start to act on it.

Approver\'s notes: { $Notes }
'
    },
    {  Queue       => '___Approvals',
       Name        => "Approval Rejected",    # loc
       Description =>
         "Notify Owner of their rejected ticket", # loc
       Content => 'Subject: Ticket Rejected: {$Ticket->Subject}

Greetings,

Your ticket has been rejected by { eval { $Approval->OwnerObj->Name } }.

Approver\'s notes: { $Notes }
'
    },
    {  Queue       => '___Approvals',
       Name        => "Approval Ready for Owner",    # loc
       Description =>
         "Notify Owner of their ticket has been approved and is ready to be acted on", # loc
       Content => 'Subject: Ticket Approved: {$Ticket->Subject}

Greetings,

The ticket has been approved, you may now start to act on it.

'
    },
);

@Final = (
    sub {
        $RT::Logger->debug("Going to adjust dashboards");
        my $sys = RT::System->new(RT->SystemUser);

        my $attrs = RT::Attributes->new( RT->SystemUser );
        $attrs->UnLimit;
        my @dashboards = $attrs->Named('Dashboard');

        if (@dashboards == 0) {
            $RT::Logger->debug("You have no dashboards. Skipped.");
            return 1;
        }

        for my $attr (@dashboards) {
            my $props = $attr->Content;
            if (exists $props->{Searches}) {
                $props->{Panes} = {
                    body => [
                        map {
                            my ($privacy, $id, $desc) = @$_;

                            {
                                portlet_type => 'search',
                                privacy      => $privacy,
                                id           => $id,
                                description  => $desc,
                                pane         => 'body',
                            }
                        } @{ delete $props->{Searches} }
                    ],
                };
            }
            my ($status, $msg) = $attr->SetContent( $props );
            $RT::Logger->error($msg) unless $status;
        }

        $RT::Logger->debug("Fixed.");
        return 1;
    },
    sub {
        my $approvals_q = RT::Queue->new( RT->SystemUser );
        $approvals_q->Load('___Approvals');
        unless ( $approvals_q->id ) {
            $RT::Logger->error("You have no approvals queue.");
            return 1;
        }

        require File::Temp;
        my ($tmp_fh, $tmp_fn) = File::Temp::tempfile( 'rt-approvals-scrips-XXXX', CLEANUP => 0 );
        unless ( $tmp_fh ) {
            $RT::Logger->error("Couldn't create temporary file.");
            return 0;
        }

        $RT::Logger->warning(
            "IMPORTANT: We're going to delete all scrips in Approvals queue"
            ." and save them in '$tmp_fn' file."
        );

        require Data::Dumper;

        my $scrips = RT::Scrips->new( RT->SystemUser );
        $scrips->LimitToQueue( $approvals_q->id );
        while ( my $scrip = $scrips->Next ) {
            my %tmp =
                map { $tmp->{ $_ } = $scrip->_Value( $_ ) }
                $scrip->ReadableAttributes;

            print $tmp_fh Data::Dumper::Dumper( \%tmp );

            my ($status, $msg) = $scrip->Delete;
            unless ( $status ) {
                $RT::Logger->error( "Couldn't delete scrip: $msg");
            }
        }
    },
);
