#!@PERL@
# BEGIN BPS TAGGED BLOCK {{{
#
# COPYRIGHT:
#
# This software is Copyright (c) 1996-2019 Best Practical Solutions, LLC
#                                          <sales@bestpractical.com>
#
# (Except where explicitly superseded by other copyright notices)
#
#
# LICENSE:
#
# This work is made available to you under the terms of Version 2 of
# the GNU General Public License. A copy of that license should have
# been provided with this software, but in any event can be snarfed
# from www.gnu.org.
#
# This work is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 or visit their web page on the internet at
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
#
#
# CONTRIBUTION SUBMISSION POLICY:
#
# (The following paragraph is not intended to limit the rights granted
# to you to modify and distribute this software under the terms of
# the GNU General Public License and is only of importance to you if
# you choose to contribute your changes and enhancements to the
# community by submitting them to Best Practical Solutions, LLC.)
#
# By intentionally submitting any modifications, corrections or
# derivatives to this work, or any other work intended for use with
# Request Tracker, to Best Practical Solutions, LLC, you confirm that
# you are the copyright holder for those contributions and you grant
# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
# royalty-free, perpetual, license to use, copy, create derivative
# works based on those contributions, and sublicense and distribute
# those contributions and any derivatives thereof.
#
# END BPS TAGGED BLOCK }}}
use 5.10.1;
use strict;
use warnings;

use lib "@LOCAL_LIB_PATH@";
use lib "@RT_LIB_PATH@";

use RT::Interface::CLI qw(Init);
Init();

my $db_name = RT->Config->Get('DatabaseName');
my $db_type = RT->Config->Get('DatabaseType');

my $dbh = $RT::Handle->dbh;

my $found_fm_tables;
foreach my $name ( $RT::Handle->_TableNames ) {
    next unless $name =~ /^fm_/i;
    $found_fm_tables->{lc $name}++;
}

unless ( $found_fm_tables->{fm_topics} && $found_fm_tables->{fm_objecttopics} ) {
    warn "Couldn't find topics tables, it appears you have RTFM 2.0 or earlier.";
    warn "This script cannot yet upgrade RTFM versions which are that old";
    exit;
}

{ # port over Articles
    my @columns = qw(id Name Summary SortOrder Class Parent URI Creator Created LastUpdatedBy LastUpdated);
    copy_tables('FM_Articles','Articles',\@columns);

}


{ # port over Classes
    my @columns = qw(id Name Description SortOrder Disabled Creator Created LastUpdatedBy LastUpdated);
    if ( grep lc($_) eq 'hotlist', $RT::Handle->Fields('FM_Classes') ) {
        push @columns, 'HotList';
    }
    copy_tables('FM_Classes','Classes',\@columns);
}

{ # port over Topics
    my @columns = qw(id Parent Name Description ObjectType ObjectId);
    copy_tables('FM_Topics','Topics',\@columns);
}

{ # port over ObjectTopics
    my @columns = qw(id Topic ObjectType ObjectId);
    copy_tables('FM_ObjectTopics','ObjectTopics',\@columns);
}

sub copy_tables {
    my ($source, $dest, $columns) = @_;
    my $column_list = join(', ',@$columns);
    my $sql;
    # SQLite: http://www.sqlite.org/lang_insert.html
    if ( $db_type eq 'mysql' || $db_type eq 'SQLite' ) {
        $sql = "insert into $dest ($column_list) select $column_list from $source";
    }
    # Oracle: http://www.adp-gmbh.ch/ora/sql/insert/select_and_subquery.html
    elsif ( $db_type eq 'Pg' || $db_type eq 'Oracle' ) {
        $sql = "insert into $dest ($column_list) (select $column_list from $source)";
    }
    $RT::Logger->debug($sql);
    $dbh->do($sql);
}

{ # create ObjectClasses
  # this logic will need updating when folks have an FM_ObjectClasses table
    use RT::Classes;
    use RT::ObjectClass;

    my $classes = RT::Classes->new(RT->SystemUser);
    $classes->UnLimit;
    while ( my $class = $classes->Next ) {
        my $objectclass = RT::ObjectClass->new(RT->SystemUser);
        my ($ret, $msg ) = $objectclass->Create( Class => $class->Id, ObjectType => 'RT::System', ObjectId => 0 );
        if ($ret) {
            warn("Applied Class '".$class->Name."' globally");
        } else {
            warn("Couldn't create linkage for Class ".$class->Name.": $msg");
        }
    }
}

{ # update ACLs
    use RT::ACL;
    my $acl = RT::ACL->new(RT->SystemUser);
    $acl->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::Class' );
    $acl->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::System' );
    while ( my $ace = $acl->Next ) {
        if ( $ace->__Value('ObjectType') eq 'RT::FM::Class' ) {
            my ($ret, $msg ) = $ace->__Set( Field => 'ObjectType', Value => 'RT::Class');
            warn "Fixing ACL ".$ace->Id." to refer to RT::Class: $msg";
        } elsif ( $ace->__Value('ObjectType') eq 'RT::FM::System' ) {
            my ($ret, $msg) = $ace->__Set(Field => 'ObjectType', Value => 'RT::System');
            warn "Fixing ACL ".$ace->Id." to refer to RT::System: $msg";
        }
    }


}

