#!/usr/bin/perl

use strict;
use warnings;

BEGIN {
    unshift @INC, '/usr/local/cpanel';
}

use Cpanel::FileUtils ();

my %new_versions = ();
$new_versions{'addservlets2'} = <<'EO_ADDSERVLETS';
#!/usr/bin/perl
# cpanel - addservlets2                           Copyright(c) 2010 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

BEGIN {
    unshift @INC, '/usr/local/cpanel'; #issafe
}

use Cpanel::HttpUtils         (); #issafe
use Cpanel::CPAN::Getopt::Param (); #issafe
use Cpanel::EditHttpdconf       (); #issafe

if (! -e '/usr/local/jakarta') {
   print 'Sorry you must install mod_jk from easyapache';
   exit;
}

my $help_cr = sub {
    print <<"END_HELP";
Add JkMount entries to a vhost

$0 --help  (this screen)
$0 --domain=domain-you-want-to-have-jsp-and-servlets.com

END_HELP
    exit;
};

my $prm = Cpanel::CPAN::Getopt::Param->new({ #issafe
    'help_coderef' => $help_cr, 
});

my @list = $prm->list_params();
$help_cr->() if @list == 0;

my $domain = $prm->get_param('domain');
$domain =~ s{ \A www [.] }{}xms;
die 'Please specify a domain' if !$prm->get_param('domain') || $prm->get_param('domain') eq '--domain';

my $content = <<"END_CONTENT";
<IfModule mod_jk.c>
  JkMount /*.jsp ajp13
  JkMount /servlet/* ajp13
  JkMount /servlets/* ajp13
</IfModule>
END_CONTENT

my $addedhost = Cpanel::EditHttpdconf::add_vhost_include({ #issafe
    'domain'  => $prm->get_param('domain'),
    'file'    => 'cp_jkmount.conf',
    'restart_apache' => 1,
    'content' => {
        'std' => {
            '1' => $content,
            '2' => $content,
        },
        'ssl' => {
            '1' => $content,
            '2' => $content,            
        },        
    },
});

if ( $addedhost ) {
   system '/scripts/gentomcatlist2';
   Cpanel::HttpUtils::safeaprestart(); #issafe
   print "Done: .jsp support installed for $domain\n";
} 
else {
   print "Error: .jsp support could not be added for $domain\n";
}
EO_ADDSERVLETS

$new_versions{'remservlets'} = <<'EO_REMSERVLETS';
#!/usr/bin/perl
# cpanel - remservlets                            Copyright(c) 2010 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

BEGIN {
    unshift @INC, '/usr/local/cpanel';    #issafe
}

use Cpanel::HttpUtils           ();       #issafe
use Cpanel::CPAN::Getopt::Param ();       #issafe
use Cpanel::EditHttpdconf       ();       #issafe

if ( !-e '/usr/local/jakarta' ) {
    print 'Sorry you must install mod_jk from easyapache';
    exit;
}

my $help_cr = sub {
    print <<"END_HELP";
Remove JkMount entries to a vhost

$0 --help  (this screen)
$0 --domain=domain-you-want-to-remove-jsp-and-servlets.com

END_HELP
    exit;
};

my $prm = Cpanel::CPAN::Getopt::Param->new(
    {    #issafe
        'help_coderef' => $help_cr,
    }
);

my @list = $prm->list_params();
$help_cr->() if @list == 0;

my $domain = $prm->get_param('domain');
$domain =~ s{ \A www [.] }{}xms;
die 'Please specify a domain' if !$prm->get_param('domain') || $prm->get_param('domain') eq '--domain';

my $removedhost = Cpanel::EditHttpdconf::del_vhost_include(
    {    #issafe
        'domain'         => $prm->get_param('domain'),
        'file'           => 'cp_jkmount.conf',
        'restart_apache' => 1,
    }
);

if ($removedhost) {
    system '/scripts/gentomcatlist2';
    Cpanel::HttpUtils::safeaprestart();    #issafe
    print "Done: .jsp support removed for $domain\n";
}
else {
    print "Error: .jsp support could not be removed for $domain\n";
}
EO_REMSERVLETS

foreach my $file (qw(remservlets addservlets2)) {
    if ( -e '/scripts/' . $file && Cpanel::FileUtils::has_txt_in_file( '/scripts/' . $file, 'cPScript::' ) ) {
        if ( open my $fh, '>', '/scripts/' . $file ) {
            print $fh $new_versions{$file};
            close $fh;
            chmod( oct(700), '/scripts/' . $file );
            print "Updated /scripts/$file\n";
        }
        else {
            print "Failed to update /scripts/$file : $!\n";
        }
    }
}
