#!@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 $to = shift || '';
my $from;

if ($to =~ /html|text/i) {
    $to   = $to =~ /html/i  ? 'html' : 'text';
    $from = $to eq 'html'   ? 'text' : 'html';
} else {
    print "Usage: $0 [html|text]\n";
    warn "Please specify if you'd like to switch to HTML or text templates.\n";
    exit 1;
}


my @templates = (
    "Autoreply",
    "Transaction",
    "Admin Correspondence",
    "Correspondence",
    "Admin Comment",
    "Status Change",
    "Resolved",
    "New Pending Approval",
    "Approval Passed",
    "All Approvals Passed",
    "Approval Rejected",
    "Approval Ready for Owner",
);

$RT::Handle->BeginTransaction();

use RT::Scrips;
my $scrips = RT::Scrips->new( RT->SystemUser );
$scrips->UnLimit;

for (@templates) {
    $scrips->Limit(
        FIELD => 'Template',
        VALUE => ($to eq 'html' ? $_ : "$_ in HTML"),
        ENTRYAGGREGATOR => 'OR'
    );
}

my $switched = 0;
while ( my $s = $scrips->Next ) {
    my $new = $s->TemplateObj->Name;

    if ($to eq 'html') {
        $new .= ' in HTML';
    } else {
        $new =~ s/ in HTML$//;
    }

    print $s->id, ": ", $s->Description, "\n";
    print "    ", $s->TemplateObj->Name, " -> $new\n\n";

    my ($ok, $msg) = $s->SetTemplate($new);

    if ($ok) {
        $switched++;
    } else {
        warn "    Couldn't switch templates: $msg\n";
    }
}

$RT::Handle->Commit;

if ($switched) {
    print <<"    EOT";
Switched $switched scrips to $to templates.  You should now manually port any
customizations from the old templates to the new templates.
    EOT
    exit 1 if $switched != $scrips->Count;
}
elsif ($scrips->Count) {
    print <<"    EOT";
@{[$scrips->Count]} scrips using $from templates were found, but none were
successfully switched to $to.  See the errors above.
    EOT
    exit 1;
}
else {
    print <<"    EOT";
No scrips were found using the $from templates, so none were switched to
$to templates.
    EOT
}

