package Cpanel::Easy::ModJk;

# cpanel - Cpanel/Easy/ModJk.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;
no warnings qw( redefine );
use Cpanel::FileUtils           ();
use Cpanel::Easy::Utils::Tomcat ();

my $version = '1.2.37';

our $easyconfig = {
    'name'          => qq{ModJk $version},
    'version'       => $version,
    'hastargz'      => 1,
    'src_cd2'       => qq{tomcat-connectors-$version-src/native},
    'url'           => q{http://www.apache.org/dist/tomcat/tomcat-connectors/jk/},
    'display_hide'  => 1,
    'when_i_am_off' => sub {
        my $self = shift;
        return remove_modjk($self);
    },
    'step' => {
        '0' => {
            'name'    => q{Cleaning previous mod_jk},
            'command' => sub {
                my $self = shift;
                return remove_modjk($self);
            },
        },
        '1' => {
            'name'    => 'Configure mod_jk',
            'command' => sub {
                my $self     = shift;
                my $withapxs = sprintf( '--with-apxs=%s', $self->_get_main_apxs_bin() );
                my @rc       = $self->run_system_cmd_returnable( [ './configure', $withapxs ] );
                if ( !$rc[0] ) {
                    push @{ $self->{'attach_files_to_report'} }, $self->cwd() . '/config.log' if ( -e $self->cwd() . '/config.log' );
                }
                return @rc;
            },
        },
        '2' => {
            'name'    => 'Build mod_jk',
            'command' => sub {
                my $self = shift;
                return $self->run_system_cmd_returnable( ['make'] );
            },
        },
        '3' => {
            'name'    => 'Install mod_jk',
            'command' => sub {
                my $self = shift;
                return $self->run_system_cmd_returnable( [qw(make install)] );
            },
        },
        '4' => {
            'name'    => 'Libtool mod_jk',
            'command' => sub {
                my $self  = shift;
                my $sodir = $self->_get_main_httpd_moddir();
                return $self->run_system_cmd_returnable( [ qw(libtool --finish), $sodir ] );
            },
        },
        '5' => {
            'name'    => 'Copying jk.conf',
            'command' => sub {
                my $self = shift;
                my $so   = sprintf( '%s/jk.conf', $self->_get_main_httpd_confdir() );
                my @rc   = Cpanel::FileUtils::safecopy( '../../jk.conf', $so );
                @rc = $rc[0] ? ( 1, 'Ok' ) : ( 0, q{Failed to copy jk.conf to '[_1]'}, $so );
                return @rc;
            },
        },
        '6' => {
            'name'    => 'LoadModule mod_jk',
            'command' => sub {
                my $self = shift;
                return $self->ensure_loadmodule_in_httpdconf( 'jk', 'mod_jk.so' );
              }
        },
        '7' => {
            'name'    => 'Include jk.conf',
            'command' => sub {
                my $self = shift;
                my $jk = sprintf( '%s/jk.conf', $self->_get_main_httpd_confdir() );
                return $self->include_directive_handler(
                    {
                        'include_path' => $jk,
                        'addmodule'    => qr{mod_(?:tomcat|jk)},
                        'loadmodule'   => qr{(?:tomcat|jk)_module},
                    }
                );
            },
        },
        '8' => {
            'name'    => 'Record mod_jk config directory for tomcat install',
            'command' => sub {
                my $self  = shift;
                my $start = $self->cwd();
                my @rc;

                if ( chdir('../conf') ) {
                    Cpanel::Easy::Utils::Tomcat::set_src_modjk_confdir( $self->cwd() );
                    chdir $start;
                    @rc = ( 1, 'Ok' );
                }
                else {
                    @rc = ( 0, q{Failed to change to '[_1]' config directory}, 'mod_jk' );
                }

                return @rc;
            },
        },
    },    # end all steps
};    # end easyconfig hr

# Handy routine to ensure that httpd.conf doesn't contain mod_jk references
sub remove_modjk {
    my $self    = shift;
    my $confdir = $self->_get_main_httpd_confdir();

    # Since this can run any time mod_jk isn't installed, this Only display confirmation of removal if they had this installed, then
    # decided to disable it.  No point in notifying user of these actions
    # if they didn't have it to begin with.
    $self->print_to_screen("Disabling mod_jk") if -e "$confdir/jk.conf";

    # remove mod_jk module from httpd.conf
    my %err;

    my %regex = (
        qr{^[\s]*Include[\s]["]($confdir/jk[.]conf)["]}                                            => q{ # Include "$1"},
        qr{^[\s]*LoadModule[\s]*(tomcat|jk.*)_module[\s]*(libexec|modules)/mod_(tomcat|jk.*)[.]so} => q{# LoadModule ${1}_module $2/mod_$3.so},
        qr{^[\s]*AddModule[\s]*mod_(tomcat|jk.*)[.]c}                                              => q{# AddModule mod_$1.c},
    );
    my @rc = Cpanel::FileUtils::regex_rep_file( $self->_get_main_httpd_conf(), \%regex, \%err ) ? ( 1, 'Ok' ) : ( 0, q{Failed to remove '[_1]' references from httpd.conf}, 'mod_jk' );

    # remove jk.conf
    unlink sprintf( '%s/jk.conf', $confdir );

    return @rc;
}

1;

__END__

