#!/usr/bin/perl
# cpanel - autofixer/log_processing_patch         Copyright(c) 2010 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 '/scripts';
use strict;
use warnings;

open( my $fh, '<', '/etc/userdomains' ) or die "Unable to open /etc/userdomains: $!\n";
my $domaincnt = () = <$fh>;
close $fh;

exit unless $domaincnt > 300;

{
    # Process config file in place.
    open( my $fh, '<', '/var/cpanel/cpanel.config' ) or die "Unable to open config file: $!\n";
    my ( $bwcycle_found, $cycle_found );
    while (<$fh>) {

        # Only counts if not default value.
        $bwcycle_found = 1 if /^bwcycle=(.*)/ && $1 != 2;

        # Any value counts.
        $cycle_found = 1 if /^cycle_hours=/;
    }
    open( $fh, '>>', '/var/cpanel/cpanel.config' ) or die "Unable to modify config file: $!\n";
    print $fh "bwcycle=12\n"     unless $bwcycle_found;
    print $fh "cycle_hours=24\n" unless $cycle_found;
}

opendir( my $dh, '/var/cpanel/lastrun' ) or die "Unable to open lastrun dir: $!\n";
my @dirs = grep { !/^\./ } readdir $dh;
closedir($dh);

while (@dirs) {
    system '/bin/touch', map { ( "/var/cpanel/lastrun/$_/stats", "/var/cpanel/lastrun/$_/bandwidth" ) } splice @dirs, 0, 20;
}
