package Cpanel::Easy::ModQos;

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

use warnings;
use strict;

my $version       = '11.5';
my $major_version = '11';

our $easyconfig = {
    'name'     => qq{Mod Qos $version},
    'note'     => qq{See documentation at http://mod-qos.sourceforge.net/},
    'version'  => $version,
    'hastargz' => 1,
    'url'      => 'http://mod-qos.sourceforge.net/',
    'implies'  => {
        'Cpanel::Easy::Apache::UniqueId' => 1,
    },
    'modself' => sub {
        my ( $easy, $self_hr, $profile_hr ) = @_;
        my $apver = $profile_hr->{'Apache'}{'version'};

        # modqos does not currently compile correctly under apache 2.4
        if ( $apver eq '2_4' ) {
            $self_hr->{'hastargz'}                   = 0;
            $self_hr->{'skip'}                       = 1;
            $self_hr->{'treat_as_off_while_skipped'} = 1;
            return ( 0, "ModQos unsupported with Apache version $apver" );
        }

        for my $mpm (qw(Event Itk Prefork)) {
            if ( $profile_hr->{"Cpanel::Easy::Apache::MPM$mpm"} ) {
                $self_hr->{'hastargz'}                   = 0;
                $self_hr->{'skip'}                       = 1;
                $self_hr->{'treat_as_off_while_skipped'} = 1;
                $mpm =~ s/Itk/ITK/;
                return ( 0, "ModQos is unsupported with MPM $mpm" );
            }
        }

        return ( 1, 'Ok' );
    },
    'step' => {
        '0' => {
            'name'    => 'apxsing qos module',
            'command' => sub {
                my ($self) = @_;
                my $path = "mod_qos-$major_version/apache2/mod_qos.c";
                my @apxs_cmd = ( $self->_get_main_apxs_bin(), qw(-i -a -c), $path );
                return $self->run_system_cmd_returnable( [@apxs_cmd] );
            },
        },
        '1' => {
            'name'    => 'activating qos module',
            'command' => sub {
                my ($self) = @_;
                my ( $status, $msg ) = $self->ensure_loadmodule_in_httpdconf( 'qos', 'mod_qos.so' );
                if ( !$status ) {
                    return ( q{ensure_loadmodule_in_httpdconf() did not return true, [_1]}, $msg );
                }
                return ( 1, 'Ok' );
            },
        },
    },
};

1;