{ # update CustomFields
    use RT::CustomFields;
    my $cfs = RT::CustomFields->new(RT->SystemUser);
    $cfs->Limit( FIELD => 'LookupType', VALUE => 'RT::FM::Class-RT::FM::Article' );
    $cfs->{'find_disabled_rows'} = 1;
    while ( my $cf = $cfs->Next ) {
        my ($ret, $msg) = $cf->__Set( Field => 'LookupType', Value => 'RT::Class-RT::Article' );
        warn "Update Custom Field LookupType for CF.".$cf->Id." $msg";
    }
}

{ # update ObjectCustomFieldValues
    use RT::ObjectCustomFieldValues;
    my $ocfvs = RT::ObjectCustomFieldValues->new(RT->System);
    $ocfvs->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::Article' );
    $ocfvs->{'find_expired_rows'} = 1;
    while ( my $ocfv = $ocfvs->Next ) {
        my ($ret, $msg) = $ocfv->__Set( Field => 'ObjectType', Value => 'RT::Article' );
        warn "Updated CF ".$ocfv->__Value('CustomField')." Value for Article ".$ocfv->__Value('ObjectId');
    }

}

{ # update Topics
    use RT::Topics;
    my $topics = RT::Topics->new(RT->SystemUser);
    $topics->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::Class' );
    $topics->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::System' );
    while ( my $topic = $topics->Next ) {
        if ( $topic->__Value('ObjectType') eq 'RT::FM::Class' ) {
            my ($ret, $msg ) = $topic->__Set( Field => 'ObjectType', Value => 'RT::Class');
            warn "Fixing Topic ".$topic->Id." to refer to RT::Class: $msg";
        } elsif ( $topic->__Value('ObjectType') eq 'RT::FM::System' ) {
            my ($ret, $msg) = $topic->__Set(Field => 'ObjectType', Value => 'RT::System');
            warn "Fixing Topic ".$topic->Id." to refer to RT::System: $msg";
        }
    }
}

{ # update ObjectTopics
    use RT::ObjectTopics;
    my $otopics = RT::ObjectTopics->new(RT->SystemUser);
    $otopics->UnLimit;
    while ( my $otopic = $otopics->Next ) {
        if ( $otopic->ObjectType eq 'RT::FM::Article' ) {
            my ($ret, $msg) = $otopic->SetObjectType('RT::Article');
            warn "Fixing Topic ".$otopic->Topic." to apply to article: $msg";
        }
    }
}

{ # update Links
    use RT::Links;
    my $links = RT::Links->new(RT->SystemUser);
    $links->Limit(FIELD => 'Base', VALUE => 'rtfm', OPERATOR => 'LIKE', SUBCLAUSE => 'stopanding', ENTRYAGGREGATOR => 'OR');
    $links->Limit(FIELD => 'Target', VALUE => 'rtfm', OPERATOR => 'LIKE', SUBCLAUSE => 'stopanding', ENTRYAGGREGATOR => 'OR' );
    while ( my $link = $links->Next ) {
        my $base   = $link->__Value('Base');
        my $target = $link->__Value('Target');
        if ( $base =~ s/rtfm/article/i ) {
            my ($ret, $msg) = $link->__Set( Field => 'Base', Value => $base );
            warn "Updating base to $base: $msg for link ".$link->id;
        }
        if ( $target =~ s/rtfm/article/i ) {
            my ($ret, $msg) = $link->__Set( Field => 'Target', Value => $target );
            warn "Updating target to $target: $msg for link ".$link->id;
        }

    }
}

{ # update Transactions
  # we only keep article transactions at this point
    no warnings 'once';
    use RT::Transactions;
    # Next calls Type to check readability and Type calls _Accessible
    # which called CurrentUserCanSee which calls Object which tries to instantiate
    # an RT::FM::Article.  Rather than a shim RT::FM::Article class, I'm just avoiding
    # the ACL check since we're running around as the superuser.
    local *RT::Transaction::Type = sub { shift->__Value('Type') };
    my $transactions = RT::Transactions->new(RT->SystemUser);
    $transactions->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::Article' );
    while ( my $t = $transactions->Next ) {
        my ($ret, $msg) = $t->__Set( Field => 'ObjectType', Value => 'RT::Article' );
        warn "Updated Transaction ".$t->Id." to point to RT::Article";
    }

    # we also need to change links that point to articles
    $transactions = RT::Transactions->new(RT->SystemUser);
    $transactions->Limit( FIELD => 'Type', VALUE => 'AddLink' );
    $transactions->Limit( FIELD => 'NewValue', VALUE => 'rtfm', OPERATOR => 'LIKE' );
    while ( my $t = $transactions->Next ) {
        my $value = $t->__Value('NewValue');
        $value =~ s/rtfm/article/;
        my ($ret, $msg) = $t->__Set( Field => 'NewValue', Value => $value );
        warn "Updated Transaction ".$t->Id." to link to $value";
    }
}

{ # update Attributes
  # these are all things we should make real columns someday
    use RT::Attributes;
    my $attributes = RT::Attributes->new(RT->SystemUser);
    $attributes->Limit( FIELD => 'ObjectType', VALUE => 'RT::FM::Class' );
    while ( my $a = $attributes->Next ) {
        my ($ret,$msg) = $a->__Set( Field => 'ObjectType', Value => 'RT::Class' );
        warn "Updating Attribute ".$a->Name." to point to RT::Class";
    }
}
