#!/usr/bin/perl

use lib '/usr/local/cpanel', '/scripts';
use Cpanel::Form;
use SafeFile;
require "regsrep.pl";

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;
        }
    }
};

print "<b>Now uninstalling mod_bandwidth</b>\n";
system( "/etc/init.d/httpd", "stop" );

system( "rm", "-rf", "/var/cpanel/apachebw" );
system( "rm", "-f",  "/var/cpanel/cpmodbandwidth" );
system( "rm", "-f",  "/var/cpanel/apachemod/modbandwidth" );
system( "rm", "-rf", "/usr/src/modbandwidth" );
delinclude();
system("/scripts/restartsrv_httpd");
regsrep( "/usr/local/apache/conf/httpd.conf", "LoadModule[\\s]*bandwidth_module[\\s]*libexec/mod_bandwidth.so", "#LoadModule bandwidth_module        libexec/mod_bandwidth.so" );
regsrep( "/usr/local/apache/conf/httpd.conf", "AddModule[\\s]*mod_bandwidth.c", "#AddModule mod_bandwidth.c" );
print "<b>Complete</b>";

sub delinclude {
    my $conflock = SafeFile::safeopen( \*HC, "+<", "/usr/local/apache/conf/httpd.conf" );
    my $addmodule = 0;
    my (@HC);
    while (<HC>) {
        next if (/BandWidthDataDir /);
        push( @HC, $_ );
    }
    seek( HC, 0, 0 );
    print HC join( "", @HC );
    truncate( HC, tell(HC) );
    SafeFile::safeclose( \*HC, $conflock );
}

