#!/usr/bin/perl
# cpanel12 - install (mod_bandwidth)         Copyright(c) 2004-2007 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

use lib '/usr/local/cpanel';
use strict;
use warnings;
use SafeFile;
use Cpanel::Form;
use Cpanel::SafeRun ();

eval {
    require Cpanel::Config;
    if ( defined &Cpanel::Config::is_ea3 ) {
        if ( Cpanel::Config::is_ea3() ) {
            print "This module is now managed via easyapache.\n";
            print "To keep this message from appearing: ";
            print "Remove its entry from /var/cpanel/addonmodules\n";
            exit;
        }
    }
};

my $apache_ver = get_httpd_version();
if ( $apache_ver !~ m/^1\.3/ ) {
    print <<"EOM";


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Unable to install the Bandwidth addon. This addon is designed specifically
for Apache 1.3. You are currently running Apache $apache_ver. This addon
has been included in the latest version of EasyApache.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


EOM
    exit;
}
else {
    print "Detected Apache $apache_ver\n";
}

my $workdir = `pwd`;

print "<b>Now installing mod_bandwidth</b>\n";
umask(0022);
if ( !-d "/usr/src/modbandwidth" ) { mkdir("/usr/src/modbandwidth"); }
if ( !-d "/var/cpanel/apachemod" ) { mkdir("/var/cpanel/apachemod"); }
system( "cp", "-f", "mod_bandwidth.c",     "/usr/src/modbandwidth" );
system( "cp", "-f", "modbandwidth.apache", "/var/cpanel/apachemod" );
system("/var/cpanel/apachemod/modbandwidth.apache");
mkdir("/var/cpanel/apachebw");
system( 'chown', "nobody", "nobody", "/var/cpanel/apachebw" );
mkdir("/var/cpanel/apachebw/master");
system( 'chown', "nobody", "/var/cpanel/apachebw/master" );
mkdir("/var/cpanel/apachebw/link");
system( 'chown', "nobody", "nobody", "/var/cpanel/apachebw/link" );
system( "cp", "-f", "setbwlimit", "/scripts" );
addconfig();
system( "touch", "/var/cpanel/cpmodbandwidth" );
system("/scripts/restartsrv_httpd");
print "<b>Install complete!\n";
print "To set limits on a domain please run <i>/scripts/setbwlimit</i> from root SSH</b>\n";

sub addconfig {
    my $conflock  = SafeFile::safeopen( \*HC, "+<", "/usr/local/apache/conf/httpd.conf" );
    my $line      = 0;
    my $addmodule = 0;
    my @HC;
    while (<HC>) {
        next if (/BandWidthDataDir/i);
        $line++;
        if (/^[\s\t]*AddModule/) { $addmodule = $line; }
        push( @HC, $_ );
    }
    seek( HC, 0, 0 );

    foreach (@HC) {
        $line++;
        if ( $line == ( $addmodule + 1 ) ) {
            print HC "BandWidthDataDir \"/var/cpanel/apachebw\"\n";
        }
        print HC $_;
    }
    truncate( HC, tell(HC) );
    SafeFile::safeclose( \*HC, $conflock );
}

sub get_httpd_version {
    my $bin = '/usr/local/apache/bin/httpd';
    return 'UNKNOWN' if !-x $bin;
    if ( Cpanel::SafeRun::saferunallerrors( $bin, '-v' ) =~ m{Apache/(\d+\.\d+\.\d+)}m ) {
        return $1;
    }
    return 'UNKNOWN';
}

