package Cpanel::Easy::ModBandwidth;

# cpanel - Cpanel/Easy/ModBandwidth.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

my $version = '0.92';

our $easyconfig = {
    'name'          => qq{Mod Bandwidth $version},
    'note'          => qq{See documentation that can found by clicking "More Info" for details},
    'version'       => $version,
    'hastargz'      => 1,
    'url'           => 'http://go.cpanel.net/modbandwidth',                                        # ap1 - http://www.cohprog.com/mod_bandwidth.html , ap2 - http://apache.ivn.cl/
    'when_i_am_off' => sub {
        my ($self) = @_;
        unlink '/usr/local/cpanel/scripts/setbwlimit' if -e '/usr/local/cpanel/scripts/setbwlimit';
    },
    'step' => {
        '0' => {
            'name'    => 'patching bandwidth module',
            'command' => sub {
                my ($self) = @_;
                my $apv = $self->_get_current_apache_version_key();

                if ( !defined $apv || !$apv ) {
                    return ( 0, q{Could not determine '[_1]' version}, 'apache' );
                }

                if ( $apv lt '2_4' ) {
                    return ( 1, q{Patch only applies to Apache 2.4 or later} );
                }

                my $start = $self->cwd();
                warn "Starting directory = $start";
                my $src = "mod_bw";
                chdir $src or return ( 0, q{Could not chdir into '[_1]': [_2]}, $src, $! );
                my @result = $self->apply_patch( '../cppatch/mod_bw-apache24-api.patch', );
                chdir $start or return ( 0, q{Could not chdir into '[_1]': [_2]}, $start, $! );
                return @result;
            },
        },
        '0.25' => {
            'name'    => 'apxsing bandwidth module',
            'command' => sub {
                my ($self) = @_;

                my $path = 'mod_bw/mod_bw.c';
                return $self->run_system_cmd_returnable( [ $self->_get_main_apxs_bin(), qw(-i -a -c), $path ] );
            },
        },
        '0.5' => {
            'name'    => 'activating bandwidth module',
            'command' => sub {
                my ($self) = @_;

                $self->ensure_loadmodule_in_httpdconf( 'bw', 'mod_bw.so' );
                return ( 1, 'Ok' );
            },
        },
        '1' => {
            'name'    => 'setup setbwlimit script',
            'command' => sub {
                my ($self) = @_;
                my $script = '/usr/local/cpanel/scripts/setbwlimit';

                $self->copy_file( 'setbwlimit', $script );
                return ( 0, q{Copy '[_1]' to '[_2]' failed: [_3]}, 'setupbwlimit', $script, $! ) if !-e $script;

                chmod 0700, $script;
                chown 0, 0, $script;

                return ( 1, 'ok' );
            },
        },
        '2' => {
            'name'    => 'bandwidth module httpd.conf setup',
            'command' => sub {
                my ($self) = @_;

                my $new_line = <<"END_BW";
<IfModule mod_bandwidth.c>
   BandWidthDataDir "/var/cpanel/apachebw"
</IfModule>

END_BW
                my $file = '/usr/local/apache/conf/mod_bandwidth.conf';

                if ( !-e $file ) {
                    if ( open my $inc_fh, '>', $file ) {
                        print {$inc_fh} $new_line;
                        close $inc_fh;
                    }
                    else {
                        return ( 0, q{Could not open() file '[_1]' for writing: [_2]}, $file, $! );
                    }
                }

                mkdir '/var/cpanel/apachebw' if !-d '/var/cpanel/apachebw';
                chown scalar getpwnam('nobody'), scalar getgrnam('nobody'), '/var/cpanel/apachebw';

                # yes both, new style, and old style respectively
                $self->strip_from_httpconf( '<IfModule mod_bandwidth.c>', 'BandWidthDataDir "/var/cpanel/apachebw"' );

                return $self->include_directive_handler(
                    {
                        'include_path' => $file,
                        'addmodule'    => qr{mod_(?:bw|bandwidth)},
                        'loadmodule'   => qr{(?:bw|bandwidth)_module},
                    }
                );
            },
        },
    },
};

1;
