package Cpanel::Easy::Tomcat::7_0;

# cpanel - Cpanel/Easy/Tomcat/7_0.pm              Copyright(c) 2013 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

use strict;
use warnings;
use Cpanel::Easy::Apache::Utils::Support ();
use Cpanel::Easy::Utils::Tomcat          ();
use Cpanel::Version::Compare             ();
use Cpanel::Version::Tiny                ();
use File::Copy::Recursive                ();
use Cpanel::StringFunc::File             ();
use Cpanel::SafeRun                      ();

my $version       = '7.0.42-3';
my $short_version = '7.0';
my $tomcat_port   = '8080';

our $easyconfig = {
    'name'      => qq{Tomcat $short_version},
    'verify_on' => qq{Do not install Tomcat without an experienced tomcat admin.\n\nThe Tomcat service requires the "tomcat" user to be in the "nobody" user group. If you already use Tomcat, EasyApache will change the group ID of the "tomcat" user to the "nobody" user group ID.},
    'version'   => $version,
    'support'   => $Cpanel::Easy::Apache::Utils::Support::EXPERIMENTAL,
    'url'       => 'http://tomcat.apache.org',
    'note'      => "Experts ONLY: Do not install this unless you have a tomcat admin.",
    'implies'   => {
        'Cpanel::Easy::ModJk' => 1,
    },
    'easy-rpms' => ['easy-tomcat7'],
    'ensurepkg' => [
        'log4j',
        'eclipse-ecj',
        'jakarta-commons-logging',
        'jakarta-commons-collections',
        'jakarta-commons-dbcp',
        'jakarta-commons-pool',
        'jakarta-taglibs-standard',
        'java-1.7.0-openjdk-devel',
    ],
    'skip'    => 0,
    'modself' => sub {
        my ( $easy, $self_hr ) = @_;

        # Tomcat 7 only available with cPanel/WHM 11.38 and later.
        if ( !$easy->tomcat_7_available() ) {
            $self_hr->{'hastargz'}                   = 0;
            $self_hr->{'skip'}                       = 1;
            $self_hr->{'treat_as_off_while_skipped'} = 1;
            return ( 1, 'Ok' );
        }

        # If Tomcat 5.5 is not avaiable, don't include this rule, so that we
        # don't generate messages about a version of Tomcat that is supposed to
        # be hidden from the user
        if ( $easy->tomcat_55_available() ) {
            $self_hr->{'implies'}{'Cpanel::Easy::ModJk5'} = 0;
        }
    },
    'step' => {
        '0' => {
            'name'    => 'Use EasyApache workers.properties',
            'command' => sub {
                my $self    = shift;
                my $src_dir = $self->{'opt_mod_src_dir'};
                my $jk_dir  = Cpanel::Easy::Utils::Tomcat::get_src_modjk_confdir();
                my @ret;

                # remove mod_jk provided file, then overwrite with our own cpanel file
                if ( -e "$src_dir/workers.properties" && -d $jk_dir ) {
                    unlink "$jk_dir/workers.properties";

                    if ( Cpanel::FileUtils::safecopy( "$src_dir/workers.properties", "$jk_dir/workers.properties" ) ) {
                        @ret = ( 1, 'Ok' );
                    }
                    else {
                        @ret = ( 0, q{Failed to backup '[_1]' using [_2]}, 'workers.properties', 'safecopy()' );
                    }
                }
                else {
                    @ret = ( 0, q{Unable to use EA workers.properties; Check to make sure '[_1]' and '[_2]' exist}, "$src_dir/workers.properties", $jk_dir );
                }

                return @ret;
            },
        },
        '1' => {
            'name'    => 'Install mod_jk configuration files',
            'command' => sub {
                my $self    = shift;
                my $src_dir = Cpanel::Easy::Utils::Tomcat::get_src_modjk_confdir();
                my $dst_dir = $self->get_tomcat_confdir();
                my @ret;

                if ( -d $dst_dir ) {
                    local $File::Copy::Recursive::CPRFComp = 0;

                    if ( File::Copy::Recursive::dircopy( $src_dir, $dst_dir ) ) {
                        @ret = ( 1, 'Ok' );
                    }
                    else {
                        @ret = ( 0, q{Failed to copy '[_1]' to '[_2]' using '[_3]'}, $src_dir, $dst_dir, 'dircopy()' );
                    }
                }
                else {
                    @ret = ( 0, q{Unable to find Tomcat configuration directory, [_1]}, $dst_dir );
                }

                return @ret;
            },
        },
        '2' => {

            # This is here because we have to deploy mod_jk to Apache when either 5.5
            # or 7.0 are installed.  They place configuration file in different directories.
            # This is here to ensure jk.conf points to the correct path.
            'name'    => 'Verify Apache jk.conf points to Tomcat 7.0 configuration directory',
            'command' => sub {
                my $self = shift;
                my $jk = sprintf( '%s/jk.conf', $self->_get_main_httpd_confdir() );
                my @ret;

                if ( -e $jk ) {
                    my $workers = sprintf( '%s/workers.properties', $self->get_tomcat_confdir() );

                    if ( -e $workers ) {
                        Cpanel::StringFunc::File::remlinefile( $jk, 'JkWorkersFile', 'begin' );
                        @ret = $self->add_to_file_after_regex( $jk, qq{    JkWorkersFile $workers}, qr{IfModule\s+mod_jk} );
                    }
                    else {
                        @ret = ( 0, q{Unable to find Tomcat 7.0 config file, [_1]}, $workers );
                    }
                }
                else {
                    @ret = ( 0, q{Unable to find '[_1]'}, $jk );
                }

                return @ret;
            },
        },
        '3' => {
            'name'    => q{Adding .jsp support setup to post restart list},
            'command' => sub {
                my $self = shift;

                push @{ $self->{'_'}{'post_httpd_restart_tests'} }, {
                    'name'    => q{Enabling servlet support for enabled domains},
                    'command' => sub {
                        my $easy = shift;
                        return $easy->mass_install_servlet();
                      }
                };

                return ( 1, 'Ok' );
            },
        },
        '4' => {
            'name'    => 'Adding Tomcat startup to post restart list',
            'command' => sub {
                my ($self) = @_;

                push @{ $self->{'_'}{'post_httpd_restart_tests'} }, {
                    'name'    => 'Tomcat 7 startup',
                    'command' => sub {
                        my ($self) = @_;
                        $self->print_alert(q{starting tomcat 7...});
                        my ( $rc, @msg );
                        {
                            local %ENV = %ENV;
                            Cpanel::Env::cleanenv( 'http_purge' => 1 );
                            ( $rc, @msg ) = $self->run_system_cmd_returnable( ['/usr/sbin/starttomcat'], 1, 1 );
                        }
                        $self->print_alert(q{done});

                        if ($rc) {
                            sleep 30;
                            $self->print_alert("Verifying Tomcat setup");
                            my $content = $self->get_localhost_uri( '/', 0, '', $tomcat_port );
                            if ( !$content ) {
                                print "\n";
                                return (
                                    0,
                                    qq{There was no content, the test may be unable to connect to '[_1]' simply because tomcat is still initializing.\nYou can try to go to http://localhost:[_1]/ to manually check it.},
                                    $tomcat_port
                                );
                            }
                            elsif ( $content =~ m{successfully installed Tomcat} ) {
                                return ( 1, 'ok' );
                            }
                            else {
                                return ( 0, q{content received but not the default Apache tomcat page, probably ok} );
                            }
                        }
                        else {
                            return ( 0, @msg );
                        }
                    },
                };

                return ( 1, 'ok' );
            },
        },
    },
};

1;

__END__

