#!/usr/bin/perl
# cpanel12 - setbwlimit                      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 SafeFile;

if ( !-e "/var/cpanel/cpmodbandwidth" ) {
    print "Sorry you must install the cPanel mod_bandwidth addon module.";
    exit;
}

die if ( !-e "/usr/local/apache/conf/httpd.conf" );

my $domain = $ARGV[0];
$domain =~ s/\n//g;

my $limit = $ARGV[1];
$limit =~ s/\n//g;

$| = 1;

if (!$domain) {
    if ( -t STDIN ) {
        print "What is the domainname you want to setup? ";
        $domain = <STDIN>;
        chomp($domain);
    }
    else { die "stdin is not a tty and no domain was given to us"; }
}

if ( $limit !~ m/^[0-9]+$/ ) {
    $limit = "";
}
if ( $limit eq "" ) {
    if ( -t STDIN ) {
        print "What is the limit (in bytes/sec you want to setup (hint 1024 = 1kb)? ";
        $limit = <STDIN>;
        chomp($limit);
        if ( $limit !~ /^[0-9]+$/ ) {
            die "Limit must be a number!\n";
        }
    }
    else { die "stdin is not a tty and limit was given to us"; }
}

$domain =~ s/^www\.//g;
print "Scanning httpd.conf.";

$addedhost = 0;
$conflock = SafeFile::safeopen( \*HC, "+<", "/usr/local/apache/conf/httpd.conf" );
while (<HC>) {
    push( @HC, $_ );
}
seek( HC, 0, 0 );
foreach (@HC) {
    if ( !/^#/ ) {
        if (/<virtualhost/i) {
            $bh  = 1;
            $ivh = 1;
        }
        if (/<\/virtualhost/i) {
            if ($bh) {
                $owner   = getdomainowner($sn);
                $homedir = gethomedir($owner);
                print HC "\n<IfModule mod_bandwidth.c>\n" . "<Directory \"${homedir}/public_html\">\n" . "   BandWidth all ${limit}\n</Directory>\n</IfModule>\n\n";
                $addedhost = 1;
            }
            $ivh = 0;
        }
        if ($ivh) {
            if (/mod_bandwidth/i) {
                $bh = 0;
            }
            if (/ServerName (\S+)/i) {
                $sn = $1;
                $sn =~ s/^www\.//g;
                if ( $sn ne $domain ) { $bh = 0; }
            }
        }
    }
    print HC $_;
}
truncate( HC, tell(HC) );

SafeFile::safeclose( \*HC, $conflock );

if ($addedhost) {
    safeaprestart();
    print "mod_bandwidth support installed!\n";
}
else {
    print "\n$domain not found or mod_bandwidth already installed!\n";
}

sub safeaprestart {
    if ( -x "/usr/local/cpanel/bin/safeapacherestart" ) {
        system("/usr/local/cpanel/bin/safeapacherestart");
    }
    else {
        if ( -x "/usr/bin/killall" ) {
            system( "/usr/bin/killall", "-USR1", "httpd" );
        }
        elsif ( -x "/usr/sbin/killall" ) {
            system( "/usr/sbin/killall", "-USR1", "httpd" );
        }
        elsif ( -x "/sbin/killall" ) {
            system( "/sbin/killall", "-USR1", "httpd" );
        }
        elsif ( -x "/bin/killall" ) {
            system( "/bin/killall", "-USR1", "httpd" );
        }
        elsif ( -x "/usr/local/bin/killall" ) {
            system( "/usr/local/bin/killall", "-USR1", "httpd" );
        }
        elsif ( -x "/usr/local/sbin/killall" ) {
            system( "/usr/local/sbin/killall", "-USR1", "httpd" );
        }
    }
}

sub gethomedir {
    local $_;
    my ($user) = @_;
    my ($homedir);
    open( PASSWD, "/etc/passwd" );
    while (<PASSWD>) {
        if (/^$user:/) {
            ( undef, undef, undef, undef, undef, $homedir, undef ) = split( /:/, $_, 7 );
            while ( -l $homedir ) {
                $homedir = readlink($homedir);
            }
            close(PASSWD);

            #$_ = "";
            return ($homedir);
        }
    }
    close(PASSWD);

}

sub getdomainowner {
    local $_;
    my ($domain) = @_;
    my ($user)   = '';
    open( USERDOMS, "/etc/userdomains" );
    seek( USERDOMS, 0, 0 );
    while (<USERDOMS>) {
        s/\n//g;
        if (/^$domain: (\S+)/) {
            $user = $1;
            last;
        }
    }
    close(USERDOMS);
    return $user;
}

