#!/usr/bin/perl -w

use strict;

use lib "/usr/share/request-tracker3.6/lib";
use RT;
RT::LoadConfig;
$RT::LogToScreen = 'notice';
RT::InitLogging;

$RT::Logger->debug("Connecting to the database");
require RT::Handle;
$RT::Handle = RT::Handle->new();
$RT::Handle->Connect();

my $CurrentUser = new RT::CurrentUser();

my $attr = RT::Attribute->new($CurrentUser);

$RT::Logger->debug("Loading the possibly broken attribute");
$attr->LoadByCols(Name => "Search - My Tickets");

my $format = $attr->SubValue('Format');
$RT::Logger->debug("Format value: $format");

my @matches = ($format =~ m{href="([^"]*)/Ticket/Display.html}g);

for my $match (@matches) {
    next if $match eq "__WebPath__";
    $RT::Logger->notice("Replacing webpath \"$match\" with __WebPath__");
    $format =~ s{href="\Q$match\E/Ticket/Display.html}{href="__WebPath__/Ticket/Display.html};
    $attr->SetSubValues(Format => $format);
    $RT::Logger->debug("Format now: $format");
}
$RT::Logger->debug("Finished");

